Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SparseMap.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2015 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_SPARSE_MAP_H
12#define EIGEN_SPARSE_MAP_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
22struct traits<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
23 : public traits<SparseMatrix<MatScalar, MatOptions, MatIndex> > {
24 using PlainObjectType = SparseMatrix<MatScalar, MatOptions, MatIndex>;
25 using TraitsBase = traits<PlainObjectType>;
26 enum { Flags = TraitsBase::Flags & (~NestByRefBit) };
27};
28
29template <typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
30struct traits<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
31 : public traits<SparseMatrix<MatScalar, MatOptions, MatIndex> > {
32 using PlainObjectType = SparseMatrix<MatScalar, MatOptions, MatIndex>;
33 using TraitsBase = traits<PlainObjectType>;
34 enum { Flags = TraitsBase::Flags & (~(NestByRefBit | LvalueBit)) };
35};
36
37} // end namespace internal
38
39template <typename Derived,
40 int Level = internal::accessors_level<Derived>::has_write_access ? WriteAccessors : ReadOnlyAccessors>
41class SparseMapBase;
42
47template <typename Derived>
48class SparseMapBase<Derived, ReadOnlyAccessors> : public SparseCompressedBase<Derived> {
49 public:
50 using Base = SparseCompressedBase<Derived>;
51 using Scalar = typename Base::Scalar;
52 using StorageIndex = typename Base::StorageIndex;
53 enum { IsRowMajor = Base::IsRowMajor };
54 using Base::operator=;
55
56 protected:
57 using ScalarPointer = std::conditional_t<bool(internal::is_lvalue<Derived>::value), Scalar*, const Scalar*>;
58 using IndexPointer =
59 std::conditional_t<bool(internal::is_lvalue<Derived>::value), StorageIndex*, const StorageIndex*>;
60
61 Index m_outerSize;
62 Index m_innerSize;
63 Array<StorageIndex, 2, 1> m_zero_nnz;
64 IndexPointer m_outerIndex;
65 IndexPointer m_innerIndices;
66 ScalarPointer m_values;
67 IndexPointer m_innerNonZeros;
68
69 public:
71 inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
73 inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
75 inline Index innerSize() const { return m_innerSize; }
77 inline Index outerSize() const { return m_outerSize; }
79 inline Index nonZeros() const { return m_zero_nnz[1]; }
80
82 bool isCompressed() const { return m_innerNonZeros == 0; }
83
84 //----------------------------------------
85 // direct access interface
87 inline const Scalar* valuePtr() const { return m_values; }
89 inline const StorageIndex* innerIndexPtr() const { return m_innerIndices; }
91 inline const StorageIndex* outerIndexPtr() const { return m_outerIndex; }
93 inline const StorageIndex* innerNonZeroPtr() const { return m_innerNonZeros; }
94 //----------------------------------------
95
97 inline Scalar coeff(Index row, Index col) const {
98 const Index outer = IsRowMajor ? row : col;
99 const Index inner = IsRowMajor ? col : row;
100
101 Index start = m_outerIndex[outer];
102 Index end = isCompressed() ? m_outerIndex[outer + 1] : start + m_innerNonZeros[outer];
103 if (start == end)
104 return Scalar(0);
105 else if (end > 0 && inner == m_innerIndices[end - 1])
106 return m_values[end - 1];
107 // ^^ optimization: let's first check if it is the last coefficient
108 // (very common in high level algorithms)
109
110 const StorageIndex* r = std::lower_bound(&m_innerIndices[start], &m_innerIndices[end - 1], inner);
111 const Index id = r - &m_innerIndices[0];
112 return ((*r == inner) && (id < end)) ? m_values[id] : Scalar(0);
113 }
114
115 inline SparseMapBase(Index rows, Index cols, Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
116 ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
117 : m_outerSize(IsRowMajor ? rows : cols),
118 m_innerSize(IsRowMajor ? cols : rows),
119 m_zero_nnz(0, internal::convert_index<StorageIndex>(nnz)),
120 m_outerIndex(outerIndexPtr),
121 m_innerIndices(innerIndexPtr),
122 m_values(valuePtr),
123 m_innerNonZeros(innerNonZerosPtr) {}
124
125 // for vectors
126 inline SparseMapBase(Index size, Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
127 : m_outerSize(1),
128 m_innerSize(size),
129 m_zero_nnz(0, internal::convert_index<StorageIndex>(nnz)),
130 m_outerIndex(m_zero_nnz.data()),
131 m_innerIndices(innerIndexPtr),
132 m_values(valuePtr),
133 m_innerNonZeros(0) {}
134
135 // A vector map points m_outerIndex at its own m_zero_nnz, which a copy must not share.
136 inline SparseMapBase(const SparseMapBase& other)
137 : Base(other),
138 m_outerSize(other.m_outerSize),
139 m_innerSize(other.m_innerSize),
140 m_zero_nnz(other.m_zero_nnz),
141 m_outerIndex(other.m_outerIndex == other.m_zero_nnz.data() ? m_zero_nnz.data() : other.m_outerIndex),
142 m_innerIndices(other.m_innerIndices),
143 m_values(other.m_values),
144 m_innerNonZeros(other.m_innerNonZeros) {}
145
146 // Rebinds like the implicit copy assignment did, with the same m_zero_nnz redirect.
147 inline SparseMapBase& operator=(const SparseMapBase& other) {
148 m_outerSize = other.m_outerSize;
149 m_innerSize = other.m_innerSize;
150 m_zero_nnz = other.m_zero_nnz;
151 m_outerIndex = other.m_outerIndex == other.m_zero_nnz.data() ? m_zero_nnz.data() : other.m_outerIndex;
152 m_innerIndices = other.m_innerIndices;
153 m_values = other.m_values;
154 m_innerNonZeros = other.m_innerNonZeros;
155 return *this;
156 }
157
158 protected:
159 inline SparseMapBase() = default;
160};
161
166template <typename Derived>
167class SparseMapBase<Derived, WriteAccessors> : public SparseMapBase<Derived, ReadOnlyAccessors> {
168 public:
169 using Base = SparseMapBase<Derived, ReadOnlyAccessors>;
170 using Scalar = typename Base::Scalar;
171 using StorageIndex = typename Base::StorageIndex;
172 enum { IsRowMajor = Base::IsRowMajor };
173
174 using Base::operator=;
175 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SparseMapBase)
176 SparseMapBase& operator=(const SparseMapBase&) = default;
177
178 public:
179 //----------------------------------------
180 // direct access interface
181 using Base::innerIndexPtr;
182 using Base::innerNonZeroPtr;
183 using Base::outerIndexPtr;
184 using Base::valuePtr;
186 inline Scalar* valuePtr() { return Base::m_values; }
188 inline StorageIndex* innerIndexPtr() { return Base::m_innerIndices; }
190 inline StorageIndex* outerIndexPtr() { return Base::m_outerIndex; }
192 inline StorageIndex* innerNonZeroPtr() { return Base::m_innerNonZeros; }
193 //----------------------------------------
194
202 inline Scalar& coeffRef(Index row, Index col) {
203 const Index outer = IsRowMajor ? row : col;
204 const Index inner = IsRowMajor ? col : row;
205
206 Index start = Base::m_outerIndex[outer];
207 Index end = Base::isCompressed() ? Base::m_outerIndex[outer + 1] : start + Base::m_innerNonZeros[outer];
208 eigen_assert(end >= start && "you probably called coeffRef on a non finalized matrix");
209 eigen_assert(end > start && "coeffRef cannot be called on a zero coefficient");
210 StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start], &Base::m_innerIndices[end], inner);
211 const Index id = r - &Base::m_innerIndices[0];
212 eigen_assert((*r == inner) && (id < end) && "coeffRef cannot be called on a zero coefficient");
213 return const_cast<Scalar*>(Base::m_values)[id];
214 }
215
216 inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
217 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
218 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr) {}
219
220 // for vectors
221 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
222 : Base(size, nnz, innerIndexPtr, valuePtr) {}
223
224 protected:
225 inline SparseMapBase() = default;
226};
227
237#ifndef EIGEN_PARSED_BY_DOXYGEN
238template <typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
239class Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
240 : public SparseMapBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
241#else
242template <typename SparseMatrixType>
243class Map<SparseMatrixType> : public SparseMapBase<Derived, WriteAccessors>
244#endif
245{
246 public:
247 using Base = SparseMapBase<Map>;
248 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
249 enum { IsRowMajor = Base::IsRowMajor };
250
251 public:
261 inline Map(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
262 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
263 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr) {}
264#ifndef EIGEN_PARSED_BY_DOXYGEN
265};
266
267template <typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
268class Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
269 : public SparseMapBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > {
270 public:
271 using Base = SparseMapBase<Map>;
272 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
273 enum { IsRowMajor = Base::IsRowMajor };
274
275 public:
276#endif
282 inline Map(Index rows, Index cols, Index nnz, const StorageIndex* outerIndexPtr, const StorageIndex* innerIndexPtr,
283 const Scalar* valuePtr, const StorageIndex* innerNonZerosPtr = 0)
284 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr) {}
285};
286
287namespace internal {
288
289template <typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
290struct evaluator<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
291 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
292 using Base = evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
293 using XprType = Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
294 evaluator() = default;
295 explicit evaluator(const XprType& mat) : Base(mat) {}
296};
297
298template <typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
299struct evaluator<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
300 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
301 using Base =
302 evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
303 using XprType = Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
304 evaluator() = default;
305 explicit evaluator(const XprType& mat) : Base(mat) {}
306};
307
308} // namespace internal
309
310} // end namespace Eigen
311
312#endif // EIGEN_SPARSE_MAP_H
Map(Index rows, Index cols, Index nnz, const StorageIndex *outerIndexPtr, const StorageIndex *innerIndexPtr, const Scalar *valuePtr, const StorageIndex *innerNonZerosPtr=0)
Definition SparseMap.h:282
Map(Index rows, Index cols, Index nnz, StorageIndex *outerIndexPtr, StorageIndex *innerIndexPtr, Scalar *valuePtr, StorageIndex *innerNonZerosPtr=0)
Definition SparseMap.h:261
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
constexpr Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition Map.h:124
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:44
Scalar & coeffRef(Index row, Index col)
Definition SparseMap.h:202
Scalar * valuePtr()
Definition SparseMap.h:186
StorageIndex * innerNonZeroPtr()
Definition SparseMap.h:192
StorageIndex * outerIndexPtr()
Definition SparseMap.h:190
StorageIndex * innerIndexPtr()
Definition SparseMap.h:188
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
@ ReadOnlyAccessors
Definition Constants.h:377
@ WriteAccessors
Definition Constants.h:379
constexpr unsigned int LvalueBit
Definition Constants.h:149