CompressedStorage.h
Go to the documentation of this file.
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 
10 #ifndef EIGEN_COMPRESSED_STORAGE_H
11 #define EIGEN_COMPRESSED_STORAGE_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
21 template<typename _Scalar,typename _StorageIndex>
23 {
24  public:
25 
26  typedef _Scalar Scalar;
27  typedef _StorageIndex StorageIndex;
28 
29  protected:
30 
32 
33  public:
34 
36  : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
37  {}
38 
40  : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
41  {
42  resize(size);
43  }
44 
46  : m_values(0), m_indices(0), m_size(0), m_allocatedSize(0)
47  {
48  *this = other;
49  }
50 
52  {
53  resize(other.size());
54  if(other.size()>0)
55  {
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);
58  }
59  return *this;
60  }
61 
63  {
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);
68  }
69 
71  {
72  delete[] m_values;
73  delete[] m_indices;
74  }
75 
77  {
78  Index newAllocatedSize = m_size + size;
79  if (newAllocatedSize > m_allocatedSize)
80  reallocate(newAllocatedSize);
81  }
82 
83  void squeeze()
84  {
87  }
88 
89  void resize(Index size, double reserveSizeFactor = 0)
90  {
92  {
93  Index realloc_size = (std::min<Index>)(NumTraits<StorageIndex>::highest(), size + Index(reserveSizeFactor*double(size)));
94  if(realloc_size<size)
96  reallocate(realloc_size);
97  }
98  m_size = size;
99  }
100 
101  void append(const Scalar& v, Index i)
102  {
103  Index id = m_size;
104  resize(m_size+1, 1);
105  m_values[id] = v;
106  m_indices[id] = internal::convert_index<StorageIndex>(i);
107  }
108 
109  inline Index size() const { return m_size; }
110  inline Index allocatedSize() const { return m_allocatedSize; }
111  inline void clear() { m_size = 0; }
112 
113  const Scalar* valuePtr() const { return m_values; }
114  Scalar* valuePtr() { return m_values; }
115  const StorageIndex* indexPtr() const { return m_indices; }
117 
119  inline const Scalar& value(Index i) const { eigen_internal_assert(m_values!=0); return m_values[i]; }
120 
122  inline const StorageIndex& index(Index i) const { eigen_internal_assert(m_indices!=0); return m_indices[i]; }
123 
126  {
127  return searchLowerIndex(0, m_size, key);
128  }
129 
131  inline Index searchLowerIndex(Index start, Index end, Index key) const
132  {
133  while(end>start)
134  {
135  Index mid = (end+start)>>1;
136  if (m_indices[mid]<key)
137  start = mid+1;
138  else
139  end = mid;
140  }
141  return start;
142  }
143 
146  inline Scalar at(Index key, const Scalar& defaultValue = Scalar(0)) const
147  {
148  if (m_size==0)
149  return defaultValue;
150  else if (key==m_indices[m_size-1])
151  return m_values[m_size-1];
152  // ^^ optimization: let's first check if it is the last coefficient
153  // (very common in high level algorithms)
154  const Index id = searchLowerIndex(0,m_size-1,key);
155  return ((id<m_size) && (m_indices[id]==key)) ? m_values[id] : defaultValue;
156  }
157 
159  inline Scalar atInRange(Index start, Index end, Index key, const Scalar &defaultValue = Scalar(0)) const
160  {
161  if (start>=end)
162  return defaultValue;
163  else if (end>start && key==m_indices[end-1])
164  return m_values[end-1];
165  // ^^ optimization: let's first check if it is the last coefficient
166  // (very common in high level algorithms)
167  const Index id = searchLowerIndex(start,end-1,key);
168  return ((id<end) && (m_indices[id]==key)) ? m_values[id] : defaultValue;
169  }
170 
174  inline Scalar& atWithInsertion(Index key, const Scalar& defaultValue = Scalar(0))
175  {
177  if (id>=m_size || m_indices[id]!=key)
178  {
179  if (m_allocatedSize<m_size+1)
180  {
181  m_allocatedSize = 2*(m_size+1);
184 
185  // copy first chunk
186  internal::smart_copy(m_values, m_values +id, newValues.ptr());
187  internal::smart_copy(m_indices, m_indices+id, newIndices.ptr());
188 
189  // copy the rest
190  if(m_size>id)
191  {
192  internal::smart_copy(m_values +id, m_values +m_size, newValues.ptr() +id+1);
193  internal::smart_copy(m_indices+id, m_indices+m_size, newIndices.ptr()+id+1);
194  }
195  std::swap(m_values,newValues.ptr());
196  std::swap(m_indices,newIndices.ptr());
197  }
198  else if(m_size>id)
199  {
202  }
203  m_size++;
204  m_indices[id] = internal::convert_index<StorageIndex>(key);
205  m_values[id] = defaultValue;
206  }
207  return m_values[id];
208  }
209 
210  void moveChunk(Index from, Index to, Index chunkSize)
211  {
212  eigen_internal_assert(to+chunkSize <= m_size);
213  if(to>from && from+chunkSize>to)
214  {
215  // move backward
216  internal::smart_memmove(m_values+from, m_values+from+chunkSize, m_values+to);
217  internal::smart_memmove(m_indices+from, m_indices+from+chunkSize, m_indices+to);
218  }
219  else
220  {
221  internal::smart_copy(m_values+from, m_values+from+chunkSize, m_values+to);
222  internal::smart_copy(m_indices+from, m_indices+from+chunkSize, m_indices+to);
223  }
224  }
225 
227  {
228  Index k = 0;
229  Index n = size();
230  for (Index i=0; i<n; ++i)
231  {
233  {
234  value(k) = value(i);
235  index(k) = index(i);
236  ++k;
237  }
238  }
239  resize(k,0);
240  }
241 
242  protected:
243 
244  inline void reallocate(Index size)
245  {
246  #ifdef EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
248  #endif
252  Index copySize = (std::min)(size, m_size);
253  if (copySize>0) {
254  internal::smart_copy(m_values, m_values+copySize, newValues.ptr());
255  internal::smart_copy(m_indices, m_indices+copySize, newIndices.ptr());
256  }
257  std::swap(m_values,newValues.ptr());
258  std::swap(m_indices,newIndices.ptr());
260  }
261 
262  protected:
267 
268 };
269 
270 } // end namespace internal
271 
272 } // end namespace Eigen
273 
274 #endif // EIGEN_COMPRESSED_STORAGE_H
Eigen::internal::CompressedStorage::reserve
void reserve(Index size)
Definition: CompressedStorage.h:76
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::internal::CompressedStorage::searchLowerIndex
Index searchLowerIndex(Index start, Index end, Index key) const
Definition: CompressedStorage.h:131
Eigen::internal::scoped_array::ptr
T *& ptr()
Definition: Memory.h:704
Eigen::internal::CompressedStorage::append
void append(const Scalar &v, Index i)
Definition: CompressedStorage.h:101
Eigen::internal::CompressedStorage::index
StorageIndex & index(Index i)
Definition: CompressedStorage.h:121
Eigen::internal::CompressedStorage::swap
void swap(CompressedStorage &other)
Definition: CompressedStorage.h:62
Eigen::internal::CompressedStorage::moveChunk
void moveChunk(Index from, Index to, Index chunkSize)
Definition: CompressedStorage.h:210
Eigen::internal::CompressedStorage::m_allocatedSize
Index m_allocatedSize
Definition: CompressedStorage.h:266
Eigen::internal::CompressedStorage::allocatedSize
Index allocatedSize() const
Definition: CompressedStorage.h:110
Eigen::internal::CompressedStorage::valuePtr
Scalar * valuePtr()
Definition: CompressedStorage.h:114
Eigen::internal::isMuchSmallerThan
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1940
eigen_internal_assert
#define eigen_internal_assert(x)
Definition: Macros.h:1043
Eigen::internal::CompressedStorage::value
const Scalar & value(Index i) const
Definition: CompressedStorage.h:119
Eigen::internal::CompressedStorage::index
const StorageIndex & index(Index i) const
Definition: CompressedStorage.h:122
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::internal::CompressedStorage::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: CompressedStorage.h:31
epsilon
static double epsilon
Definition: testRot3.cpp:37
Eigen::internal::CompressedStorage::~CompressedStorage
~CompressedStorage()
Definition: CompressedStorage.h:70
Eigen::internal::CompressedStorage::searchLowerIndex
Index searchLowerIndex(Index key) const
Definition: CompressedStorage.h:125
id
static const Similarity3 id
Definition: testSimilarity3.cpp:44
Eigen::internal::CompressedStorage::size
Index size() const
Definition: CompressedStorage.h:109
Eigen::internal::throw_std_bad_alloc
EIGEN_DEVICE_FUNC void throw_std_bad_alloc()
Definition: Memory.h:67
Eigen::internal::CompressedStorage::clear
void clear()
Definition: CompressedStorage.h:111
return_value_policy::reference
@ reference
Eigen::internal::CompressedStorage::indexPtr
const StorageIndex * indexPtr() const
Definition: CompressedStorage.h:115
std::swap
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
Definition: NearestNeighbor.hpp:827
Eigen::internal::CompressedStorage::atWithInsertion
Scalar & atWithInsertion(Index key, const Scalar &defaultValue=Scalar(0))
Definition: CompressedStorage.h:174
Eigen::internal::CompressedStorage::m_indices
StorageIndex * m_indices
Definition: CompressedStorage.h:264
Eigen::internal::smart_memmove
void smart_memmove(const T *start, const T *end, T *target)
Definition: Memory.h:539
Eigen::internal::CompressedStorage::at
Scalar at(Index key, const Scalar &defaultValue=Scalar(0)) const
Definition: CompressedStorage.h:146
Eigen::internal::CompressedStorage::Scalar
_Scalar Scalar
Definition: CompressedStorage.h:26
Eigen::internal::CompressedStorage::m_size
Index m_size
Definition: CompressedStorage.h:265
Eigen::internal::CompressedStorage::CompressedStorage
CompressedStorage(const CompressedStorage &other)
Definition: CompressedStorage.h:45
Eigen::internal::CompressedStorage::value
Scalar & value(Index i)
Definition: CompressedStorage.h:118
EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
#define EIGEN_SPARSE_COMPRESSED_STORAGE_REALLOCATE_PLUGIN
Definition: sparse_basic.cpp:14
key
const gtsam::Symbol key('X', 0)
Eigen::internal::CompressedStorage::resize
void resize(Index size, double reserveSizeFactor=0)
Definition: CompressedStorage.h:89
Eigen::internal::CompressedStorage::valuePtr
const Scalar * valuePtr() const
Definition: CompressedStorage.h:113
Eigen::internal::CompressedStorage::atInRange
Scalar atInRange(Index start, Index end, Index key, const Scalar &defaultValue=Scalar(0)) const
Definition: CompressedStorage.h:159
Eigen::internal::CompressedStorage::CompressedStorage
CompressedStorage()
Definition: CompressedStorage.h:35
Eigen::internal::CompressedStorage::prune
void prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Definition: CompressedStorage.h:226
Eigen::internal::scoped_array
Definition: Memory.h:690
Eigen::internal::CompressedStorage::StorageIndex
_StorageIndex StorageIndex
Definition: CompressedStorage.h:27
Eigen::internal::CompressedStorage::CompressedStorage
CompressedStorage(Index size)
Definition: CompressedStorage.h:39
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::internal::CompressedStorage
Definition: CompressedStorage.h:22
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::internal::CompressedStorage::m_values
Scalar * m_values
Definition: CompressedStorage.h:263
Eigen::internal::CompressedStorage::reallocate
void reallocate(Index size)
Definition: CompressedStorage.h:244
internal
Definition: BandTriangularSolver.h:13
Eigen::internal::smart_copy
EIGEN_DEVICE_FUNC void smart_copy(const T *start, const T *end, T *target)
Definition: Memory.h:515
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
Eigen::internal::CompressedStorage::indexPtr
StorageIndex * indexPtr()
Definition: CompressedStorage.h:116
Eigen::internal::CompressedStorage::operator=
CompressedStorage & operator=(const CompressedStorage &other)
Definition: CompressedStorage.h:51
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::internal::CompressedStorage::squeeze
void squeeze()
Definition: CompressedStorage.h:83
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:00:37