Eigen  5.0.1
 
Loading...
Searching...
No Matches
CompressedStorage.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_COMPRESSED_STORAGE_H
12#define EIGEN_COMPRESSED_STORAGE_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
25template <typename Scalar_, typename StorageIndex_>
26class CompressedStorage {
27 public:
28 using Scalar = Scalar_;
29 using StorageIndex = StorageIndex_;
30
31 CompressedStorage() = default;
32
33 explicit CompressedStorage(Index size) : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) { resize(size); }
34
35 CompressedStorage(const CompressedStorage& other) : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0) {
36 *this = other;
37 }
38
39 CompressedStorage& operator=(const CompressedStorage& other) {
40 resize(other.size());
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);
44 }
45 return *this;
46 }
47
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);
53 }
54
55 ~CompressedStorage() {
56 conditional_aligned_delete_auto<Scalar, true>(m_values, m_allocatedSize);
57 conditional_aligned_delete_auto<StorageIndex, true>(m_indices, m_allocatedSize);
58 }
59
60 void reserve(Index size) {
61 Index newAllocatedSize = m_size + size;
62 if (newAllocatedSize > m_allocatedSize) reallocate(newAllocatedSize);
63 }
64
65 void squeeze() {
66 if (m_allocatedSize > m_size) reallocate(m_size);
67 }
68
69 void resize(Index size, double reserveSizeFactor = 0) {
70 if (m_allocatedSize < size) {
71 // Avoid underflow on the std::min<Index> call by choosing the smaller index type.
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)()),
74 Index, StorageIndex>;
75 Index realloc_size =
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);
79 }
80 m_size = size;
81 }
82
83 void append(const Scalar& v, Index i) {
84 Index id = m_size;
85 resize(m_size + 1, 1);
86 m_values[id] = v;
87 m_indices[id] = internal::convert_index<StorageIndex>(i);
88 }
89
90 inline Index size() const { return m_size; }
91 inline Index allocatedSize() const { return m_allocatedSize; }
92 inline void clear() { m_size = 0; }
93
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; }
98
99 inline Scalar& value(Index i) {
100 eigen_internal_assert(m_values != 0);
101 return m_values[i];
102 }
103 inline const Scalar& value(Index i) const {
104 eigen_internal_assert(m_values != 0);
105 return m_values[i];
106 }
107
108 inline StorageIndex& index(Index i) {
109 eigen_internal_assert(m_indices != 0);
110 return m_indices[i];
111 }
112 inline const StorageIndex& index(Index i) const {
113 eigen_internal_assert(m_indices != 0);
114 return m_indices[i];
115 }
116
118 inline Index searchLowerIndex(Index key) const { return searchLowerIndex(0, m_size, key); }
119
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)));
123 }
124
127 inline Scalar at(Index key, const Scalar& defaultValue = Scalar(0)) const {
128 if (m_size == 0)
129 return defaultValue;
130 else if (key == m_indices[m_size - 1])
131 return m_values[m_size - 1];
132 // ^^ optimization: let's first check if it is the last coefficient
133 // (very common in high level algorithms)
134 const Index id = searchLowerIndex(0, m_size - 1, key);
135 return ((id < m_size) && (m_indices[id] == key)) ? m_values[id] : defaultValue;
136 }
137
139 inline Scalar atInRange(Index start, Index end, Index key, const Scalar& defaultValue = Scalar(0)) const {
140 if (start >= end)
141 return defaultValue;
142 else if (end > start && key == m_indices[end - 1])
143 return m_values[end - 1];
144 // ^^ optimization: let's first check if it is the last coefficient
145 // (very common in high level algorithms)
146 const Index id = searchLowerIndex(start, end - 1, key);
147 return ((id < end) && (m_indices[id] == key)) ? m_values[id] : defaultValue;
148 }
149
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);
159 m_indices =
160 conditional_aligned_realloc_new_auto<StorageIndex, true>(m_indices, newAllocatedSize, m_allocatedSize);
161 m_allocatedSize = newAllocatedSize;
162 }
163 if (m_size > id) {
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);
166 }
167 m_size++;
168 m_indices[id] = internal::convert_index<StorageIndex>(key);
169 m_values[id] = defaultValue;
170 }
171 return m_values[id];
172 }
173
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);
178 }
179
180 protected:
181 inline void reallocate(Index size) {
182#ifdef EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
183 EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
184#endif
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;
189 }
190
191 protected:
192 Scalar* m_values = nullptr;
193 StorageIndex* m_indices = nullptr;
194 Index m_size = 0;
195 Index m_allocatedSize = 0;
196};
197
198} // end namespace internal
199
200} // end namespace Eigen
201
202#endif // EIGEN_COMPRESSED_STORAGE_H