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
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
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 {
|
2010-06-03 08:41:11 +02:00
|
|
|
public:
|
2021-08-04 22:41:52 +00:00
|
|
|
typedef Scalar_ Scalar;
|
|
|
|
|
typedef StorageIndex_ StorageIndex;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-06-03 08:41:11 +02:00
|
|
|
protected:
|
2009-01-21 18:46:04 +00:00
|
|
|
typedef typename NumTraits<Scalar>::Real RealScalar;
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2008-06-23 13:25:22 +00:00
|
|
|
public:
|
2009-01-15 12:52:59 +00:00
|
|
|
CompressedStorage() : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2022-08-23 21:44:22 +00:00
|
|
|
explicit CompressedStorage(Index size) : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) { resize(size); }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2008-06-29 21:29:12 +00:00
|
|
|
CompressedStorage(const CompressedStorage& other) : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) {
|
2008-06-23 13:25:22 +00:00
|
|
|
*this = other;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-02-13 18:57:41 +01:00
|
|
|
CompressedStorage& operator=(const CompressedStorage& other) {
|
|
|
|
|
resize(other.size());
|
2008-06-29 21:29:12 +00:00
|
|
|
if (other.size() > 0) {
|
|
|
|
|
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);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
2008-06-23 13:25:22 +00:00
|
|
|
return *this;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
void swap(CompressedStorage& other) {
|
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);
|
2009-01-15 12:52:59 +00:00
|
|
|
std::swap(m_allocatedSize, other.m_allocatedSize);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
~CompressedStorage() {
|
2009-01-19 15:20:45 +00:00
|
|
|
conditional_aligned_delete_auto<Scalar, true>(m_values, m_allocatedSize);
|
|
|
|
|
conditional_aligned_delete_auto<StorageIndex, true>(m_indices, m_allocatedSize);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
void reserve(Index size) {
|
2008-06-23 13:25:22 +00:00
|
|
|
Index newAllocatedSize = m_size + size;
|
2009-01-15 12:52:59 +00:00
|
|
|
if (newAllocatedSize > m_allocatedSize) reallocate(newAllocatedSize);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2008-06-29 21:29:12 +00:00
|
|
|
void squeeze() {
|
2008-06-23 13:25:22 +00:00
|
|
|
if (m_allocatedSize > m_size) reallocate(m_size);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-15 12:52:59 +00:00
|
|
|
void resize(Index size, double reserveSizeFactor = 0) {
|
2008-06-23 13:25:22 +00:00
|
|
|
if (m_allocatedSize < size) {
|
2022-08-23 21:44:22 +00:00
|
|
|
Index realloc_size =
|
2009-01-15 12:52:59 +00:00
|
|
|
(std::min<Index>)(NumTraits<StorageIndex>::highest(), size + Index(reserveSizeFactor * double(size)));
|
2019-11-05 17:17:58 -08:00
|
|
|
if (realloc_size < size) internal::throw_std_bad_alloc();
|
2008-06-29 21:29:12 +00:00
|
|
|
reallocate(realloc_size);
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
2019-11-05 17:17:58 -08:00
|
|
|
m_size = size;
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2019-11-05 17:17:58 -08:00
|
|
|
void append(const Scalar& v, Index i) {
|
|
|
|
|
Index id = m_size;
|
|
|
|
|
resize(m_size + 1, 1);
|
|
|
|
|
m_values[id] = v;
|
|
|
|
|
m_indices[id] = internal::convert_index<StorageIndex>(i);
|
2015-05-25 22:30:56 +02:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2008-06-29 21:29:12 +00: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; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2016-02-27 14:55:40 +01:00
|
|
|
const Scalar* valuePtr() const { return m_values; }
|
2008-06-29 21:29:12 +00:00
|
|
|
Scalar* valuePtr() { return m_values; }
|
2009-01-15 12:52:59 +00:00
|
|
|
const StorageIndex* indexPtr() const { return m_indices; }
|
2016-02-27 14:55:40 +01:00
|
|
|
StorageIndex* indexPtr() { return m_indices; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2008-06-26 23:22:26 +00:00
|
|
|
inline Scalar& value(Index i) {
|
2022-08-23 21:44:22 +00:00
|
|
|
eigen_internal_assert(m_values != 0);
|
2016-02-27 14:55:40 +01:00
|
|
|
return m_values[i];
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
2022-08-23 21:44:22 +00:00
|
|
|
inline const Scalar& value(Index i) const {
|
|
|
|
|
eigen_internal_assert(m_values != 0);
|
2016-02-27 14:55:40 +01:00
|
|
|
return m_values[i];
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 14:55:40 +01:00
|
|
|
inline StorageIndex& index(Index i) {
|
|
|
|
|
eigen_internal_assert(m_indices != 0);
|
|
|
|
|
return m_indices[i];
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
2022-08-23 21:44:22 +00:00
|
|
|
inline const StorageIndex& index(Index i) const {
|
|
|
|
|
eigen_internal_assert(m_indices != 0);
|
2016-02-27 14:55:40 +01:00
|
|
|
return m_indices[i];
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-23 21:44:22 +00:00
|
|
|
/** \returns the largest \c k such that for all \c j in [0,k) index[\c j]\<\a key */
|
|
|
|
|
inline Index searchLowerIndex(Index key) const { return searchLowerIndex(0, m_size, key); }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2022-08-23 21:44:22 +00:00
|
|
|
/** \returns the largest \c k in [start,end) such that for all \c j in [start,k) index[\c j]\<\a key */
|
|
|
|
|
inline Index searchLowerIndex(Index start, Index end, Index key) const {
|
|
|
|
|
return static_cast<Index>(std::distance(m_indices, std::lower_bound(m_indices + start, m_indices + end, key)));
|
2008-06-23 13:25:22 +00:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2015-02-13 18:57:41 +01: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. */
|
|
|
|
|
inline Scalar at(Index key, const Scalar& defaultValue = Scalar(0)) const {
|
|
|
|
|
if (m_size == 0)
|
|
|
|
|
return defaultValue;
|
|
|
|
|
else if (key == m_indices[m_size - 1])
|
2009-01-19 15:20:45 +00:00
|
|
|
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);
|
|
|
|
|
return ((id < m_size) && (m_indices[id] == key)) ? m_values[id] : defaultValue;
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 20:09:37 +00:00
|
|
|
/** Like at(), but the search is performed in the range [start,end) */
|
|
|
|
|
inline Scalar atInRange(Index start, Index end, Index key, const Scalar& defaultValue = Scalar(0)) const {
|
|
|
|
|
if (start >= end)
|
2009-01-19 15:20:45 +00:00
|
|
|
return defaultValue;
|
|
|
|
|
else if (end > start && key == m_indices[end - 1])
|
|
|
|
|
return m_values[end - 1];
|
2022-12-19 20:09:37 +00:00
|
|
|
// ^^ optimization: let's first check if it is the last coefficient
|
|
|
|
|
// (very common in high level algorithms)
|
|
|
|
|
const Index id = searchLowerIndex(start, end - 1, key);
|
|
|
|
|
return ((id < end) && (m_indices[id] == key)) ? m_values[id] : defaultValue;
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-19 15:20:45 +00:00
|
|
|
/** \returns a reference to the value at index \a key
|
2022-12-19 20:09:37 +00:00
|
|
|
* If the value does not exist, then the value \a defaultValue is inserted
|
2009-01-19 15:20:45 +00:00
|
|
|
* such that the keys are sorted. */
|
2015-02-17 18:52:39 +01:00
|
|
|
inline Scalar& atWithInsertion(Index key, const Scalar& defaultValue = Scalar(0)) {
|
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) {
|
2022-12-19 20:09:37 +00:00
|
|
|
if (m_allocatedSize < m_size + 1) {
|
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 =
|
2022-12-19 20:09:37 +00:00
|
|
|
conditional_aligned_realloc_new_auto<StorageIndex, true>(m_indices, newAllocatedSize, m_allocatedSize);
|
2015-03-13 20:57:33 +01:00
|
|
|
m_allocatedSize = newAllocatedSize;
|
|
|
|
|
}
|
2009-01-19 15:20:45 +00:00
|
|
|
if (m_size > id) {
|
2023-01-07 22:09:42 +00:00
|
|
|
internal::smart_memmove(m_values + id, m_values + m_size, m_values + id + 1);
|
2014-10-09 23:35:49 +02:00
|
|
|
internal::smart_memmove(m_indices + id, m_indices + m_size, m_indices + id + 1);
|
2009-01-19 15:20:45 +00:00
|
|
|
}
|
2014-10-09 23:35:49 +02:00
|
|
|
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;
|
2023-11-29 11:12:48 +00:00
|
|
|
}
|
2009-01-19 15:20:45 +00:00
|
|
|
return m_values[id];
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2023-01-07 22:09:42 +00:00
|
|
|
inline void moveChunk(Index from, Index to, Index chunkSize) {
|
|
|
|
|
eigen_internal_assert(chunkSize >= 0 && to + chunkSize <= m_size);
|
2023-01-20 21:32:32 +00:00
|
|
|
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);
|
2019-01-28 17:29:50 +01:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2008-06-29 21:29:12 +00:00
|
|
|
protected:
|
2015-02-13 18:57:41 +01:00
|
|
|
inline void reallocate(Index size) {
|
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;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
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-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
|