00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef EMARRAY_H_
00037 #define EMARRAY_H_
00038
00039 #include "exception.h"
00040 #include <vector>
00041
00042 using std::vector;
00043
00044 namespace EMAN {
00045
00052 template<class T>
00053 class EMArray {
00054 public:
00056 explicit EMArray(size_t xsize, size_t ysize=1, size_t zsize=1)
00057 : nx(xsize),ny(ysize),nz(zsize),
00058 xoff(0),yoff(0),zoff(0) {
00059 size = (size_t)nx*ny*nz;
00060 array = new T[size];
00061 }
00063 ~EMArray() { delete [] array; }
00068 void set_array_offsets(const size_t xoff_=0, const size_t yoff_=0,
00069 const size_t zoff_=0) {
00070 xoff = xoff_; yoff = yoff_; zoff = zoff_;
00071 }
00075 void set_array_offsets(const vector<int> offsets) {
00076 set_array_offsets(offsets[0],offsets[1],offsets[2]);
00077 }
00079 vector<int> get_array_offsets() {
00080 vector<int> offsets;
00081 offsets.push_back(xoff);
00082 offsets.push_back(yoff);
00083 offsets.push_back(zoff);
00084 return offsets;
00085 }
00091 T& operator()(const size_t ix, const size_t iy=0,
00092 const size_t iz=0) {
00093 long pos = (ix-xoff) + ((iy-yoff)+(iz-zoff)*ny)*nx;
00094 #ifdef BOUNDS_CHECKING
00095 if (pos < 0 || pos >= long(size))
00096 throw OutofRangeException(0, size-1, pos, "emarray");
00097 #endif // BOUNDS_CHECKING
00098 return *(array + pos);
00099 }
00100
00101 int get_xsize() const { return nx; }
00102 int get_ysize() const { return ny; }
00103 int get_zsize() const { return nz; }
00104
00105 private:
00106 const size_t nx, ny, nz;
00107 size_t size;
00108 size_t xoff, yoff, zoff;
00109 T* array;
00110
00111 EMArray(const EMArray&);
00112 EMArray& operator=(const EMArray&);
00113 };
00114 }
00115 #endif // EMARRAY_H_