arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

arrayOwn3.h

Go to the documentation of this file.
00001 /*
00002  * $Id: arrayOwn3.h,v 1.2 2003/06/27 04:58:37 jmk Exp $
00003  */
00004 ///////////////////////////////////////////////////////////////////////////
00005 //              _____________  ______________________    __   __  _____
00006 //             /  ________  |  |   ___   ________   /   |  \ /  \   |
00007 //            |  |       |  |_ |  |_ |  |       /  /    \__  |      |
00008 //            |  |  ___  |  || |  || |  |      /  /        | |      |
00009 //            |  | |   \ |  || |  || |  |     /  /      \__/ \__/ __|__
00010 //            |  | |_@  ||  || |  || |  |    /  /          Institute
00011 //            |  |___/  ||  ||_|  || |  |   /  /_____________________
00012 //             \_______/  \______/ | |__|  /___________________________
00013 //                        |  |__|  |
00014 //                         \______/
00015 //                    University of Utah       
00016 //                           2002
00017 ///////////////////////////////////////////////////////////////////////////
00018 // 8/26/02      Aaron Lefohn    Scientific Computing and Imaging Institute
00019 //                                                      School of Computing
00020 //                                                      University of Utah
00021 // 
00022 // arrayOwn3 - A lightweight 3D array class that OWNS its data.
00023 //                       - Very little (nearly none) error checking
00024 //                       - Designed to act just like a built-in 2D array, but handle memory allocation
00025 //                       - No dynamic resizing of memory. 
00026 //                       - "reshape(d0, d1, d2)" only sets dimensions. It does NOT affect memory allocation.
00027 
00028 #ifndef GLIFT_ARRAY_3_OWN_H_
00029 #define GLIFT_ARRAY_3_OWN_H_
00030 
00031 #include "arrayWrap3.h"
00032 #include <assert.h>
00033 
00034 namespace gutz {
00035 
00036 ////////////////////////////////////
00037 // ArrayOwn3
00038 ////////////////////////////////////
00039 
00040 template <class T>
00041 class arrayOwn3 : public arrayWrap3<T>
00042 {
00043 public:
00044         // Constructors - Deep copies of input data
00045     arrayOwn3 ();
00046     arrayOwn3 (int d0, int d1, int d2, T v ); 
00047         arrayOwn3 (int d0, int d1, int d3, const T* v);
00048         arrayOwn3 ( const arrayWrap3<T>& a );
00049 
00050         // Copy Constructor, Assignment Operator, and Destructor
00051     arrayOwn3 (const arrayOwn3<T>& a);
00052     arrayOwn3<T>& operator= (const arrayOwn3<T>& a);
00053     ~arrayOwn3() {killData();}
00054 
00055         // Transfer ownership of data ('v') to this object.
00056         // - Destroys previous contents of 'this' before doing shallow copy of new data
00057         // - 'killWithDelete' is true if 'v' is allocated with 'new', otherwise false ('malloc')
00058         void transfer( int d0, int d1, int d3, T* v, bool killWithDelete );
00059 
00060 private:
00061         void initVal (T v);
00062         void initData (const T* d);
00063     void copy (const arrayOwn3<T>& a);
00064 };
00065 
00066 ////////////////////////////////////
00067 // Implementation
00068 ////////////////////////////////////
00069 
00070 // Constructors
00071 template <class T>
00072 arrayOwn3<T>::arrayOwn3()
00073                                 : arrayWrap3<T>()
00074 {}
00075 
00076 template <class T>
00077 arrayOwn3<T>::arrayOwn3 (int d0, int d1, int d2, T val)
00078                                 : arrayWrap3<T>(d0,d1,d2,NULL)
00079 {
00080     arrayBase<T>::initValOwn( val );
00081 }
00082 
00083 template <class T>
00084 arrayOwn3<T>::arrayOwn3 (int d0, int d1, int d2, const T* data)
00085                                 : arrayWrap3<T>(d0,d1,d2,NULL)
00086 {
00087         arrayBase<T>::initDataOwn( data );
00088 }
00089 
00090 template <class T>
00091 arrayOwn3<T>::arrayOwn3 ( const arrayWrap3<T>& a ) : arrayWrap3<T>(a)
00092 { 
00093         arrayBase<T>::initDataOwn( a.data() );
00094 }
00095 
00096 // Copy Constructor
00097 template <class T>
00098 arrayOwn3<T>::arrayOwn3 (const arrayOwn3<T>& a) : arrayWrap3<T>(a)
00099                                                                                                  // Shallow copy of a
00100 {
00101         arrayBase<T>::initDataOwn(a.mData);                      // DEEP copy of a
00102 }
00103     
00104 // Assignment Operator
00105 template <class T>
00106 arrayOwn3<T>& arrayOwn3<T>::operator= (const arrayOwn3<T>& a) 
00107 { 
00108         if( &a != this ) {
00109                                 // Kill data and allocate new memory only if sizes don't match.
00110                 if( (mSize != a.size() ) || 
00111                         (dim(0) != a.dim(0)) ||
00112                         (dim(1) != a.dim(1)) ||
00113                         (dim(2) != a.dim(2)) ) {
00114                         killData();
00115                         arrayWrap3<T>::operator=(a);    // SHALLOW copy of a
00116                         arrayBase<T>::allocDataOwn();
00117                 }
00118                 arrayBase<T>::copyDataOwn(a.mData);     // Deep copy of a.mData
00119         }
00120         return *this; 
00121 }
00122 
00123 // Transfer ownership of data ('v') to this object.
00124 // - Destroys previous contents of 'this' before doing shallow copy of new data
00125 template <class T>
00126 void arrayOwn3<T>::transfer( int d0, int d1, int d2, T* v, bool killWithDelete )
00127 {
00128         killData();
00129         
00130         int sizes[3];
00131         sizes[0] = d0;
00132         sizes[1] = d1;
00133         sizes[2] = d2;
00134 
00135         set(3, sizes, v);
00136         mSlice = arrayWrap2<T>(d1,d2,v);
00137         mKillWithDelete = killWithDelete;
00138 }
00139 
00140 ////////////////////////////////////
00141 // Typedefs
00142 ////////////////////////////////////
00143 
00144 typedef arrayOwn3<char>         arrayo3c;
00145 typedef arrayOwn3<uchar>        arrayo3uc;
00146 typedef arrayOwn3<char>         arrayo3b;
00147 typedef arrayOwn3<uchar>        arrayo3ub;
00148 typedef arrayOwn3<short>        arrayo3s;
00149 typedef arrayOwn3<ushort>       arrayo3us;
00150 typedef arrayOwn3<int>          arrayo3i;
00151 typedef arrayOwn3<uint>         arrayo3ui;
00152 typedef arrayOwn3<long>         arrayo3l;
00153 typedef arrayOwn3<ulong>        arrayo3ul;
00154 typedef arrayOwn3<llong>        arrayo3ll;
00155 typedef arrayOwn3<ullong>       arrayo3ull;
00156 typedef arrayOwn3<float>        arrayo3f;
00157 typedef arrayOwn3<double>       arrayo3d;
00158 
00159 #ifdef USING_MATHGUTZ
00160         typedef arrayOwn3<vec2ub>       arrayo3v2ub;
00161         typedef arrayOwn3<vec2i>        arrayo3v2i;
00162         typedef arrayOwn3<vec2ui>       arrayo3v2ui;
00163         typedef arrayOwn3<vec2f>        arrayo3v2f;
00164 
00165         typedef arrayOwn3<vec3ub>       arrayo3v3ub;
00166         typedef arrayOwn3<vec3i>        arrayo3v3i;
00167         typedef arrayOwn3<vec3ui>       arrayo3v3ui;
00168         typedef arrayOwn3<vec3f>        arrayo3v3f;
00169 
00170         typedef arrayOwn3<vec4ub>       arrayo3v4ub;
00171         typedef arrayOwn3<vec4i>        arrayo3v4i;
00172         typedef arrayOwn3<vec4ui>       arrayo3v4ui;
00173         typedef arrayOwn3<vec4f>        arrayo3v4f;
00174 
00175         typedef arrayOwn3<mat2ub>       arrayo3m2ub;
00176         typedef arrayOwn3<mat2i>        arrayo3m2i;
00177         typedef arrayOwn3<mat2ui>       arrayo3m2ui;
00178         typedef arrayOwn3<mat2f>        arrayo3m2f;
00179 
00180         typedef arrayOwn3<mat3ub>       arrayo3m3ub;
00181         typedef arrayOwn3<mat3i>        arrayo3m3i;
00182         typedef arrayOwn3<mat3ui>       arrayo3m3ui;
00183         typedef arrayOwn3<mat3f>        arrayo3m3f;
00184 
00185         typedef arrayOwn3<mat4ub>       arrayo3m4ub;
00186         typedef arrayOwn3<mat4i>        arrayo3m4i;
00187         typedef arrayOwn3<mat4ui>       arrayo3m4ui;
00188         typedef arrayOwn3<mat4f>        arrayo3m4f;
00189 #endif
00190 
00191 } // End of namespace gutz
00192 
00193 #endif // arrayOwn3_h

Send questions, comments, and bug reports to:
jmk