22 #ifndef __SparseArray2D_hh__
23 #define __SparseArray2D_hh__
25 #include <bias_config.h>
27 #include <Base/Debug/Exception.hh>
28 #include <Base/Debug/Error.hh>
37 std::ostream& operator<<(std::ostream& os, const SparseArray2D<T> &t);
80 bool is_valid(
const unsigned row,
const unsigned col)
const;
82 void erase(
const unsigned row,
const unsigned col);
85 T&
operator()(
const unsigned row,
const unsigned col);
86 const T&
operator()(
const unsigned row,
const unsigned col)
const;
100 inline long long unsigned first()
const
101 {
return Val_->
first; }
106 { Val_ = it.Val_;
return *
this; }
111 typename std::map<long long unsigned, T>::iterator Val_;
112 inline iterator(
const typename std::map<long long unsigned, T>::iterator it) : Val_(it) {}
127 inline long long unsigned first()
const
128 {
return Val_->
first; }
131 {
return (Val_ == it.Val_); }
133 {
return (Val_ != it.Val_); }
135 { Val_ = it.Val_;
return *
this; }
139 typename std::map<long long unsigned, T>::const_iterator Val_;
144 it.Val_ =
Data_.begin();
149 it.Val_ =
Data_.end();
161 friend std::ostream& operator<<<T>(std::ostream& os,
165 std::map<long long unsigned, T>
Data_;
169 const unsigned col)
const
170 {
return (
long long unsigned)row * (
long long unsigned)
Ncols_ +
171 (
long long unsigned)col; }
174 unsigned& row,
unsigned & col)
const
175 { row = (unsigned)(map / (
long long unsigned)
Ncols_);
176 col = (unsigned)(map % (
long long unsigned)
Ncols_); }
187 : Data_(), Nrows_(0), Ncols_(0)
193 : Data_(), Nrows_(nrows), Ncols_(ncols)
195 if (nrows == 0 || ncols == 0)
196 BEXCEPTION(
"invalid size in Arra2D "<<nrows<<
"x"<<ncols);
202 : Data_(), Nrows_(0), Ncols_(0)
215 std::map<long long unsigned, T> tmp = m.
Data_;
225 { Data_.clear(); Ncols_ = Nrows_ = 0; }
231 std::map<long long unsigned, T> tmp;
233 typename std::map<long long unsigned, T>::const_iterator it;
234 for (it = Data_.begin(); it!=Data_.end(); it++){
235 Map2Indices_(it->first, row, col);
236 tmp[(
long long unsigned)row * (
long long unsigned)ncols +
237 (
long long unsigned)col] = it->second;
248 if (row >= Nrows_ || col >= Ncols_)
return false;
249 if (Data_.find(Indices2Map_(row, col)) == Data_.end())
return false;
257 BIASASSERT(row < Nrows_ && col < Ncols_);
258 typename std::map<long long unsigned, T>::iterator it;
259 it = Data_.find(Indices2Map_(row, col));
260 if (it == Data_.end())
return;
268 if (row >= Nrows_ || col >= Ncols_)
269 BEXCEPTION(
"index out of bounds: array2D size = "<<Ncols_<<
"x"<<Nrows_
270 <<
" and index access at ["<<row<<
"]["<<col<<
"]");
271 return Data_[Indices2Map_(row, col)];
278 if (row >= Nrows_ || col >= Ncols_)
279 BEXCEPTION(
"index out of bounds: array2D size = "<<Ncols_<<
"x"<<Nrows_
280 <<
" and index access at ["<<row<<
"]["<<col<<
"]");
281 typename std::map<long long unsigned, T>::const_iterator it;
282 if ((it=Data_.find(Indices2Map_(row, col)))==Data_.end())
283 BEXCEPTION(
"matrix is empty at ["<<row<<
"]["<<col<<
"]");
289 std::ostream& operator<<(std::ostream& os, const SparseArray2D<T> &a)
292 unsigned col, row, last_row=0 ;
293 for (it=a.begin(); it!=a.end(); it++){
294 a.position(it, row, col);
299 os <<
"a["<<row<<
"]["<<col<<
"]="<<*it<<
" ";
void Map2Indices_(const long long unsigned &map, unsigned &row, unsigned &col) const
bool operator!=(const const_iterator &it)
const_iterator begin() const
std::map< long long unsigned, T > Data_
for iterator access todo: derive from std::iterator class(es)
void resize(const unsigned nrows, const unsigned ncols)
preserves the content
const_iterator operator++(int)
bool operator==(const iterator &it)
bool operator!=(const iterator &it)
bool is_valid(const unsigned row, const unsigned col) const
long long unsigned Indices2Map_(const unsigned row, const unsigned col) const
const_iterator(const const_iterator &it)
generic two dimensional psarsly populated rectangular array holding arbitrary data types ...
for const_iterator access todo: derive from std::iterator class(es)
bool operator==(const const_iterator &it)
SparseArray2D< T > & operator=(const SparseArray2D< T > &m)
copy operator
iterator(const iterator &it)
void position(const iterator &it, unsigned &row, unsigned &col) const
T & operator()(const unsigned row, const unsigned col)
checked element access
void position(const const_iterator &it, unsigned &row, unsigned &col) const
long long unsigned first() const
const_iterator & operator=(const const_iterator &it)
const_iterator end() const
long long unsigned first() const
void erase(const unsigned row, const unsigned col)
void clear()
frees the memory
unsigned num_entries() const
const_iterator(const iterator &it)
iterator & operator=(const iterator &it)