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

arrayOwn2.h

Go to the documentation of this file.
00001 /*
00002  * $Id: arrayOwn2.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 // arrayOwn2 - A lightweight 2D 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(dim0,dim1)" only sets dimensions. It does NOT affect memory allocation.
00027 
00028 #ifndef GLIFT_ARRAY_2_OWN_H_
00029 #define GLIFT_ARRAY_2_OWN_H_
00030 
00031 #include "arrayWrap2.h"
00032 #include <assert.h>
00033 
00034 namespace gutz {
00035 
00036 ////////////////////////////////////
00037 // ArrayOwn2
00038 ////////////////////////////////////
00039 
00040 template <class T>
00041 class arrayOwn2 : public arrayWrap2<T>
00042 {
00043 public:
00044         // Constructors - Deep copies of input data
00045     arrayOwn2 ();
00046     arrayOwn2 (int d0, int d1, T v ); 
00047         arrayOwn2 (int d0, int d1, const T* v);
00048         arrayOwn2 ( const arrayWrap2<T>& a );
00049 
00050         // Copy Constructor, Assignment Operator, and Destructor
00051     arrayOwn2 (const arrayOwn2<T>& a);
00052     arrayOwn2<T>& operator= (const arrayOwn2<T>& a);
00053     ~arrayOwn2() {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, T* v, bool killWithDelete );
00059 
00060 private:
00061         void initVal (T v);
00062         void initData (const T* d);
00063     void copy (const arrayOwn2<T>& a);
00064 };
00065 
00066 ////////////////////////////////////
00067 // Implementation
00068 ////////////////////////////////////
00069 
00070 // Constructors
00071 template <class T>
00072 arrayOwn2<T>::arrayOwn2()
00073                            : arrayWrap2<T>()
00074 {}
00075 
00076 template <class T>
00077 arrayOwn2<T>::arrayOwn2 (int d0, int d1, T val)
00078                            : arrayWrap2<T>(d0,d1,NULL)
00079 {
00080     arrayBase<T>::initValOwn( val );
00081 }
00082 
00083 template <class T>
00084 arrayOwn2<T>::arrayOwn2 (int d0, int d1, const T* data)
00085                            : arrayWrap2<T>(d0,d1,NULL)
00086 {
00087         arrayBase<T>::initDataOwn( data );
00088 }
00089 
00090 template <class T>
00091 arrayOwn2<T>::arrayOwn2 ( const arrayWrap2<T>& a ) : arrayWrap2<T>(a)
00092 {
00093         arrayBase<T>::initDataOwn( a.data() );
00094 }
00095 
00096 // Copy Constructor: "this" does NOT exist yet
00097 template <class T>
00098 arrayOwn2<T>::arrayOwn2( const arrayOwn2<T>& a ) : arrayWrap2<T>(a)
00099                                                                                                  // SHALLOW copy 'a'
00100 {
00101         arrayBase<T>::initDataOwn(a.mData);                      // DEEP copy of a
00102 }
00103 
00104 // Assignment operator: "this" DOES exist
00105 template <class T>
00106 arrayOwn2<T>& arrayOwn2<T>::operator=( const arrayOwn2<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                         killData();
00114                         arrayWrap2<T>::operator=(a);    // SHALLOW copy of a
00115                         arrayBase<T>::allocDataOwn();
00116                 }
00117                 arrayBase<T>::copyDataOwn(a.mData);     // Deep copy of a.mData
00118         }
00119         return *this; 
00120 }
00121 
00122 // Transfer ownership of data ('v') to this object.
00123 // - Destroys previous contents of 'this' before doing shallow copy of new data
00124 template <class T>
00125 void arrayOwn2<T>::transfer( int d0, int d1, T* v, bool killWithDelete )
00126 {
00127         killData();
00128         
00129         int sizes[2];
00130         sizes[0] = d0;
00131         sizes[1] = d1;
00132 
00133         set(2, sizes, v);
00134         mSlice = arrayWrap1<T>(d1,v);
00135         mKillWithDelete = killWithDelete;
00136 }
00137 
00138 ////////////////////////////////////
00139 // Typedefs
00140 ////////////////////////////////////
00141 
00142 typedef arrayOwn2<char>         arrayo2c;
00143 typedef arrayOwn2<uchar>        arrayo2uc;
00144 typedef arrayOwn2<char>         arrayo2b;
00145 typedef arrayOwn2<uchar>        arrayo2ub;
00146 typedef arrayOwn2<short>        arrayo2s;
00147 typedef arrayOwn2<ushort>       arrayo2us;
00148 typedef arrayOwn2<int>          arrayo2i;
00149 typedef arrayOwn2<uint>         arrayo2ui;
00150 typedef arrayOwn2<long>         arrayo2l;
00151 typedef arrayOwn2<ulong>        arrayo2ul;
00152 typedef arrayOwn2<llong>        arrayo2ll;
00153 typedef arrayOwn2<ullong>       arrayo2ull;
00154 typedef arrayOwn2<float>        arrayo2f;
00155 typedef arrayOwn2<double>       arrayo2d;
00156 
00157 #ifdef USING_MATHGUTZ
00158         typedef arrayOwn2<vec2ub> arrayo2v2ub;
00159         typedef arrayOwn2<vec2i>  arrayo2v2i;
00160         typedef arrayOwn2<vec2ui> arrayo2v2ui;
00161         typedef arrayOwn2<vec2f>  arrayo2v2f;
00162 
00163         typedef arrayOwn2<vec3ub> arrayo2v3ub;
00164         typedef arrayOwn2<vec3i>  arrayo2v3i;
00165         typedef arrayOwn2<vec3ui> arrayo2v3ui;
00166         typedef arrayOwn2<vec3f>  arrayo2v3f;
00167 
00168         typedef arrayOwn2<vec4ub> arrayo2v4ub;
00169         typedef arrayOwn2<vec4i>  arrayo2v4i;
00170         typedef arrayOwn2<vec4ui> arrayo2v4ui;
00171         typedef arrayOwn2<vec4f>  arrayo2v4f;
00172 
00173         typedef arrayOwn2<mat2ub> arrayo2m2ub;
00174         typedef arrayOwn2<mat2i>  arrayo2m2i;
00175         typedef arrayOwn2<mat2ui> arrayo2m2ui;
00176         typedef arrayOwn2<mat2f>  arrayo2m2f;
00177 
00178         typedef arrayOwn2<mat3ub> arrayo2m3ub;
00179         typedef arrayOwn2<mat3i>  arrayo2m3i;
00180         typedef arrayOwn2<mat3ui> arrayo2m3ui;
00181         typedef arrayOwn2<mat3f>  arrayo2m3f;
00182 
00183         typedef arrayOwn2<mat4ub> arrayo2m4ub;
00184         typedef arrayOwn2<mat4i>  arrayo2m4i;
00185         typedef arrayOwn2<mat4ui> arrayo2m4ui;
00186         typedef arrayOwn2<mat4f>  arrayo2m4f;
00187 #endif
00188 
00189 } // End of namespace gutz
00190 
00191 #endif // arrayOwn2_h

Send questions, comments, and bug reports to:
jmk