22 #ifndef __Array2D_hh__
23 #define __Array2D_hh__
25 #include <bias_config.h>
27 #include <Base/Debug/Exception.hh>
34 std::ostream &operator<<(std::ostream& os, const Array2D<T> &arg);
66 {
return (Data_==0); }
69 void fill(
const T& val);
71 unsigned ncols()
const {
return Ncols_; }
73 unsigned nrows()
const {
return Nrows_; }
75 unsigned size()
const {
return Nrows_ * Ncols_; }
78 T&
operator()(
const unsigned row,
const unsigned col);
79 const T&
operator()(
const unsigned row,
const unsigned col)
const;
82 T*
operator[](
const unsigned row) {
return &(Data_[row*Ncols_]); }
84 {
return &(Data_[row*Ncols_]); }
102 { Val_ = it.Val_;
return *
this; }
124 {
return (Val_ == it.Val_); }
126 {
return (Val_ != it.Val_); }
128 { Val_ = it.Val_;
return *
this; }
144 unsigned Nrows_, Ncols_;
153 : Data_(0), Nrows_ (0), Ncols_ (0)
159 : Data_(new T[nrows * ncols]), Nrows_ (nrows), Ncols_ (ncols)
161 if (nrows == 0 || ncols == 0)
162 BEXCEPTION(
"invalid size in Arra2D "<<nrows<<
"x"<<ncols);
168 : Data_(0), Nrows_ (0), Ncols_(0)
184 const int size = m.Ncols_*m.Nrows_;
185 T *tmp =
new T[size];
188 for (
int i=0; i<size; i++){ tmp[i] = m.Data_[i]; }
189 if (!empty()) { clear(); }
199 {
if (Data_){
delete[] Data_; Data_ = 0; Ncols_ = Nrows_ = 0; } }
205 if (nrows==Nrows_ && ncols==Ncols_)
return;
206 T *tmp =
new T[nrows*ncols];
209 const unsigned nr=Nrows_, nc=Ncols_;
210 unsigned r, c, tmp_offs, old_offs;
211 for (r=0; r<nr; r++){
214 for (c=0; c<nc; c++){
215 tmp[tmp_offs+c] = Data_[old_offs+c];
229 const unsigned nr=Nrows_, nc=Ncols_;
231 for (r=0; r<nr; r++){
233 for (c=0; c<nc; c++){
243 if (row >= Nrows_ || col >= Ncols_)
244 BEXCEPTION(
"index out of bounds: array2D size = "<<Ncols_<<
"x"<<Nrows_
245 <<
" and index access at ["<<row<<
"]["<<col<<
"]");
246 return Data_[row*Ncols_ + col];
253 if (row >= Nrows_ || col >= Ncols_)
254 BEXCEPTION(
"index out of bounds: array2D size = "<<Ncols_<<
"x"<<Nrows_
255 <<
" and index access at ["<<row<<
"]["<<col<<
"]");
256 return Data_[row*Ncols_ + col];
260 std::ostream &operator<<(std::ostream& os, const Array2D<T> &arg)
262 os << arg.nrows() <<
" " << arg.ncols() << std::endl;
263 for (
unsigned r=0; r<arg.nrows(); r++){
264 for (
unsigned c=0; c<arg.ncols(); c++){
265 os << arg(r,c) <<
"\t";
iterator(const iterator &it)
for const_iterator access todo: derive from std::iterator class(es)
void clear()
frees the memory
Array2D< T > & operator=(const Array2D< T > &m)
copy operator
T * operator[](const unsigned row)
unchecked element access
generic two dimensional rectangular array holding arbitrary data types
T & operator()(const unsigned row, const unsigned col)
checked element access
bool operator!=(const iterator &it)
for iterator access todo: derive from std::iterator class(es)
bool operator==(const const_iterator &it)
const_iterator operator++(int)
void resize(const unsigned nrows, const unsigned ncols)
preserves the content
const_iterator end() const
const_iterator(const const_iterator &it)
const_iterator & operator=(const const_iterator &it)
bool operator==(const iterator &it)
const_iterator(const iterator &it)
void fill(const T &val)
fills the array with val
iterator & operator=(const iterator &it)
bool operator!=(const const_iterator &it)
const T * operator[](const unsigned row) const
const_iterator begin() const