EMAN2
emarray.h
Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Pawel A.Penczek, 09/09/2006 (Pawel.A.Penczek@uth.tmc.edu)
00007  * Copyright (c) 2000-2006 The University of Texas - Houston Medical School
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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             // disable copy and assignment
00111             EMArray(const EMArray&); 
00112             EMArray& operator=(const EMArray&);
00113     };
00114 }
00115 #endif // EMARRAY_H_