9 #ifndef CSparseMatrixTemplate_H
10 #define CSparseMatrixTemplate_H
98 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
99 if (
r >=
mRows ||
c >=
mColumns)
throw std::logic_error(
"Out of range");
109 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
110 if (
r >=
mRows ||
c >=
mColumns)
throw std::logic_error(
"Out of range");
129 void getRow(
size_t nRow, std::vector<T>& vec)
const
131 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
132 if (nRow >=
mRows)
throw std::logic_error(
"Out of range");
135 size_t nextIndex = 0;
139 const std::pair<size_t, size_t>&
index = it->first;
140 if (
index.first < nRow)
142 else if (
index.first == nRow)
144 for (
size_t i = nextIndex; i <
index.second; i++) vec[i] = T();
145 vec[
index.second] = it->second;
146 nextIndex =
index.second + 1;
150 for (
size_t i = nextIndex; i <
mColumns; i++) vec[i] = T();
162 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
163 if (nCol >=
mColumns)
throw std::logic_error(
"Out of range");
166 size_t nextIndex = 0;
170 const std::pair<size_t, size_t>&
index = it->first;
171 if (
index.second == nCol)
173 for (
size_t i = nextIndex; i <
index.first; i++) vec[i] = T();
174 vec[
index.first] = it->second;
175 nextIndex =
index.first + 1;
178 for (
size_t i = nextIndex; i <
mRows; i++) vec[i] = T();
190 template <
class MATRIX_LIKE>
193 for (
size_t nr = 0; nr < mat.getRowCount(); nr++)
194 for (
size_t nc = 0; nc < mat.getColCount(); nc++)
195 operator()(
row + nr,
column + nc) = mat(nr, nc);
237 size_t nRow,
const std::vector<T>& vec,
const T& nullObject = T())
239 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
240 if (nRow >=
mRows)
throw std::logic_error(
"Out of range");
242 size_t N = vec.size();
243 if (N !=
mColumns)
throw std::logic_error(
"Wrong-sized vector");
244 for (
size_t i = 0; i < N; i++)
246 const T&
obj = vec[i];
247 std::pair<size_t, size_t>
index = std::make_pair(nRow, i);
248 if (
obj == nullObject)
262 size_t nCol,
const std::vector<T>& vec,
const T& nullObject = T())
264 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
265 if (nCol >=
mColumns)
throw std::logic_error(
"Out of range");
267 size_t N = vec.size();
268 if (N !=
mRows)
throw std::logic_error(
"Wrong-sized vector");
269 for (
size_t i = 0; i < N; i++)
271 const T&
obj = vec[i];
272 std::pair<size_t, size_t>
index = std::make_pair(i, nCol);
273 if (
obj == nullObject)
289 std::vector<std::pair<size_t, size_t>> toErase;
293 const std::pair<size_t, size_t>& i = it->first;
294 if (i.first >= nRows || i.second >= nCols)
295 toErase.push_back(it->first);
299 it != toErase.end(); ++it)
308 size_t firstRow,
size_t lastRow,
size_t firstColumn,
309 size_t lastColumn)
const
311 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
313 throw std::logic_error(
"Out of range");
314 if (firstRow > lastRow || firstColumn > lastColumn)
315 throw std::logic_error(
"Invalid size");
318 lastRow + 1 - firstRow, lastColumn + 1 - firstColumn);
322 const std::pair<size_t, size_t>& i = it->first;
323 if (i.first >= firstRow && i.first <= lastRow &&
324 i.second >= firstColumn && i.second <= lastColumn)
325 res(i.first - firstRow, i.second - firstColumn) = it->second;
340 vec.push_back(it->second);
364 inline bool isNull(
size_t nRow,
size_t nCol)
const
367 throw std::logic_error(
"Out of range");
368 return objectList.count(std::make_pair(nRow, nCol)) == 0;
377 throw std::logic_error(
"Out of range");
378 return objectList.count(std::make_pair(nRow, nCol)) > 0;
390 std::vector<std::pair<size_t, size_t>> nulls;
392 if (it->second == nullObject) nulls.push_back(it->first);
395 it != nulls.end(); ++it)
429 if (
c <
r) std::swap(
r,
c);
439 if (
c <
r) std::swap(
r,
c);
442 throw std::logic_error(
"Out of range");
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.
std::map< std::pair< size_t, size_t >, T > SparseMatrixMap
Internal map type, used to store the actual matrix.
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...
T & operator()(size_t r, size_t c)
Reference access operator.
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.
void setColumn(size_t nCol, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full column into the matrix.
CSparseMatrixTemplate< T > operator()(size_t firstRow, size_t lastRow, size_t firstColumn, size_t lastColumn) const
Extracts a submatrix form the matrix.
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.
virtual ~CSparseSymmetricalMatrix()
T & operator()(size_t r, size_t c)
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
GLsizei GLsizei GLuint * obj
GLenum GLenum GLvoid * row
GLenum GLenum GLvoid GLvoid * column
GLdouble GLdouble GLdouble r
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.