File indexing completed on 2025-02-22 10:34:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_SPARSE_MAP_H
0011 #define EIGEN_SPARSE_MAP_H
0012
0013 namespace Eigen {
0014
0015 namespace internal {
0016
0017 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0018 struct traits<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0019 : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
0020 {
0021 typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
0022 typedef traits<PlainObjectType> TraitsBase;
0023 enum {
0024 Flags = TraitsBase::Flags & (~NestByRefBit)
0025 };
0026 };
0027
0028 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0029 struct traits<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0030 : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
0031 {
0032 typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
0033 typedef traits<PlainObjectType> TraitsBase;
0034 enum {
0035 Flags = TraitsBase::Flags & (~ (NestByRefBit | LvalueBit))
0036 };
0037 };
0038
0039 }
0040
0041 template<typename Derived,
0042 int Level = internal::accessors_level<Derived>::has_write_access ? WriteAccessors : ReadOnlyAccessors
0043 > class SparseMapBase;
0044
0045
0046
0047
0048
0049 template<typename Derived>
0050 class SparseMapBase<Derived,ReadOnlyAccessors>
0051 : public SparseCompressedBase<Derived>
0052 {
0053 public:
0054 typedef SparseCompressedBase<Derived> Base;
0055 typedef typename Base::Scalar Scalar;
0056 typedef typename Base::StorageIndex StorageIndex;
0057 enum { IsRowMajor = Base::IsRowMajor };
0058 using Base::operator=;
0059 protected:
0060
0061 typedef typename internal::conditional<
0062 bool(internal::is_lvalue<Derived>::value),
0063 Scalar *, const Scalar *>::type ScalarPointer;
0064 typedef typename internal::conditional<
0065 bool(internal::is_lvalue<Derived>::value),
0066 StorageIndex *, const StorageIndex *>::type IndexPointer;
0067
0068 Index m_outerSize;
0069 Index m_innerSize;
0070 Array<StorageIndex,2,1> m_zero_nnz;
0071 IndexPointer m_outerIndex;
0072 IndexPointer m_innerIndices;
0073 ScalarPointer m_values;
0074 IndexPointer m_innerNonZeros;
0075
0076 public:
0077
0078
0079 inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
0080
0081 inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
0082
0083 inline Index innerSize() const { return m_innerSize; }
0084
0085 inline Index outerSize() const { return m_outerSize; }
0086
0087 inline Index nonZeros() const { return m_zero_nnz[1]; }
0088
0089
0090 bool isCompressed() const { return m_innerNonZeros==0; }
0091
0092
0093
0094
0095 inline const Scalar* valuePtr() const { return m_values; }
0096
0097 inline const StorageIndex* innerIndexPtr() const { return m_innerIndices; }
0098
0099 inline const StorageIndex* outerIndexPtr() const { return m_outerIndex; }
0100
0101 inline const StorageIndex* innerNonZeroPtr() const { return m_innerNonZeros; }
0102
0103
0104
0105 inline Scalar coeff(Index row, Index col) const
0106 {
0107 const Index outer = IsRowMajor ? row : col;
0108 const Index inner = IsRowMajor ? col : row;
0109
0110 Index start = m_outerIndex[outer];
0111 Index end = isCompressed() ? m_outerIndex[outer+1] : start + m_innerNonZeros[outer];
0112 if (start==end)
0113 return Scalar(0);
0114 else if (end>0 && inner==m_innerIndices[end-1])
0115 return m_values[end-1];
0116
0117
0118
0119 const StorageIndex* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
0120 const Index id = r-&m_innerIndices[0];
0121 return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0);
0122 }
0123
0124 inline SparseMapBase(Index rows, Index cols, Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
0125 ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
0126 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_zero_nnz(0,internal::convert_index<StorageIndex>(nnz)), m_outerIndex(outerIndexPtr),
0127 m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(innerNonZerosPtr)
0128 {}
0129
0130
0131 inline SparseMapBase(Index size, Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
0132 : m_outerSize(1), m_innerSize(size), m_zero_nnz(0,internal::convert_index<StorageIndex>(nnz)), m_outerIndex(m_zero_nnz.data()),
0133 m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(0)
0134 {}
0135
0136
0137 inline ~SparseMapBase() {}
0138
0139 protected:
0140 inline SparseMapBase() {}
0141 };
0142
0143
0144
0145
0146
0147 template<typename Derived>
0148 class SparseMapBase<Derived,WriteAccessors>
0149 : public SparseMapBase<Derived,ReadOnlyAccessors>
0150 {
0151 typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
0152
0153 public:
0154 typedef SparseMapBase<Derived, ReadOnlyAccessors> Base;
0155 typedef typename Base::Scalar Scalar;
0156 typedef typename Base::StorageIndex StorageIndex;
0157 enum { IsRowMajor = Base::IsRowMajor };
0158
0159 using Base::operator=;
0160
0161 public:
0162
0163
0164
0165 using Base::valuePtr;
0166 using Base::innerIndexPtr;
0167 using Base::outerIndexPtr;
0168 using Base::innerNonZeroPtr;
0169
0170 inline Scalar* valuePtr() { return Base::m_values; }
0171
0172 inline StorageIndex* innerIndexPtr() { return Base::m_innerIndices; }
0173
0174 inline StorageIndex* outerIndexPtr() { return Base::m_outerIndex; }
0175
0176 inline StorageIndex* innerNonZeroPtr() { return Base::m_innerNonZeros; }
0177
0178
0179
0180 inline Scalar& coeffRef(Index row, Index col)
0181 {
0182 const Index outer = IsRowMajor ? row : col;
0183 const Index inner = IsRowMajor ? col : row;
0184
0185 Index start = Base::m_outerIndex[outer];
0186 Index end = Base::isCompressed() ? Base::m_outerIndex[outer+1] : start + Base::m_innerNonZeros[outer];
0187 eigen_assert(end>=start && "you probably called coeffRef on a non finalized matrix");
0188 eigen_assert(end>start && "coeffRef cannot be called on a zero coefficient");
0189 StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start],&Base::m_innerIndices[end],inner);
0190 const Index id = r - &Base::m_innerIndices[0];
0191 eigen_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient");
0192 return const_cast<Scalar*>(Base::m_values)[id];
0193 }
0194
0195 inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
0196 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
0197 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
0198 {}
0199
0200
0201 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
0202 : Base(size, nnz, innerIndexPtr, valuePtr)
0203 {}
0204
0205
0206 inline ~SparseMapBase() {}
0207
0208 protected:
0209 inline SparseMapBase() {}
0210 };
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 #ifndef EIGEN_PARSED_BY_DOXYGEN
0221 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0222 class Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
0223 : public SparseMapBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0224 #else
0225 template<typename SparseMatrixType>
0226 class Map<SparseMatrixType>
0227 : public SparseMapBase<Derived,WriteAccessors>
0228 #endif
0229 {
0230 public:
0231 typedef SparseMapBase<Map> Base;
0232 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
0233 enum { IsRowMajor = Base::IsRowMajor };
0234
0235 public:
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 inline Map(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr,
0246 StorageIndex* innerIndexPtr, Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
0247 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
0248 {}
0249 #ifndef EIGEN_PARSED_BY_DOXYGEN
0250
0251 inline ~Map() {}
0252 };
0253
0254 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0255 class Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
0256 : public SparseMapBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0257 {
0258 public:
0259 typedef SparseMapBase<Map> Base;
0260 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
0261 enum { IsRowMajor = Base::IsRowMajor };
0262
0263 public:
0264 #endif
0265
0266
0267
0268
0269
0270 inline Map(Index rows, Index cols, Index nnz, const StorageIndex* outerIndexPtr,
0271 const StorageIndex* innerIndexPtr, const Scalar* valuePtr, const StorageIndex* innerNonZerosPtr = 0)
0272 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
0273 {}
0274
0275
0276 inline ~Map() {}
0277 };
0278
0279 namespace internal {
0280
0281 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0282 struct evaluator<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0283 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
0284 {
0285 typedef evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
0286 typedef Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
0287 evaluator() : Base() {}
0288 explicit evaluator(const XprType &mat) : Base(mat) {}
0289 };
0290
0291 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
0292 struct evaluator<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
0293 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
0294 {
0295 typedef evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
0296 typedef Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
0297 evaluator() : Base() {}
0298 explicit evaluator(const XprType &mat) : Base(mat) {}
0299 };
0300
0301 }
0302
0303 }
0304
0305 #endif