10 #ifndef EIGEN_SPARSE_COMPRESSED_BASE_H
11 #define EIGEN_SPARSE_COMPRESSED_BASE_H
13 #include "./InternalHeaderCheck.h"
17 template<
typename Derived>
class SparseCompressedBase;
21 template<
typename Derived>
22 struct traits<SparseCompressedBase<Derived> > : traits<Derived>
37 template<
typename Derived>
44 using Base::operator=;
45 using Base::IsRowMajor;
48 class ReverseInnerIterator;
67 return innerNonZeros().sum();
137 internal::LowerBoundIndex lower_bound(
Index row,
Index col)
const
139 eigen_internal_assert(row>=0 && row<this->
rows() && col>=0 && col<this->
cols());
141 const Index outer = Derived::IsRowMajor ? row : col;
142 const Index inner = Derived::IsRowMajor ? col : row;
146 eigen_assert(
end>=start &&
"you are using a non finalized sparse matrix or written coefficient does not exist");
147 internal::LowerBoundIndex p;
156 template<
typename OtherDerived>
explicit SparseCompressedBase(
const SparseCompressedBase<OtherDerived>&);
159 template<
typename Derived>
160 class SparseCompressedBase<Derived>::InnerIterator
164 : m_values(0), m_indices(0), m_outer(0), m_id(0), m_end(0)
167 InnerIterator(
const InnerIterator& other)
168 : m_values(other.m_values), m_indices(other.m_indices), m_outer(other.m_outer), m_id(other.m_id), m_end(other.m_end)
171 InnerIterator& operator=(
const InnerIterator& other)
173 m_values = other.m_values;
174 m_indices = other.m_indices;
175 const_cast<OuterType&
>(m_outer).setValue(other.m_outer.value());
184 if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
187 m_end = mat.nonZeros();
191 m_id = mat.outerIndexPtr()[outer];
192 if(mat.isCompressed())
193 m_end = mat.outerIndexPtr()[outer+1];
195 m_end = m_id + mat.innerNonZeroPtr()[outer];
202 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
205 explicit InnerIterator(
const internal::CompressedStorage<Scalar,StorageIndex>& data)
206 : m_values(data.
valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_id(0), m_end(data.
size())
208 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
211 inline InnerIterator& operator++() { m_id++;
return *
this; }
212 inline InnerIterator& operator+=(
Index i) { m_id += i ;
return *
this; }
214 inline InnerIterator operator+(
Index i)
216 InnerIterator result = *
this;
221 inline const Scalar& value()
const {
return m_values[m_id]; }
222 inline Scalar& valueRef() {
return const_cast<Scalar&
>(m_values[m_id]); }
224 inline StorageIndex index()
const {
return m_indices[m_id]; }
225 inline Index outer()
const {
return m_outer.value(); }
226 inline Index row()
const {
return IsRowMajor ? m_outer.value() : index(); }
227 inline Index col()
const {
return IsRowMajor ? index() : m_outer.value(); }
229 inline operator bool()
const {
return (m_id < m_end); }
232 const Scalar* m_values;
234 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
235 const OuterType m_outer;
242 template<
typename T> InnerIterator(
const SparseMatrixBase<T>&,
Index outer);
245 template<
typename Derived>
246 class SparseCompressedBase<Derived>::ReverseInnerIterator
252 if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
255 m_id = mat.nonZeros();
259 m_start = mat.outerIndexPtr()[outer];
260 if(mat.isCompressed())
261 m_id = mat.outerIndexPtr()[outer+1];
263 m_id = m_start + mat.innerNonZeroPtr()[outer];
270 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
273 explicit ReverseInnerIterator(
const internal::CompressedStorage<Scalar,StorageIndex>& data)
274 : m_values(data.
valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_start(0), m_id(data.
size())
276 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
279 inline ReverseInnerIterator& operator--() { --m_id;
return *
this; }
280 inline ReverseInnerIterator& operator-=(
Index i) { m_id -= i;
return *
this; }
282 inline ReverseInnerIterator operator-(
Index i)
284 ReverseInnerIterator result = *
this;
289 inline const Scalar& value()
const {
return m_values[m_id-1]; }
290 inline Scalar& valueRef() {
return const_cast<Scalar&
>(m_values[m_id-1]); }
292 inline StorageIndex index()
const {
return m_indices[m_id-1]; }
293 inline Index outer()
const {
return m_outer.value(); }
294 inline Index row()
const {
return IsRowMajor ? m_outer.value() : index(); }
295 inline Index col()
const {
return IsRowMajor ? index() : m_outer.value(); }
297 inline operator bool()
const {
return (m_id > m_start); }
300 const Scalar* m_values;
302 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
303 const OuterType m_outer;
310 template<
typename Derived>
311 struct evaluator<SparseCompressedBase<Derived> >
312 : evaluator_base<Derived>
314 typedef typename Derived::Scalar Scalar;
315 typedef typename Derived::InnerIterator InnerIterator;
318 CoeffReadCost = NumTraits<Scalar>::ReadCost,
319 Flags = Derived::Flags
322 evaluator() : m_matrix(0), m_zero(0)
324 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
326 explicit evaluator(
const Derived &mat) : m_matrix(&mat), m_zero(0)
328 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
331 inline Index nonZerosEstimate()
const {
332 return m_matrix->nonZeros();
335 operator Derived&() {
return m_matrix->const_cast_derived(); }
336 operator const Derived&()
const {
return *m_matrix; }
338 typedef typename DenseCoeffsBase<Derived,ReadOnlyAccessors>::CoeffReturnType CoeffReturnType;
339 const Scalar& coeff(
Index row,
Index col)
const
341 Index p = find(row,col);
346 return m_matrix->const_cast_derived().valuePtr()[p];
351 Index p = find(row,col);
352 eigen_assert(p!=
Dynamic &&
"written coefficient does not exist");
353 return m_matrix->const_cast_derived().valuePtr()[p];
360 internal::LowerBoundIndex p = m_matrix->lower_bound(row,col);
361 return p.found ? p.value :
Dynamic;
364 const Derived *m_matrix;
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
static ConstMapType Map(const Scalar *data)
Definition: PlainObjectBase.h:642
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:40
Index nonZeros() const
Definition: SparseCompressedBase.h:58
Scalar * valuePtr()
Definition: SparseCompressedBase.h:77
const Scalar * valuePtr() const
Definition: SparseCompressedBase.h:73
const Map< const Array< Scalar, Dynamic, 1 > > coeffs() const
Definition: SparseCompressedBase.h:116
StorageIndex * innerIndexPtr()
Definition: SparseCompressedBase.h:86
const StorageIndex * outerIndexPtr() const
Definition: SparseCompressedBase.h:92
StorageIndex * innerNonZeroPtr()
Definition: SparseCompressedBase.h:106
StorageIndex * outerIndexPtr()
Definition: SparseCompressedBase.h:97
Map< Array< Scalar, Dynamic, 1 > > coeffs()
Definition: SparseCompressedBase.h:128
bool isCompressed() const
Definition: SparseCompressedBase.h:109
const StorageIndex * innerIndexPtr() const
Definition: SparseCompressedBase.h:82
SparseCompressedBase()
Definition: SparseCompressedBase.h:132
const StorageIndex * innerNonZeroPtr() const
Definition: SparseCompressedBase.h:102
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:45
Index size() const
Definition: SparseMatrixBase.h:181
Index rows() const
Definition: SparseMatrixBase.h:176
Index outerSize() const
Definition: SparseMatrixBase.h:189
Index cols() const
Definition: SparseMatrixBase.h:178
static const lastp1_t end
Definition: IndexedViewHelper.h:183
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Derived & derived()
Definition: EigenBase.h:48
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41