9#ifndef CSparseMatrixTemplate_H
10#define CSparseMatrixTemplate_H
75 else return it->second;
80 inline bool exists(
size_t r,
size_t c)
const {
81#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
82 if (r>=
mRows||c>=
mColumns)
throw std::logic_error(
"Out of range");
91#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
92 if (r>=
mRows||c>=
mColumns)
throw std::logic_error(
"Out of range");
115 void getRow(
size_t nRow,std::vector<T> &vec)
const {
116#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
117 if (nRow>=
mRows)
throw std::logic_error(
"Out of range");
122 const std::pair<size_t,size_t> &index=it->first;
123 if (index.first<nRow)
continue;
124 else if (index.first==nRow) {
125 for (
size_t i=nextIndex;i<index.second;i++) vec[i]=T();
126 vec[index.second]=it->second;
127 nextIndex=index.second+1;
129 for (
size_t i=nextIndex;i<
mColumns;i++) vec[i]=T();
140#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
141 if (nCol>=
mColumns)
throw std::logic_error(
"Out of range");
146 const std::pair<size_t,size_t> &index=it->first;
147 if (index.second==nCol) {
148 for (
size_t i=nextIndex;i<index.first;i++) vec[i]=T();
149 vec[index.first]=it->second;
150 nextIndex=index.first+1;
153 for (
size_t i=nextIndex;i<
mRows;i++) vec[i]=T();
159 inline void insert(
size_t row,
size_t column,
const T& obj) {
164 template <
class MATRIX_LIKE>
165 inline void insertMatrix(
size_t row,
size_t column,
const MATRIX_LIKE& mat)
167 for (
size_t nr=0;nr<mat.getRowCount();nr++)
168 for (
size_t nc=0;nc<mat.getColCount();nc++)
169 operator()(row+nr,column+nc)=mat(nr,nc);
206 void setRow(
size_t nRow,
const std::vector<T> &vec,
const T& nullObject=T()) {
207#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
208 if (nRow>=
mRows)
throw std::logic_error(
"Out of range");
211 if (N!=
mColumns)
throw std::logic_error(
"Wrong-sized vector");
212 for (
size_t i=0;i<N;i++) {
214 std::pair<size_t,size_t> index=std::make_pair(nRow,i);
224 void setColumn(
size_t nCol,
const std::vector<T> &vec,
const T& nullObject=T()) {
225#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
226 if (nCol>=
mColumns)
throw std::logic_error(
"Out of range");
229 if (N!=
mRows)
throw std::logic_error(
"Wrong-sized vector");
230 for (
size_t i=0;i<N;i++) {
232 std::pair<size_t,size_t> index=std::make_pair(i,nCol);
245 std::vector<std::pair<size_t,size_t> > toErase;
247 const std::pair<size_t,size_t> &i=it->first;
248 if (i.first>=nRows||i.second>=nCols) toErase.push_back(it->first);
250 for (std::vector<std::pair<size_t,size_t> >::
const_iterator it=toErase.begin();it!=toErase.end();++it)
objectList.erase(*it);
258#if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
259 if (lastRow>=
mRows||lastColumn>=
mColumns)
throw std::logic_error(
"Out of range");
260 if (firstRow>lastRow||firstColumn>lastColumn)
throw std::logic_error(
"Invalid size");
264 const std::pair<size_t,size_t> &i=it->first;
265 if (i.first>=firstRow&&i.first<=lastRow&&i.second>=firstColumn&&i.second<=lastColumn) res(i.first-firstRow,i.second-firstColumn)=it->second;
302 inline bool isNull(
size_t nRow,
size_t nCol)
const {
303 if (nRow>=
mRows||nCol>=
mColumns)
throw std::logic_error(
"Out of range");
304 return objectList.count(std::make_pair(nRow,nCol))==0;
311 if (nRow>=
mRows||nCol>=
mColumns)
throw std::logic_error(
"Out of range");
312 return objectList.count(std::make_pair(nRow,nCol))>0;
324 std::vector<std::pair<size_t,size_t> > nulls;
326 for (std::vector<std::pair<size_t,size_t> >::
const_iterator it=nulls.begin();it!=nulls.end();++it)
objectList.erase(*it);
348 if (c<r) std::swap(r,c);
351 else return it->second;
354 if (c<r) std::swap(r,c);
A sparse matrix container (with cells of any type), with iterators.
const_iterator begin() const
Returns an iterator which points to the starting point of the matrix.
const_reverse_iterator rbegin() const
Returns an iterator which points to the end of the matrix, and can be used to move backwards.
void setRow(size_t nRow, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full row into the matrix.
SparseMatrixMap objectList
Actual matrix.
size_t getRowCount() const
Returns the amount of rows in this matrix.
void getAsVector(std::vector< T > &vec) const
Gets a vector containing all the elements of the matrix, ignoring their position.
bool isNotNull(size_t nRow, size_t nCol) const
Checks whether an element of the matrix is not the default object.
const_iterator end() const
Returns an iterator which points to the end of the matrix.
CSparseMatrixTemplate(size_t nR, size_t nC)
Constructor with default size.
void getColumn(size_t nCol, std::vector< T > &vec) const
Extracts a full column from the matrix.
size_t getColCount() const
Returns the amount of columns in this matrix.
void purge(T nullObject=T())
Checks each non-null elements against the basic objects, erasing unnecesary references to it.
size_t getNonNullElements() const
Gets the amount of non-null elements inside the matrix.
void insertMatrix(size_t row, size_t column, const MATRIX_LIKE &mat)
Inserts submatrix at a given location.
bool empty() const
Are there no elements set to !=0 ?
CSparseMatrixTemplate()
Basic constructor with no data.
size_t mRows
Size of the matrix.
size_t getNullElements() const
Gets the amount of null elements inside the matrix.
void clear()
Completely removes all elements, although maintaining the matrix's size.
bool isNull(size_t nRow, size_t nCol) const
Checks whether an element of the matrix is the default object.
bool exists(size_t r, size_t c) const
Element access operator.
const_reverse_iterator rend() const
Returns an iterator which points to the starting point of the matrix, although it's the upper limit o...
CSparseMatrixTemplate< T > operator()(size_t firstRow, size_t lastRow, size_t firstColumn, size_t lastColumn) const
Extracts a submatrix form the matrix.
void getRow(size_t nRow, std::vector< T > &vec) const
Extracts a full row from the matrix.
T operator()(size_t r, size_t c) const
Element access operator.
SparseMatrixMap::const_reverse_iterator const_reverse_iterator
Const reverse iterator to move through the matrix.
void resize(size_t nRows, size_t nCols)
Changes the size of the matrix.
SparseMatrixMap::const_iterator const_iterator
Const iterator to move through the matrix.
std::map< std::pair< size_t, size_t >, T > SparseMatrixMap
Internal map type, used to store the actual matrix.
void setColumn(size_t nCol, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full column into the matrix.
T & operator()(size_t r, size_t c)
Reference access operator.
void insert(size_t row, size_t column, const T &obj)
Inserts an element into the matrix.
A sparse matrix container for square symmetrical content around the main diagonal.
T & operator()(size_t r, size_t c)
virtual ~CSparseSymmetricalMatrix()
void resize(size_t matrixSize)
CSparseSymmetricalMatrix()
CSparseSymmetricalMatrix(const CSparseSymmetricalMatrix &o)
T operator()(size_t r, size_t c) const
CSparseSymmetricalMatrix(const CSparseMatrixTemplate< T > &o)
const Scalar * const_iterator
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.