2008-06-23 13:25:22 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2008-06-23 13:25:22 +00:00
|
|
|
//
|
2014-10-17 15:31:11 +02:00
|
|
|
// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2008-06-23 13:25:22 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2008-06-23 13:25:22 +00:00
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
#ifndef EIGEN_COMPRESSED_STORAGE_H
|
|
|
|
|
#define EIGEN_COMPRESSED_STORAGE_H
|
2008-06-23 13:25:22 +00:00
|
|
|
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2011-12-02 10:00:24 +01:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
/** \internal
|
|
|
|
|
* Stores a sparse set of values as a list of values and a list of indices.
|
2008-06-23 13:25:22 +00:00
|
|
|
*
|
|
|
|
|
*/
|
2021-08-04 22:41:52 +00:00
|
|
|
template<typename Scalar_,typename StorageIndex_>
|
2009-01-15 12:52:59 +00:00
|
|
|
class CompressedStorage
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
2010-06-03 08:41:11 +02:00
|
|
|
public:
|
|
|
|
|
|
2021-08-04 22:41:52 +00:00
|
|
|
typedef Scalar_ Scalar;
|
|
|
|
|
typedef StorageIndex_ StorageIndex;
|
2010-06-03 08:41:11 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
2009-01-21 18:46:04 +00:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2008-06-23 13:25:22 +00:00
|
|
|
public:
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
CompressedStorage()
|
2008-06-29 21:29:12 +00:00
|
|
|
: m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
|
2008-06-23 13:25:22 +00:00
|
|
|
{}
|
|
|
|
|
|
2015-02-13 18:57:41 +01:00
|
|
|
explicit CompressedStorage(Index size)
|
2008-06-29 21:29:12 +00:00
|
|
|
: m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
|
|
|
|
resize(size);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
CompressedStorage(const CompressedStorage& other)
|
2009-01-19 15:20:45 +00:00
|
|
|
: m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
|
|
|
|
*this = other;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
CompressedStorage& operator=(const CompressedStorage& other)
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
2015-06-20 13:59:13 +02:00
|
|
|
resize(other.size());
|
2015-05-25 22:30:56 +02:00
|
|
|
if(other.size()>0)
|
|
|
|
|
{
|
2019-11-05 17:17:58 -08:00
|
|
|
internal::smart_copy(other.m_values, other.m_values + m_size, m_values);
|
|
|
|
|
internal::smart_copy(other.m_indices, other.m_indices + m_size, m_indices);
|
2015-05-25 22:30:56 +02:00
|
|
|
}
|
2008-08-22 01:19:53 +00:00
|
|
|
return *this;
|
2008-06-26 23:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
void swap(CompressedStorage& other)
|
2008-06-26 23:22:26 +00:00
|
|
|
{
|
2008-06-29 21:29:12 +00:00
|
|
|
std::swap(m_values, other.m_values);
|
|
|
|
|
std::swap(m_indices, other.m_indices);
|
|
|
|
|
std::swap(m_size, other.m_size);
|
|
|
|
|
std::swap(m_allocatedSize, other.m_allocatedSize);
|
2008-06-26 23:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
~CompressedStorage()
|
2008-06-26 23:22:26 +00:00
|
|
|
{
|
2022-08-23 21:44:22 +00:00
|
|
|
conditional_aligned_delete_auto<Scalar, true>(m_values, m_allocatedSize);
|
|
|
|
|
conditional_aligned_delete_auto<StorageIndex, true>(m_indices, m_allocatedSize);
|
2008-06-23 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-13 18:57:41 +01:00
|
|
|
void reserve(Index size)
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
2015-02-13 18:57:41 +01:00
|
|
|
Index newAllocatedSize = m_size + size;
|
2008-06-23 13:25:22 +00:00
|
|
|
if (newAllocatedSize > m_allocatedSize)
|
2008-06-29 21:29:12 +00:00
|
|
|
reallocate(newAllocatedSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void squeeze()
|
|
|
|
|
{
|
|
|
|
|
if (m_allocatedSize>m_size)
|
|
|
|
|
reallocate(m_size);
|
2008-06-23 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-13 18:57:41 +01:00
|
|
|
void resize(Index size, double reserveSizeFactor = 0)
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
|
|
|
|
if (m_allocatedSize<size)
|
2015-03-13 20:57:33 +01:00
|
|
|
{
|
|
|
|
|
Index realloc_size = (std::min<Index>)(NumTraits<StorageIndex>::highest(), size + Index(reserveSizeFactor*double(size)));
|
|
|
|
|
if(realloc_size<size)
|
|
|
|
|
internal::throw_std_bad_alloc();
|
|
|
|
|
reallocate(realloc_size);
|
|
|
|
|
}
|
2008-06-23 13:25:22 +00:00
|
|
|
m_size = size;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-30 16:00:58 -04:00
|
|
|
void append(const Scalar& v, Index i)
|
2008-06-23 13:25:22 +00:00
|
|
|
{
|
2014-12-04 22:48:53 +01:00
|
|
|
Index id = m_size;
|
2008-06-23 13:25:22 +00:00
|
|
|
resize(m_size+1, 1);
|
|
|
|
|
m_values[id] = v;
|
2014-12-04 22:48:53 +01:00
|
|
|
m_indices[id] = internal::convert_index<StorageIndex>(i);
|
2008-06-23 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-13 18:57:41 +01:00
|
|
|
inline Index size() const { return m_size; }
|
|
|
|
|
inline Index allocatedSize() const { return m_allocatedSize; }
|
2009-01-19 15:20:45 +00:00
|
|
|
inline void clear() { m_size = 0; }
|
2008-06-23 13:25:22 +00:00
|
|
|
|
2016-02-27 14:55:40 +01:00
|
|
|
const Scalar* valuePtr() const { return m_values; }
|
|
|
|
|
Scalar* valuePtr() { return m_values; }
|
|
|
|
|
const StorageIndex* indexPtr() const { return m_indices; }
|
|
|
|
|
StorageIndex* indexPtr() { return m_indices; }
|
2008-06-23 13:25:22 +00:00
|
|
|
|
2016-02-27 14:55:40 +01:00
|
|
|
inline Scalar& value(Index i) { eigen_internal_assert(m_values!=0); return m_values[i]; }
|
|
|
|
|
inline const Scalar& value(Index i) const { eigen_internal_assert(m_values!=0); return m_values[i]; }
|
|
|
|
|
|
|
|
|
|
inline StorageIndex& index(Index i) { eigen_internal_assert(m_indices!=0); return m_indices[i]; }
|
|
|
|
|
inline const StorageIndex& index(Index i) const { eigen_internal_assert(m_indices!=0); return m_indices[i]; }
|
2008-06-29 21:29:12 +00:00
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
/** \returns the largest \c k such that for all \c j in [0,k) index[\c j]\<\a key */
|
2015-02-17 18:52:39 +01:00
|
|
|
inline Index searchLowerIndex(Index key) const
|
2009-01-19 15:20:45 +00:00
|
|
|
{
|
|
|
|
|
return searchLowerIndex(0, m_size, key);
|
|
|
|
|
}
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
/** \returns the largest \c k in [start,end) such that for all \c j in [start,k) index[\c j]\<\a key */
|
2015-02-17 18:52:39 +01:00
|
|
|
inline Index searchLowerIndex(Index start, Index end, Index key) const
|
2009-01-19 15:20:45 +00:00
|
|
|
{
|
|
|
|
|
while(end>start)
|
|
|
|
|
{
|
2015-02-13 18:57:41 +01:00
|
|
|
Index mid = (end+start)>>1;
|
2009-01-19 15:20:45 +00:00
|
|
|
if (m_indices[mid]<key)
|
|
|
|
|
start = mid+1;
|
|
|
|
|
else
|
|
|
|
|
end = mid;
|
|
|
|
|
}
|
2015-02-13 18:57:41 +01:00
|
|
|
return start;
|
2009-01-19 15:20:45 +00:00
|
|
|
}
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
/** \returns the stored value at index \a key
|
|
|
|
|
* If the value does not exist, then the value \a defaultValue is returned without any insertion. */
|
2015-02-17 18:52:39 +01:00
|
|
|
inline Scalar at(Index key, const Scalar& defaultValue = Scalar(0)) const
|
2009-01-19 15:20:45 +00:00
|
|
|
{
|
|
|
|
|
if (m_size==0)
|
|
|
|
|
return defaultValue;
|
|
|
|
|
else if (key==m_indices[m_size-1])
|
|
|
|
|
return m_values[m_size-1];
|
|
|
|
|
// ^^ optimization: let's first check if it is the last coefficient
|
|
|
|
|
// (very common in high level algorithms)
|
2015-02-13 18:57:41 +01:00
|
|
|
const Index id = searchLowerIndex(0,m_size-1,key);
|
2009-01-19 15:20:45 +00:00
|
|
|
return ((id<m_size) && (m_indices[id]==key)) ? m_values[id] : defaultValue;
|
|
|
|
|
}
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
/** Like at(), but the search is performed in the range [start,end) */
|
2015-02-17 18:52:39 +01:00
|
|
|
inline Scalar atInRange(Index start, Index end, Index key, const Scalar &defaultValue = Scalar(0)) const
|
2009-01-19 15:20:45 +00:00
|
|
|
{
|
2009-05-04 14:25:12 +00:00
|
|
|
if (start>=end)
|
2014-12-01 14:41:39 +01:00
|
|
|
return defaultValue;
|
2009-01-19 15:20:45 +00:00
|
|
|
else if (end>start && key==m_indices[end-1])
|
|
|
|
|
return m_values[end-1];
|
|
|
|
|
// ^^ optimization: let's first check if it is the last coefficient
|
|
|
|
|
// (very common in high level algorithms)
|
2015-02-13 18:57:41 +01:00
|
|
|
const Index id = searchLowerIndex(start,end-1,key);
|
2009-01-19 15:20:45 +00:00
|
|
|
return ((id<end) && (m_indices[id]==key)) ? m_values[id] : defaultValue;
|
|
|
|
|
}
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
/** \returns a reference to the value at index \a key
|
|
|
|
|
* If the value does not exist, then the value \a defaultValue is inserted
|
|
|
|
|
* such that the keys are sorted. */
|
2015-02-17 18:52:39 +01:00
|
|
|
inline Scalar& atWithInsertion(Index key, const Scalar& defaultValue = Scalar(0))
|
2009-01-19 15:20:45 +00:00
|
|
|
{
|
2015-02-13 18:57:41 +01:00
|
|
|
Index id = searchLowerIndex(0,m_size,key);
|
2009-01-19 15:20:45 +00:00
|
|
|
if (id>=m_size || m_indices[id]!=key)
|
|
|
|
|
{
|
2014-10-09 23:35:49 +02:00
|
|
|
if (m_allocatedSize<m_size+1)
|
2009-01-19 15:20:45 +00:00
|
|
|
{
|
2022-08-23 21:44:22 +00:00
|
|
|
Index newAllocatedSize = 2 * (m_size + 1);
|
|
|
|
|
m_values = conditional_aligned_realloc_new_auto<Scalar, true>(m_values, newAllocatedSize, m_allocatedSize);
|
|
|
|
|
m_indices =
|
|
|
|
|
conditional_aligned_realloc_new_auto<StorageIndex, true>(m_indices, newAllocatedSize, m_allocatedSize);
|
|
|
|
|
m_allocatedSize = newAllocatedSize;
|
2009-01-19 15:20:45 +00:00
|
|
|
}
|
2022-08-23 21:44:22 +00:00
|
|
|
if(m_size>id)
|
2014-10-09 23:35:49 +02:00
|
|
|
{
|
|
|
|
|
internal::smart_memmove(m_values +id, m_values +m_size, m_values +id+1);
|
|
|
|
|
internal::smart_memmove(m_indices+id, m_indices+m_size, m_indices+id+1);
|
|
|
|
|
}
|
|
|
|
|
m_size++;
|
2015-02-17 18:52:39 +01:00
|
|
|
m_indices[id] = internal::convert_index<StorageIndex>(key);
|
2009-01-19 15:20:45 +00:00
|
|
|
m_values[id] = defaultValue;
|
|
|
|
|
}
|
|
|
|
|
return m_values[id];
|
|
|
|
|
}
|
2010-06-03 08:41:11 +02:00
|
|
|
|
2019-01-28 17:29:50 +01:00
|
|
|
void moveChunk(Index from, Index to, Index chunkSize)
|
|
|
|
|
{
|
|
|
|
|
eigen_internal_assert(to+chunkSize <= m_size);
|
|
|
|
|
if(to>from && from+chunkSize>to)
|
|
|
|
|
{
|
|
|
|
|
// move backward
|
|
|
|
|
internal::smart_memmove(m_values+from, m_values+from+chunkSize, m_values+to);
|
|
|
|
|
internal::smart_memmove(m_indices+from, m_indices+from+chunkSize, m_indices+to);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-11-05 17:17:58 -08:00
|
|
|
internal::smart_copy(m_values+from, m_values+from+chunkSize, m_values+to);
|
|
|
|
|
internal::smart_copy(m_indices+from, m_indices+from+chunkSize, m_indices+to);
|
2019-01-28 17:29:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-29 21:29:12 +00:00
|
|
|
protected:
|
|
|
|
|
|
2015-02-13 18:57:41 +01:00
|
|
|
inline void reallocate(Index size)
|
2008-06-29 21:29:12 +00:00
|
|
|
{
|
2015-03-04 10:16:46 +01:00
|
|
|
#ifdef EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
|
|
|
|
|
EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
|
|
|
|
|
#endif
|
2014-10-09 23:35:05 +02:00
|
|
|
eigen_internal_assert(size!=m_allocatedSize);
|
2022-08-23 21:44:22 +00:00
|
|
|
m_values = conditional_aligned_realloc_new_auto<Scalar, true>(m_values, size, m_allocatedSize);
|
|
|
|
|
m_indices = conditional_aligned_realloc_new_auto<StorageIndex, true>(m_indices, size, m_allocatedSize);
|
2008-06-29 21:29:12 +00:00
|
|
|
m_allocatedSize = size;
|
|
|
|
|
}
|
2008-06-23 13:25:22 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Scalar* m_values;
|
2014-12-04 22:48:53 +01:00
|
|
|
StorageIndex* m_indices;
|
2015-02-13 18:57:41 +01:00
|
|
|
Index m_size;
|
|
|
|
|
Index m_allocatedSize;
|
2008-06-26 23:22:26 +00:00
|
|
|
|
2008-06-23 13:25:22 +00:00
|
|
|
};
|
|
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
|
|
} // end namespace Eigen
|
2011-12-02 10:00:24 +01:00
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
#endif // EIGEN_COMPRESSED_STORAGE_H
|