Move evaluator<SparseCompressedBase>::find(i,j) to a more general and reusable SparseCompressedBase::lower_bound(i,j) functiion

This commit is contained in:
Gael Guennebaud 2019-01-28 17:24:44 +01:00
parent 53560f9186
commit 803fa79767
2 changed files with 32 additions and 11 deletions

View File

@ -128,6 +128,28 @@ class SparseCompressedBase
protected:
/** Default constructor. Do nothing. */
SparseCompressedBase() {}
/** \internal return the index of the coeff at (row,col) or just before if it does not exist.
* This is an analogue of std::lower_bound.
*/
internal::LowerBoundIndex lower_bound(Index row, Index col) const
{
eigen_internal_assert(row>=0 && row<this->rows() && col>=0 && col<this->cols());
const Index outer = Derived::IsRowMajor ? row : col;
const Index inner = Derived::IsRowMajor ? col : row;
Index start = this->outerIndexPtr()[outer];
Index end = this->isCompressed() ? this->outerIndexPtr()[outer+1] : this->outerIndexPtr()[outer] + this->innerNonZeroPtr()[outer];
eigen_assert(end>=start && "you are using a non finalized sparse matrix or written coefficient does not exist");
internal::LowerBoundIndex p;
p.value = std::lower_bound(this->innerIndexPtr()+start, this->innerIndexPtr()+end,inner) - this->innerIndexPtr();
p.found = (p.value<end) && (this->innerIndexPtr()[p.value]==inner);
return p;
}
friend struct internal::evaluator<SparseCompressedBase<Derived> >;
private:
template<typename OtherDerived> explicit SparseCompressedBase(const SparseCompressedBase<OtherDerived>&);
};
@ -333,17 +355,8 @@ protected:
Index find(Index row, Index col) const
{
eigen_internal_assert(row>=0 && row<m_matrix->rows() && col>=0 && col<m_matrix->cols());
const Index outer = Derived::IsRowMajor ? row : col;
const Index inner = Derived::IsRowMajor ? col : row;
Index start = m_matrix->outerIndexPtr()[outer];
Index end = m_matrix->isCompressed() ? m_matrix->outerIndexPtr()[outer+1] : m_matrix->outerIndexPtr()[outer] + m_matrix->innerNonZeroPtr()[outer];
eigen_assert(end>=start && "you are using a non finalized sparse matrix or written coefficient does not exist");
const Index p = std::lower_bound(m_matrix->innerIndexPtr()+start, m_matrix->innerIndexPtr()+end,inner) - m_matrix->innerIndexPtr();
return ((p<end) && (m_matrix->innerIndexPtr()[p]==inner)) ? p : Dynamic;
internal::LowerBoundIndex p = m_matrix->lower_bound(row,col);
return p.found ? p.value : Dynamic;
}
const Derived *m_matrix;

View File

@ -140,6 +140,14 @@ struct SparseSelfAdjointShape { static std::string debugName() { return "SparseS
template<> struct glue_shapes<SparseShape,SelfAdjointShape> { typedef SparseSelfAdjointShape type; };
template<> struct glue_shapes<SparseShape,TriangularShape > { typedef SparseTriangularShape type; };
// return type of SparseCompressedBase::lower_bound;
struct LowerBoundIndex {
LowerBoundIndex() : value(-1), found(false) {}
LowerBoundIndex(Index val, bool ok) : value(val), found(ok) {}
Index value;
bool found;
};
} // end namespace internal
/** \ingroup SparseCore_Module