11#ifndef EIGEN_COMPRESSED_STORAGE_H
12#define EIGEN_COMPRESSED_STORAGE_H
15#include "./InternalHeaderCheck.h"
25template <
typename Scalar_,
typename StorageIndex_>
26class CompressedStorage {
28 using Scalar = Scalar_;
29 using StorageIndex = StorageIndex_;
31 CompressedStorage() =
default;
33 explicit CompressedStorage(Index size) : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) { resize(size); }
35 CompressedStorage(
const CompressedStorage& other) : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) {
39 CompressedStorage& operator=(
const CompressedStorage& other) {
41 if (other.size() > 0) {
42 internal::smart_copy(other.m_values, other.m_values + m_size, m_values);
43 internal::smart_copy(other.m_indices, other.m_indices + m_size, m_indices);
48 void swap(CompressedStorage& other) {
49 std::swap(m_values, other.m_values);
50 std::swap(m_indices, other.m_indices);
51 std::swap(m_size, other.m_size);
52 std::swap(m_allocatedSize, other.m_allocatedSize);
55 ~CompressedStorage() {
56 conditional_aligned_delete_auto<Scalar, true>(m_values, m_allocatedSize);
57 conditional_aligned_delete_auto<StorageIndex, true>(m_indices, m_allocatedSize);
60 void reserve(Index size) {
61 Index newAllocatedSize = m_size + size;
62 if (newAllocatedSize > m_allocatedSize) reallocate(newAllocatedSize);
66 if (m_allocatedSize > m_size) reallocate(m_size);
69 void resize(Index size,
double reserveSizeFactor = 0) {
70 if (m_allocatedSize < size) {
72 using SmallerIndexType = std::conditional_t<static_cast<size_t>((std::numeric_limits<Index>::max)()) <
73 static_cast<size_t>((std::numeric_limits<StorageIndex>::max)()),
76 (std::min<Index>)(NumTraits<SmallerIndexType>::highest(), size + Index(reserveSizeFactor *
double(size)));
77 if (realloc_size < size) internal::throw_std_bad_alloc();
78 reallocate(realloc_size);
83 void append(
const Scalar& v, Index i) {
85 resize(m_size + 1, 1);
87 m_indices[id] = internal::convert_index<StorageIndex>(i);
90 inline Index size()
const {
return m_size; }
91 inline Index allocatedSize()
const {
return m_allocatedSize; }
92 inline void clear() { m_size = 0; }
94 const Scalar* valuePtr()
const {
return m_values; }
95 Scalar* valuePtr() {
return m_values; }
96 const StorageIndex* indexPtr()
const {
return m_indices; }
97 StorageIndex* indexPtr() {
return m_indices; }
99 inline Scalar& value(Index i) {
100 eigen_internal_assert(m_values != 0);
103 inline const Scalar& value(Index i)
const {
104 eigen_internal_assert(m_values != 0);
108 inline StorageIndex& index(Index i) {
109 eigen_internal_assert(m_indices != 0);
112 inline const StorageIndex& index(Index i)
const {
113 eigen_internal_assert(m_indices != 0);
118 inline Index searchLowerIndex(Index key)
const {
return searchLowerIndex(0, m_size, key); }
121 inline Index searchLowerIndex(Index start, Index end, Index key)
const {
122 return static_cast<Index
>(std::distance(m_indices, std::lower_bound(m_indices + start, m_indices + end, key)));
127 inline Scalar at(Index key,
const Scalar& defaultValue = Scalar(0))
const {
130 else if (key == m_indices[m_size - 1])
131 return m_values[m_size - 1];
134 const Index
id = searchLowerIndex(0, m_size - 1, key);
135 return ((
id < m_size) && (m_indices[
id] == key)) ? m_values[id] : defaultValue;
139 inline Scalar atInRange(Index start, Index end, Index key,
const Scalar& defaultValue = Scalar(0))
const {
142 else if (end > start && key == m_indices[end - 1])
143 return m_values[end - 1];
146 const Index
id = searchLowerIndex(start, end - 1, key);
147 return ((
id < end) && (m_indices[
id] == key)) ? m_values[id] : defaultValue;
153 inline Scalar& atWithInsertion(Index key,
const Scalar& defaultValue = Scalar(0)) {
154 Index
id = searchLowerIndex(0, m_size, key);
155 if (
id >= m_size || m_indices[
id] != key) {
156 if (m_allocatedSize < m_size + 1) {
157 Index newAllocatedSize = 2 * (m_size + 1);
158 m_values = conditional_aligned_realloc_new_auto<Scalar, true>(m_values, newAllocatedSize, m_allocatedSize);
160 conditional_aligned_realloc_new_auto<StorageIndex, true>(m_indices, newAllocatedSize, m_allocatedSize);
161 m_allocatedSize = newAllocatedSize;
164 internal::smart_memmove(m_values +
id, m_values + m_size, m_values +
id + 1);
165 internal::smart_memmove(m_indices +
id, m_indices + m_size, m_indices +
id + 1);
168 m_indices[id] = internal::convert_index<StorageIndex>(key);
169 m_values[id] = defaultValue;
174 inline void moveChunk(Index from, Index to, Index chunkSize) {
175 eigen_internal_assert(chunkSize >= 0 && to + chunkSize <= m_size);
176 internal::smart_memmove(m_values + from, m_values + from + chunkSize, m_values + to);
177 internal::smart_memmove(m_indices + from, m_indices + from + chunkSize, m_indices + to);
181 inline void reallocate(Index size) {
182#ifdef EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
183 EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
185 eigen_internal_assert(size != m_allocatedSize);
186 m_values = conditional_aligned_realloc_new_auto<Scalar, true>(m_values, size, m_allocatedSize);
187 m_indices = conditional_aligned_realloc_new_auto<StorageIndex, true>(m_indices, size, m_allocatedSize);
188 m_allocatedSize = size;
192 Scalar* m_values =
nullptr;
193 StorageIndex* m_indices =
nullptr;
195 Index m_allocatedSize = 0;