00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00039 #ifndef _SIMTH_CONTAINERS_H
00040 #define _SIMTH_CONTAINERS_H
00041
00042
00043 #include <string>
00044 #include <iostream>
00045
00046 #include <simthetic/misc.h>
00047 #include <simthetic/exceptions.h>
00048 #include <simthetic/checkedvector.h>
00049 #include <simthetic/array.h>
00050 #include <simthetic/phbib.h>
00051
00052 namespace simth
00053 {
00054
00064
00065
00070 template<class T>
00071 class checkedVector2d : public checkedVector< checkedVector<T> >
00072 {
00073 public:
00074 checkedVector2d(size_t cols,size_t rows);
00075 void resize(size_t newCols, size_t newRows);
00076 };
00077
00078
00079 template<class T>
00080 checkedVector2d<T>::checkedVector2d(size_t cols,size_t rows)
00081 : simth::checkedVector< checkedVector<T> >(cols)
00082 {
00083
00084 for(size_t i=0;i< cols;++i) {
00085 (*this)[i].resize(rows);
00086 }
00087 }
00088
00089
00090
00091
00092
00097 template<class T>
00098 class vector2d : public std::vector< std::vector<T> >
00099 {
00100 public:
00101 vector2d(size_t cols,size_t rows);
00102
00103 T get(size_t col, size_t row) const;
00104 void set(size_t col, size_t row,const T& newVal);
00105 };
00106
00107
00108 template<class T>
00109 vector2d<T>::vector2d(size_t cols,size_t rows)
00110 : std::vector<std::vector<T> >(cols)
00111 {
00112
00113 for(size_t i=0;i< cols;++i) {
00114 (*this)[i].resize(rows);
00115 }
00116 }
00117
00118
00119
00120 template<class T>
00121 inline T vector2d<T>::get(size_t col, size_t row) const
00122 {
00123 #if DEBUG==1
00124 if(col >= this->size()) {
00125 throw std::out_of_range("vector2d::at(size_t col, size_t row) : col == "
00126 +i2string(col)+" > " + i2string(this->size()) +" (row =="+i2string(row)+")");
00127 }
00128 if(row >= ((*this)[0]).size()) {
00129 throw std::out_of_range("vector2d::at(size_t col, size_t row) : (col == "
00130 +i2string(col)+") row =="+i2string(row));
00131 }
00132 #endif
00133 return (*this)[col][row];
00134 }
00135
00136
00137 template<class T>
00138 void vector2d<T>::set(size_t col, size_t row,const T& newVal)
00139 {
00140 #if DEBUG==1
00141 if(col >= this->size()) {
00142 throw std::out_of_range("vector2d::set(size_t col, size_t row, size_t newVal) : col == "
00143 +i2string(col)+" > " + i2string(this->size()) +" (row =="+i2string(row)+")");
00144 }
00145 if(row >= ((*this)[0]).size()) {
00146 throw std::out_of_range("vector2d::set(size_t col, size_t row, size_t newVal) : (col == "
00147 +i2string(col)+") row =="+i2string(row));
00148 }
00149 #endif
00150 (*this)[col][row] = newVal;
00151 }
00152
00153
00154
00156 class intVector2d : public std::vector<std::vector<int> >
00157 {
00158 public:
00159 intVector2d(size_t cols,size_t rows);
00160 int get(size_t col, size_t row) const;
00161 void set(size_t col, size_t row, int newVal);
00162 };
00163
00164
00165
00167 class boolVector2d : public std::vector< std::vector<bool> >
00168 {
00169 public:
00170 boolVector2d(size_t cols,size_t rows);
00171 bool get(size_t col, size_t row) const;
00172 void set(size_t col, size_t row, bool bitVal);
00173 };
00174
00175
00180 class intVector3d : public std::vector< std::vector< std::vector<int> > >
00181 {
00182 public:
00183 intVector3d(unsigned int cols, unsigned int rows, unsigned int planes);
00184 int get(unsigned int col, unsigned int row, unsigned int plane) const;
00185 void set(unsigned int col, unsigned int row, unsigned int plane,
00186 int newVal);
00187 };
00188
00189
00190
00191
00192
00193
00198 template<class T> class Array2d
00199 {
00200 typedef Array<T> row_type;
00201
00202 size_t d2;
00203
00204
00205 std::vector< row_type > array2dPtr;
00206
00207 public:
00209 Array2d(): d2(0)
00210 , array2dPtr(0) {}
00213 Array2d(size_t dim1, size_t dim2);
00219 Array2d(size_t dim1, size_t dim2, const T& Value);
00221 Array2d(const Array2d<T>& a);
00223 ~Array2d() {}
00225 Array2d<T>& operator=(const Array2d<T>& a);
00227 Array<T>& operator[](size_t pos);
00229 const Array<T>& operator[](size_t pos) const;
00231 size_t dim1() const {return array2dPtr.size();}
00234 size_t dim2() const {return d2;}
00236 size_t size() const {return d2*array2dPtr.size();}
00242 void resize(size_t dim1, size_t dim2, const T& val = T());
00243 };
00244
00245
00246 template<class T>
00247 inline Array2d<T>::Array2d(size_t dim1, size_t dim2)
00248 : d2(dim2)
00249 , array2dPtr(dim1, row_type(dim2))
00250 {}
00251
00252 template<class T>
00253 inline Array2d<T>::Array2d(size_t dim1, size_t dim2, const T& value)
00254 : d2(dim2)
00255 , array2dPtr(dim1, row_type(value, dim2))
00256 {}
00257
00258 template<class T>
00259 inline Array2d<T>::Array2d(const Array2d<T>& a)
00260 : d2(a.d2)
00261 , array2dPtr(a.array2dPtr)
00262 {}
00263
00264 template<class T>
00265 inline Array2d<T>& Array2d<T>::operator=(const Array2d& a)
00266 {
00267 d2 = a.d2;
00268 array2dPtr.operator=(a.array2dPtr);
00269 return *this;
00270 }
00271
00272 template<class T>
00273 inline void Array2d<T>::resize(size_t sDim1, size_t sDim2, const T& val)
00274 {
00275 if (d2 != sDim2) {
00276 d2 = sDim2;
00277
00278 for (size_t i = 0; i < array2dPtr.size(); i++){
00279 array2dPtr[i].resize(sDim2, val);
00280 }
00281 }
00282
00283 array2dPtr.resize(sDim1, row_type(val, sDim2) );
00284 }
00285
00286
00287 template<class T>
00288 inline Array<T>& Array2d<T>::operator[](size_t pos)
00289 {
00290 if(DEBUG) {
00291 if(pos >= dim1()) {
00292 throw std::out_of_range("inline Array2d::operator[](size_t pos)const : pos == "+
00293 simth::i2string(pos)+" this.size() == "+
00294 simth::i2string(dim1()));
00295 }
00296 }
00297 return array2dPtr[pos];
00298 }
00299
00300 template<class T>
00301 inline const Array<T>& Array2d<T>::operator[](size_t pos) const
00302 {
00303 if(DEBUG) {
00304 if(pos >= dim1()) {
00305 throw std::out_of_range("inline Array2d::operator[](size_t pos)const : pos == "+
00306 simth::i2string(pos)+" this.dim1() == "+
00307 simth::i2string(dim1()));
00308 }
00309 }
00310 return array2dPtr[pos];
00311 }
00312
00313
00314
00315 template<class T>
00316 void twoDim2oneDim(const Array2d<T>& arr, Array<T>* vec, bool fortranlike = false)
00317 {
00318 if(DEBUG) {
00319 if(vec == NULL) {
00320 throw std::runtime_error("void twoDim2oneDim(const Array2d<T>& arr, Array<T>* vec) : vec == NULL");
00321 }
00322 }
00323
00324
00325 size_t currPos = 0;
00326 size_t xSize = arr.dim1();
00327 size_t ySize = arr.dim2();
00328
00329 if(fortranlike) {
00330 for(size_t x = 0; x < xSize; ++x) {
00331 for(size_t y = 0; y < ySize; ++y) {
00332 (*vec)[currPos]=arr[x][y];
00333 ++currPos;
00334 }
00335 }
00336 }
00337 else {
00338 for(size_t y = 0; y < ySize; ++y) {
00339 for(size_t x = 0; x < xSize; ++x) {
00340 (*vec)[currPos]=arr[x][y];
00341 ++currPos;
00342 }
00343 }
00344 }
00345 }
00346
00347
00348 template<class T>
00349 void oneDim2twoDim(const Array<T>& vec, size_t xSize, size_t ySize, Array2d<T>* arr, bool fortranlike = false)
00350 {
00351 if(DEBUG) {
00352 if(arr == NULL) {
00353 throw std::runtime_error("void oneDim2twoDim(const Array<T>& vec, size_t x, size_t y, Array2d<T>* arr) : arr == NULL");
00354 }
00355 if(xSize*ySize != vec.size()) {
00356 throw std::runtime_error("void oneDim2twoDim(const Array<T>& vec, size_t x, size_t y, Array2d<T>* arr) : "
00357 "x*y != vec.size()");
00358 }
00359 }
00360
00361 size_t currPos = 0;
00362 if(fortranlike) {
00363 for(size_t x = 0; x < xSize; ++x) {
00364 for(size_t y = 0; y < ySize; ++y) {
00365 (*arr)[x][y]=vec[currPos];
00366 ++currPos;
00367 }
00368 }
00369 }
00370 else {
00371 for(size_t y = 0; y < ySize; ++y) {
00372 for(size_t x = 0; x < xSize; ++x) {
00373 (*arr)[x][y]=vec[currPos];
00374 ++currPos;
00375 }
00376 }
00377 }
00378 }
00379
00380
00381 template<class T>
00382 std::ostream& operator<<(std::ostream &os, const Array2d<T> &a)
00383 {
00384
00385 std::ios::fmtflags oldSettings = os.flags();
00386
00387 os.precision(2);
00388 for(size_t p2 = 0; p2 < a.dim2(); p2++) {
00389 for(size_t p1 = 0; p1 < a.dim1(); p1++) {
00390 std::cout<<" "<<a[p1][p2];
00391 }
00392 std::cout<<std::endl;
00393 }
00394
00395 os.flags(oldSettings);
00396 return os;
00397 }
00398
00399
00401
00402 }
00403
00404 #endif // _SIMTH_CONTAINERS_H