9 #ifndef CSparseMatrixTemplate_H 10 #define CSparseMatrixTemplate_H 73 const_iterator it=objectList.find(std::make_pair(r,c));
74 if (it==objectList.end())
return T();
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");
84 return (objectList.find(std::make_pair(r,c)) != objectList.end());
91 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES) 92 if (r>=mRows||c>=mColumns)
throw std::logic_error(
"Out of range");
94 return objectList[std::make_pair(r,c)];
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");
119 vec.resize(mColumns);
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);
177 inline const_iterator
begin()
const {
178 return objectList.begin();
184 inline const_iterator
end()
const {
185 return objectList.end();
191 inline const_reverse_iterator
rbegin()
const {
192 return objectList.rbegin();
198 inline const_reverse_iterator
rend()
const {
199 return objectList.rend();
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);
215 if (obj==nullObject) objectList.erase(index);
216 else objectList[index]=obj;
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);
233 if (obj==nullObject) objectList.erase(index);
234 else objectList[index]=obj;
242 if (mRows==nRows && mColumns==nCols)
return;
245 std::vector<std::pair<size_t,size_t> > toErase;
246 for (const_iterator it=objectList.begin();it!=objectList.end();++it) {
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;
273 size_t N=objectList.size();
276 for (const_iterator it=objectList.begin();it!=objectList.end();++it) vec.push_back(it->second);
283 return objectList.size();
288 inline bool empty()
const {
return objectList.empty(); }
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;
325 for (const_iterator it=
begin();it!=
end();++it)
if (it->second==nullObject) nulls.push_back(it->first);
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);
void insertMatrix(size_t row, size_t column, const MATRIX_LIKE &mat)
Inserts submatrix at a given location.
void getAsVector(std::vector< T > &vec) const
Gets a vector containing all the elements of the matrix, ignoring their position. ...
const_reverse_iterator rend() const
Returns an iterator which points to the starting point of the matrix, although it's the upper limit o...
size_t getRowCount() const
Returns the amount of rows in this matrix.
size_t getColCount() const
Returns the amount of columns in this matrix.
void setColumn(size_t nCol, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full column into the matrix.
void insert(size_t row, size_t column, const T &obj)
Inserts an element into the matrix.
const Scalar * const_iterator
SparseMatrixMap::const_iterator const_iterator
Const iterator to move through the matrix.
T & operator()(size_t r, size_t c)
Reference access operator.
bool empty() const
Are there no elements set to !=0 ?
CSparseSymmetricalMatrix(const CSparseSymmetricalMatrix &o)
A sparse matrix container (with cells of any type), with iterators.
void purge(T nullObject=T())
Checks each non-null elements against the basic objects, erasing unnecesary references to it...
void getRow(size_t nRow, std::vector< T > &vec) const
Extracts a full row from the matrix.
const_iterator begin() const
Returns an iterator which points to the starting point of the matrix.
CSparseMatrixTemplate(size_t nR, size_t nC)
Constructor with default size.
const_iterator end() const
Returns an iterator which points to the end of the matrix.
virtual ~CSparseSymmetricalMatrix()
SparseMatrixMap::const_reverse_iterator const_reverse_iterator
Const reverse iterator to move through the matrix.
size_t getNonNullElements() const
Gets the amount of non-null elements inside the matrix.
T operator()(size_t r, size_t c) const
bool isNotNull(size_t nRow, size_t nCol) const
Checks whether an element of the matrix is not the default object.
CSparseMatrixTemplate< T > operator()(size_t firstRow, size_t lastRow, size_t firstColumn, size_t lastColumn) const
Extracts a submatrix form the matrix.
bool isNull(size_t nRow, size_t nCol) const
Checks whether an element of the matrix is the default object.
CSparseMatrixTemplate()
Basic constructor with no data.
bool exists(size_t r, size_t c) const
Element access operator.
size_t mRows
Size of the matrix.
void clear()
Completely removes all elements, although maintaining the matrix's size.
CSparseSymmetricalMatrix(const CSparseMatrixTemplate< T > &o)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CSparseSymmetricalMatrix()
size_t getNullElements() const
Gets the amount of null elements inside the matrix.
SparseMatrixMap objectList
Actual matrix.
void resize(size_t nRows, size_t nCols)
Changes the size of the matrix.
T operator()(size_t r, size_t c) const
Element access operator.
std::map< std::pair< size_t, size_t >, T > SparseMatrixMap
Internal map type, used to store the actual matrix.
void resize(size_t matrixSize)
void setRow(size_t nRow, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full row into the matrix.
void getColumn(size_t nCol, std::vector< T > &vec) const
Extracts a full column from 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...
T & operator()(size_t r, size_t c)
A sparse matrix container for square symmetrical content around the main diagonal.