10 #ifndef EIGEN_COMPRESSED_STORAGE_H
11 #define EIGEN_COMPRESSED_STORAGE_H
21 template<
typename _Scalar,
typename _StorageIndex>
22 class CompressedStorage
26 typedef _Scalar Scalar;
27 typedef _StorageIndex StorageIndex;
31 typedef typename NumTraits<Scalar>::Real RealScalar;
36 : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
39 explicit CompressedStorage(Index size)
40 : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
45 CompressedStorage(
const CompressedStorage& other)
46 : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
51 CompressedStorage& operator=(
const CompressedStorage& other)
56 internal::smart_copy(other.m_values, other.m_values + m_size, m_values);
57 internal::smart_copy(other.m_indices, other.m_indices + m_size, m_indices);
62 void swap(CompressedStorage& other)
64 std::swap(m_values, other.m_values);
65 std::swap(m_indices, other.m_indices);
66 std::swap(m_size, other.m_size);
67 std::swap(m_allocatedSize, other.m_allocatedSize);
76 void reserve(Index size)
78 Index newAllocatedSize = m_size + size;
79 if (newAllocatedSize > m_allocatedSize)
80 reallocate(newAllocatedSize);
85 if (m_allocatedSize>m_size)
89 void resize(Index size,
double reserveSizeFactor = 0)
91 if (m_allocatedSize<size)
93 Index realloc_size = (std::min<Index>)(NumTraits<StorageIndex>::highest(), size + Index(reserveSizeFactor*
double(size)));
95 internal::throw_std_bad_alloc();
96 reallocate(realloc_size);
101 void append(
const Scalar& v, Index i)
106 m_indices[id] = internal::convert_index<StorageIndex>(i);
109 inline Index size()
const {
return m_size; }
110 inline Index allocatedSize()
const {
return m_allocatedSize; }
111 inline void clear() { m_size = 0; }
113 inline Scalar& value(Index i) {
return m_values[i]; }
114 inline const Scalar& value(Index i)
const {
return m_values[i]; }
116 inline StorageIndex& index(Index i) {
return m_indices[i]; }
117 inline const StorageIndex& index(Index i)
const {
return m_indices[i]; }
120 inline Index searchLowerIndex(Index key)
const
122 return searchLowerIndex(0, m_size, key);
126 inline Index searchLowerIndex(Index start, Index end, Index key)
const
130 Index mid = (end+start)>>1;
131 if (m_indices[mid]<key)
141 inline Scalar at(Index key,
const Scalar& defaultValue = Scalar(0))
const
145 else if (key==m_indices[m_size-1])
146 return m_values[m_size-1];
149 const Index
id = searchLowerIndex(0,m_size-1,key);
150 return ((
id<m_size) && (m_indices[
id]==key)) ? m_values[id] : defaultValue;
154 inline Scalar atInRange(Index start, Index end, Index key,
const Scalar &defaultValue = Scalar(0))
const
158 else if (end>start && key==m_indices[end-1])
159 return m_values[end-1];
162 const Index
id = searchLowerIndex(start,end-1,key);
163 return ((
id<end) && (m_indices[
id]==key)) ? m_values[id] : defaultValue;
169 inline Scalar& atWithInsertion(Index key,
const Scalar& defaultValue = Scalar(0))
171 Index
id = searchLowerIndex(0,m_size,key);
172 if (
id>=m_size || m_indices[
id]!=key)
174 if (m_allocatedSize<m_size+1)
176 m_allocatedSize = 2*(m_size+1);
177 internal::scoped_array<Scalar> newValues(m_allocatedSize);
178 internal::scoped_array<StorageIndex> newIndices(m_allocatedSize);
181 internal::smart_copy(m_values, m_values +
id, newValues.ptr());
182 internal::smart_copy(m_indices, m_indices+
id, newIndices.ptr());
187 internal::smart_copy(m_values +
id, m_values +m_size, newValues.ptr() +
id+1);
188 internal::smart_copy(m_indices+
id, m_indices+m_size, newIndices.ptr()+
id+1);
190 std::swap(m_values,newValues.ptr());
191 std::swap(m_indices,newIndices.ptr());
195 internal::smart_memmove(m_values +
id, m_values +m_size, m_values +
id+1);
196 internal::smart_memmove(m_indices+
id, m_indices+m_size, m_indices+
id+1);
199 m_indices[id] = internal::convert_index<StorageIndex>(key);
200 m_values[id] = defaultValue;
205 void prune(
const Scalar& reference,
const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision())
209 for (Index i=0; i<n; ++i)
211 if (!internal::isMuchSmallerThan(value(i), reference, epsilon))
223 inline void reallocate(Index size)
225 #ifdef EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
226 EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
228 eigen_internal_assert(size!=m_allocatedSize);
229 internal::scoped_array<Scalar> newValues(size);
230 internal::scoped_array<StorageIndex> newIndices(size);
231 Index copySize = (std::min)(size, m_size);
233 internal::smart_copy(m_values, m_values+copySize, newValues.ptr());
234 internal::smart_copy(m_indices, m_indices+copySize, newIndices.ptr());
236 std::swap(m_values,newValues.ptr());
237 std::swap(m_indices,newIndices.ptr());
238 m_allocatedSize = size;
243 StorageIndex* m_indices;
245 Index m_allocatedSize;
253 #endif // EIGEN_COMPRESSED_STORAGE_H
Definition: Eigen_Colamd.h:54