10 #ifndef EIGEN_AMBIVECTOR_H 11 #define EIGEN_AMBIVECTOR_H 22 template<
typename _Scalar,
typename _StorageIndex>
26 typedef _Scalar Scalar;
27 typedef _StorageIndex StorageIndex;
31 : m_buffer(0), m_zero(0), m_size(0), m_allocatedSize(0), m_allocatedElements(0), m_mode(-1)
36 void init(
double estimatedDensity);
42 void setBounds(
Index start,
Index end) { m_start = convert_index(start); m_end = convert_index(end); }
47 Scalar& coeffRef(
Index i);
48 Scalar& coeff(
Index i);
54 void resize(
Index size)
56 if (m_allocatedSize < size)
58 m_size = convert_index(size);
61 StorageIndex size()
const {
return m_size; }
64 StorageIndex convert_index(
Index idx)
66 return internal::convert_index<StorageIndex>(idx);
69 void reallocate(
Index size)
76 Index allocSize = (size *
sizeof(
ListEl) +
sizeof(Scalar) - 1)/
sizeof(Scalar);
77 m_allocatedElements = convert_index((allocSize*
sizeof(Scalar))/
sizeof(
ListEl));
78 m_buffer =
new Scalar[allocSize];
82 m_allocatedElements = convert_index((size*
sizeof(Scalar))/
sizeof(
ListEl));
83 m_buffer =
new Scalar[size];
85 m_size = convert_index(size);
90 void reallocateSparse()
92 Index copyElements = m_allocatedElements;
93 m_allocatedElements = (std::min)(StorageIndex(m_allocatedElements*1.5),m_size);
94 Index allocSize = m_allocatedElements *
sizeof(
ListEl);
95 allocSize = (allocSize +
sizeof(Scalar) - 1)/
sizeof(Scalar);
96 Scalar* newBuffer =
new Scalar[allocSize];
97 memcpy(newBuffer, m_buffer, copyElements *
sizeof(
ListEl));
115 StorageIndex m_start;
117 StorageIndex m_allocatedSize;
118 StorageIndex m_allocatedElements;
122 StorageIndex m_llStart;
123 StorageIndex m_llCurrent;
124 StorageIndex m_llSize;
128 template<
typename _Scalar,
typename _StorageIndex>
131 if (m_mode==IsSparse)
134 return m_end - m_start;
137 template<
typename _Scalar,
typename _StorageIndex>
140 if (estimatedDensity>0.1)
146 template<
typename _Scalar,
typename _StorageIndex>
150 if (m_mode==IsSparse)
162 template<
typename _Scalar,
typename _StorageIndex>
165 m_llCurrent = m_llStart;
169 template<
typename _Scalar,
typename _StorageIndex>
174 for (
Index i=m_start; i<m_end; ++i)
175 m_buffer[i] = Scalar(0);
179 eigen_assert(m_mode==IsSparse);
185 template<
typename _Scalar,
typename _StorageIndex>
192 ListEl* EIGEN_RESTRICT llElements =
reinterpret_cast<ListEl*
>(m_buffer);
194 eigen_assert(m_mode==IsSparse);
201 llElements[0].value = Scalar(0);
202 llElements[0].index = convert_index(i);
203 llElements[0].next = -1;
204 return llElements[0].value;
206 else if (i<llElements[m_llStart].index)
209 ListEl& el = llElements[m_llSize];
210 el.value = Scalar(0);
211 el.index = convert_index(i);
213 m_llStart = m_llSize;
215 m_llCurrent = m_llStart;
220 StorageIndex nextel = llElements[m_llCurrent].next;
221 eigen_assert(i>=llElements[m_llCurrent].index &&
"you must call restart() before inserting an element with lower or equal index");
222 while (nextel >= 0 && llElements[nextel].index<=i)
224 m_llCurrent = nextel;
225 nextel = llElements[nextel].next;
228 if (llElements[m_llCurrent].index==i)
231 return llElements[m_llCurrent].value;
235 if (m_llSize>=m_allocatedElements)
238 llElements =
reinterpret_cast<ListEl*
>(m_buffer);
240 eigen_internal_assert(m_llSize<m_allocatedElements &&
"internal error: overflow in sparse mode");
242 ListEl& el = llElements[m_llSize];
243 el.value = Scalar(0);
244 el.index = convert_index(i);
245 el.next = llElements[m_llCurrent].next;
246 llElements[m_llCurrent].next = m_llSize;
254 template<
typename _Scalar,
typename _StorageIndex>
261 ListEl* EIGEN_RESTRICT llElements =
reinterpret_cast<ListEl*
>(m_buffer);
262 eigen_assert(m_mode==IsSparse);
263 if ((m_llSize==0) || (i<llElements[m_llStart].index))
269 Index elid = m_llStart;
270 while (elid >= 0 && llElements[elid].index<i)
271 elid = llElements[elid].next;
273 if (llElements[elid].index==i)
274 return llElements[m_llCurrent].value;
282 template<
typename _Scalar,
typename _StorageIndex>
286 typedef _Scalar Scalar;
300 m_isDense = m_vector.m_mode==IsDense;
305 m_cachedIndex = m_vector.m_start-1;
310 ListEl* EIGEN_RESTRICT llElements =
reinterpret_cast<ListEl*
>(m_vector.m_buffer);
311 m_currentEl = m_vector.m_llStart;
312 while (m_currentEl>=0 && abs(llElements[m_currentEl].value)<=m_epsilon)
313 m_currentEl = llElements[m_currentEl].next;
321 m_cachedIndex = llElements[m_currentEl].index;
322 m_cachedValue = llElements[m_currentEl].value;
327 StorageIndex index()
const {
return m_cachedIndex; }
328 Scalar value()
const {
return m_cachedValue; }
330 operator bool()
const {
return m_cachedIndex>=0; }
339 }
while (m_cachedIndex<m_vector.m_end && abs(m_vector.m_buffer[m_cachedIndex])<m_epsilon);
340 if (m_cachedIndex<m_vector.m_end)
341 m_cachedValue = m_vector.m_buffer[m_cachedIndex];
347 ListEl* EIGEN_RESTRICT llElements =
reinterpret_cast<ListEl*
>(m_vector.m_buffer);
349 m_currentEl = llElements[m_currentEl].next;
350 }
while (m_currentEl>=0 && abs(llElements[m_currentEl].value)<m_epsilon);
357 m_cachedIndex = llElements[m_currentEl].index;
358 m_cachedValue = llElements[m_currentEl].value;
366 StorageIndex m_currentEl;
367 RealScalar m_epsilon;
368 StorageIndex m_cachedIndex;
369 Scalar m_cachedValue;
377 #endif // EIGEN_AMBIVECTOR_H Iterator over the nonzero coefficients.
Definition: AmbiVector.h:283
Iterator(const AmbiVector &vec, const RealScalar &epsilon=0)
Default constructor.
Definition: AmbiVector.h:295
void restart()
Must be called whenever we might perform a write access with an index smaller than the previous one...
Definition: AmbiVector.h:163
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
void setBounds(Index start, Index end)
Specifies a sub-vector to work on.
Definition: AmbiVector.h:42
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Definition: AmbiVector.h:104
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
void setZero()
Set all coefficients of current subvector to zero.
Definition: AmbiVector.h:170
Definition: BandTriangularSolver.h:13
Definition: TutorialInplaceLU.cpp:2
Definition: AmbiVector.h:23
Index nonZeros() const
Definition: AmbiVector.h:129