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

arrayWrap4.h

Go to the documentation of this file.
00001 /*
00002  * $Id: arrayWrap4.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 ////////////////////////////////////////////////////////////////////////
00019 // 9/8/02       Aaron Lefohn    Scientific Computing and Imaging Institute
00020 //                      Joe M. Kniss    School of Computing
00021 //                                                      University of Utah
00022 //
00023 //  arrayWrap4  - An Array WRAPPER class.
00024 //                      - This is a lightweight class that does NOT own its data.
00025 //                      - Copies of "arrayWrap4" objects are shallow.
00026 //                      - The constructor does NOT allocate any memory.
00027 //                              - The destructor does NOT free the memory.
00028 //                              - Accessors (In order of increasing overhead)
00029 //                                      1) The (i,j,k,m) operator is just as fast a built-in C/C++ array.
00030 //                                      2) The [] (slice) operator is next fastest 
00031 //                                         - 8% overhead for 1 level of slice
00032 //                                         - 20% overhead for 4D access via slices.
00033 //                                         - Only use slicing when you need it, otherwise use (i,j,k,m)!
00034 //
00035 ////////////////////////////////////////////////////////////////////////
00036 
00037 #ifndef ARRAY_WRAP_4_h
00038 #define ARRAY_WRAP_4_h
00039 
00040 #include <../arrayGutz/arrayBase.h>
00041 #include <assert.h>
00042 #include "arrayWrap2.h"
00043 
00044 namespace gutz {
00045 
00046 // Forward Decl
00047 template <class T> class arrayWrap5;
00048 
00049 /////////////////////////
00050 // arrayWrap4
00051 /////////////////////////
00052 template <class T>
00053 class arrayWrap4 : public arrayBase<T>
00054 {
00055 public:
00056         friend class arrayWrap5<T>;
00057         
00058         // Constructors - Shallow wrap around data. No size checking!
00059         arrayWrap4();
00060         arrayWrap4( int d0, int d1, int d2, int d3, T* v );
00061         arrayWrap4( const arrayWrap4<T>& a );
00062         arrayWrap4<T>& operator=( const arrayWrap4<T>& a );
00063 
00064     virtual ~arrayWrap4();
00065 
00066         // Return a slice of data - Optimized by returning reference to arrayWrap3<T> member.
00067         inline arrayWrap3<T>&           operator[] (int i);
00068         inline const arrayWrap3<T>& operator[] (int i) const;
00069 
00070         // Return the element at data(i,j,k,m) - Faster than arr[i][j][k][m]
00071         inline T&                                       operator() (int i, int j, int k, int m);
00072         inline const T&                         operator() (int i, int j, int k, int m) const;
00073 
00074 protected:
00075 
00076         // The slice wrapper used in the [] operator. 
00077         // Mutable so that it can be changed by a const operator
00078         mutable arrayWrap3<T>  mSlice;  
00079         
00080 private:
00081         inline void setSlice( int i ) const;                                     // Set 'mSlice' to the ith slice of 'mData'
00082         inline int  address(  int i, int j, int k, int m ) const;// Get the address of the data(i,j)'th element
00083         inline void setData( T* d ) { mData = d; }                                       // Reset just data pointer
00084 };
00085 
00086 ////////////////////////////////////
00087 // Implementation
00088 ////////////////////////////////////
00089 
00090 // Constructors - All are shallow!!
00091 template <class T>
00092 arrayWrap4<T>::arrayWrap4 ()
00093 :arrayBase<T>(), mSlice(arrayWrap3<T>())
00094 {}
00095 
00096 template <class T>
00097 arrayWrap4<T>::arrayWrap4 (int d0, int d1, int d2, int d3, T* v)
00098 :arrayBase<T>(d0,d1,d2,d3,v), mSlice(arrayWrap3<T>(d1,d2,d3,v))
00099 {}
00100 
00101 // Copy Ctor
00102 template <class T>
00103 arrayWrap4<T>::arrayWrap4<T>( const arrayWrap4<T>& a ) : arrayBase<T>(a)
00104 {
00105         mSlice = a.mSlice;
00106 }
00107 
00108 // Assignment Op
00109 template <class T>
00110 arrayWrap4<T>& arrayWrap4<T>::operator=(const arrayWrap4<T>& a)
00111 {
00112         if( this != &a ) {
00113                 arrayBase<T>::operator=(a);// Call base class assign. op
00114                 mSlice = a.mSlice;
00115         }
00116         return *this;
00117 }
00118 
00119 // Destructor
00120 template <class T>
00121 arrayWrap4<T>::~arrayWrap4()
00122 {}
00123 
00124 // Accessing Operators
00125 template <class T>
00126 inline arrayWrap3<T>& arrayWrap4<T>::operator[] (int i)                                 
00127 { 
00128         setSlice(i);
00129         return mSlice; 
00130 }
00131 
00132 template <class T>
00133 inline const arrayWrap3<T>& arrayWrap4<T>::operator[] (int i) const     
00134 { 
00135         setSlice(i);
00136         return mSlice; 
00137 }
00138 
00139 template <class T>
00140 inline T& arrayWrap4<T>::operator() (int i, int j, int k, int m)
00141 {
00142         return mData[ address(i,j,k,m) ];
00143 }
00144 
00145 template <class T>
00146 inline const T& arrayWrap4<T>::operator() (int i, int j, int k, int m) const
00147 {
00148         return mData[ address(i,j,k,m) ];
00149 }
00150 
00151 template <class T>
00152 inline int arrayWrap4<T>::address( int i, int j, int k, int m) const
00153 {
00154         //return i*mAxisDim[3]*mAxisDim[2]*mAxisDim[1] + j*mAxisDim[3]*mAxisDim[2] + k*mAxisDim[3] + m;
00155         return i*mAxisStride[0] + j*mAxisStride[1] + k*mAxisStride[2] + m;
00156 }
00157 
00158 // Set 'mSlice' to be the ith slice of 'mData'
00159 template <class T>
00160 inline void arrayWrap4<T>::setSlice( int i ) const
00161 {
00162         //int a = i * mAxisDim[1] * mAxisDim[2] * mAxisDim[3];
00163         int a = i * mAxisStride[0];
00164         assert( a < mSize );
00165         mSlice.setData( mData + a );
00166 }
00167 
00168 ////////////////////////////////////
00169 // Typedefs
00170 ////////////////////////////////////
00171 
00172 typedef arrayWrap4<char>        arrayw4c;
00173 typedef arrayWrap4<uchar>       arrayw4uc;
00174 typedef arrayWrap4<char>        arrayw4b;
00175 typedef arrayWrap4<uchar>       arrayw4ub;
00176 typedef arrayWrap4<short>       arrayw4s;
00177 typedef arrayWrap4<ushort>      arrayw4us;
00178 typedef arrayWrap4<int>         arrayw4i;
00179 typedef arrayWrap4<uint>        arrayw4ui;
00180 typedef arrayWrap4<long>        arrayw4l;
00181 typedef arrayWrap4<ulong>       arrayw4ul;
00182 typedef arrayWrap4<llong>       arrayw4ll;
00183 typedef arrayWrap4<ullong>      arrayw4ull;
00184 typedef arrayWrap4<float>       arrayw4f;
00185 typedef arrayWrap4<double>      arrayw4d;
00186 
00187 #ifdef USING_MATHGUTZ
00188         typedef arrayWrap4<vec2ub> arrayw4v2ub;
00189         typedef arrayWrap4<vec2i>  arrayw4v2i;
00190         typedef arrayWrap4<vec2ui> arrayw4v2ui;
00191         typedef arrayWrap4<vec2f>  arrayw4v2f;
00192 
00193         typedef arrayWrap4<vec3ub> arrayw4v3ub;
00194         typedef arrayWrap4<vec3i>  arrayw4v3i;
00195         typedef arrayWrap4<vec3ui> arrayw4v3ui;
00196         typedef arrayWrap4<vec3f>  arrayw4v3f;
00197 
00198         typedef arrayWrap4<vec4ub> arrayw4v4ub;
00199         typedef arrayWrap4<vec4i>  arrayw4v4i;
00200         typedef arrayWrap4<vec4ui> arrayw4v4ui;
00201         typedef arrayWrap4<vec4f>  arrayw4v4f;
00202 
00203         typedef arrayWrap4<mat2ub> arrayw4m2ub;
00204         typedef arrayWrap4<mat2i>  arrayw4m2i;
00205         typedef arrayWrap4<mat2ui> arrayw4m2ui;
00206         typedef arrayWrap4<mat2f>  arrayw4m2f;
00207 
00208         typedef arrayWrap4<mat3ub> arrayw4m3ub;
00209         typedef arrayWrap4<mat3i>  arrayw4m3i;
00210         typedef arrayWrap4<mat3ui> arrayw4m3ui;
00211         typedef arrayWrap4<mat3f>  arrayw4m3f;
00212 
00213         typedef arrayWrap4<mat4ub> arrayw4m4ub;
00214         typedef arrayWrap4<mat4i>  arrayw4m4i;
00215         typedef arrayWrap4<mat4ui> arrayw4m4ui;
00216         typedef arrayWrap4<mat4f>  arrayw4m4f;
00217 #endif
00218 
00219 } // End of namespace gutz
00220 
00221 #endif // arrayWrap4_h

Send questions, comments, and bug reports to:
jmk