28 #ifndef CSparseMatrixTemplate_H
29 #define CSparseMatrixTemplate_H
93 if (it==objectList.end())
return T();
94 else return it->second;
99 inline bool exists(
size_t r,
size_t c)
const {
100 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
101 if (r>=mRows||c>=mColumns)
throw std::logic_error(
"Out of range");
103 return (objectList.find(make_pair(r,c)) != objectList.end());
110 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
111 if (r>=mRows||c>=mColumns)
throw std::logic_error(
"Out of range");
113 return objectList[make_pair(r,c)];
134 void getRow(
size_t nRow,std::vector<T> &vec)
const {
135 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
136 if (nRow>=mRows)
throw std::logic_error(
"Out of range");
138 vec.resize(mColumns);
141 const pair<size_t,size_t> &index=it->first;
142 if (index.first<nRow)
continue;
143 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;
148 for (
size_t i=nextIndex;i<mColumns;i++) vec[i]=T();
159 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
160 if (nCol>=mColumns)
throw std::logic_error(
"Out of range");
165 const pair<size_t,size_t> &index=it->first;
166 if (index.second==nCol) {
167 for (
size_t i=nextIndex;i<index.first;i++) vec[i]=T();
168 vec[index.first]=it->second;
169 nextIndex=index.first+1;
172 for (
size_t i=nextIndex;i<mRows;i++) vec[i]=T();
178 inline void insert(
size_t row,
size_t column,
const T& obj) {
179 operator()(row,column)=obj;
183 template <
class MATRIX_LIKE>
184 inline void insertMatrix(
size_t row,
size_t column,
const MATRIX_LIKE& mat)
186 for (
size_t nr=0;nr<mat.getRowCount();nr++)
187 for (
size_t nc=0;nc<mat.getColCount();nc++)
188 operator()(row+nr,column+nc)=mat(nr,nc);
197 return objectList.begin();
204 return objectList.end();
211 return objectList.rbegin();
218 return objectList.rend();
225 void setRow(
size_t nRow,
const std::vector<T> &vec,
const T& nullObject=T()) {
226 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
227 if (nRow>=mRows)
throw std::logic_error(
"Out of range");
230 if (N!=mColumns)
throw std::logic_error(
"Wrong-sized vector");
231 for (
size_t i=0;i<N;i++) {
233 pair<size_t,size_t> index=make_pair(nRow,i);
234 if (obj==nullObject) objectList.erase(index);
235 else objectList[index]=obj;
243 void setColumn(
size_t nCol,
const std::vector<T> &vec,
const T& nullObject=T()) {
244 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
245 if (nCol>=mColumns)
throw std::logic_error(
"Out of range");
248 if (N!=mRows)
throw std::logic_error(
"Wrong-sized vector");
249 for (
size_t i=0;i<N;i++) {
251 pair<size_t,size_t> index=make_pair(i,nCol);
252 if (obj==nullObject) objectList.erase(index);
253 else objectList[index]=obj;
261 if (mRows==nRows && mColumns==nCols)
return;
264 std::vector<pair<size_t,size_t> > toErase;
265 for (
const_iterator it=objectList.begin();it!=objectList.end();++it) {
266 const pair<size_t,size_t> &i=it->first;
267 if (i.first>=nRows||i.second>=nCols) toErase.push_back(it->first);
269 for (std::vector<pair<size_t,size_t> >::
const_iterator it=toErase.begin();it!=toErase.end();++it) objectList.erase(*it);
277 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
278 if (lastRow>=mRows||lastColumn>=mColumns)
throw std::logic_error(
"Out of range");
279 if (firstRow>lastRow||firstColumn>lastColumn)
throw std::logic_error(
"Invalid size");
283 const pair<size_t,size_t> &i=it->first;
284 if (i.first>=firstRow&&i.first<=lastRow&&i.second>=firstColumn&&i.second<=lastColumn) res(i.first-firstRow,i.second-firstColumn)=it->second;
292 size_t N=objectList.size();
295 for (
const_iterator it=objectList.begin();it!=objectList.end();++it) vec.push_back(it->second);
302 return objectList.size();
307 inline bool empty()
const {
return objectList.empty(); }
314 return mRows*mColumns-getNonNullElements();
321 inline bool isNull(
size_t nRow,
size_t nCol)
const {
322 if (nRow>=mRows||nCol>=mColumns)
throw std::logic_error(
"Out of range");
323 return objectList.count(make_pair(nRow,nCol))==0;
330 if (nRow>=mRows||nCol>=mColumns)
throw std::logic_error(
"Out of range");
331 return objectList.count(make_pair(nRow,nCol))>0;
343 std::vector<std::pair<size_t,size_t> > nulls;
345 for (std::vector<std::pair<size_t,size_t> >::
const_iterator it=nulls.begin();it!=nulls.end();++it) objectList.erase(*it);
367 if (c<r) std::swap(r,c);
370 else return it->second;
373 if (c<r) std::swap(r,c);