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

arrayWrap2.h

Go to the documentation of this file.
00001 /*
00002  * $Id: arrayWrap2.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 //  arrayWrap2  - An Array WRAPPER class.
00024 //                      - This is a lightweight class that does NOT own its data.
00025 //                      - Copies of "arrayWrap2" 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) operator is just as fast a built-in C/C++ array.
00030 //                                      2) The [] (slice) operator is next fastest 
00031 //                                         - 8% overhead for 2D access via slices.
00032 //                                         - Only use slicing when you need it, otherwise use (i,j)!
00033 //                                      
00034 ////////////////////////////////////////////////////////////////////////
00035 
00036 #ifndef ARRAY_WRAP_2_h
00037 #define ARRAY_WRAP_2_h
00038 
00039 #include <../arrayGutz/arrayBase.h>
00040 #include <assert.h>
00041 #include "arrayWrap1.h"
00042 
00043 #ifdef USING_MATHGUTZ
00044         #include <mathGutz.h>
00045 #endif
00046 
00047 namespace gutz {
00048 
00049 // Forward Decl
00050 template <class T> class arrayWrap3;
00051 
00052 /////////////////////////
00053 // arrayWrap2
00054 /////////////////////////
00055 template <class T>
00056 class arrayWrap2 : public arrayBase<T>
00057 {
00058 public:
00059         friend class arrayWrap3<T>;
00060 
00061         // Constructors - Shallow wrap around data. No size checking!
00062         arrayWrap2();
00063         arrayWrap2( int d0, int d1, T* v=0 );
00064         arrayWrap2( const arrayWrap2<T>& a );
00065         arrayWrap2<T>& operator=( const arrayWrap2<T>& a );
00066 
00067   virtual ~arrayWrap2();
00068 
00069         // Return a slice of array
00070         inline arrayWrap1<T>&           operator[] (int i);
00071         inline const arrayWrap1<T>& operator[] (int i) const;
00072 
00073         // Return the element at data(i,j) - Faster than arr[i][j]
00074         inline T&                                       operator() (int i, int j);
00075         inline const T&                         operator() (int i, int j) const;
00076 
00077 protected:
00078                 
00079         // The slice wrapper used in the [] operator. 
00080         // Mutable so that it can be changed by a const operator
00081         mutable arrayWrap1<T>  mSlice;  
00082         
00083 private:
00084         inline void setSlice( int i ) const;                            // Set 'mSlice' to be the ith slice of 'mData'
00085         inline int  address(  int i, int j ) const;                     // Get the address of the data(i,j)'th element
00086         inline void setData( T* d ) { mData = d; }                              // Reset just data pointer
00087 };
00088 
00089 ////////////////////////////////////
00090 // Implementation
00091 ////////////////////////////////////
00092 
00093 // Constructors - All are shallow!!
00094 template <class T>
00095 arrayWrap2<T>::arrayWrap2 ()
00096 :arrayBase<T>(), mSlice(arrayWrap1<T>())
00097 {}
00098 
00099 
00100 template <class T>
00101 arrayWrap2<T>::arrayWrap2 (int d0, int d1, T* v)
00102 :arrayBase<T>(d0,d1,v), mSlice(arrayWrap1<T>(d1,v))
00103 {}
00104 
00105 // Copy Ctor
00106 template <class T>
00107 arrayWrap2<T>::arrayWrap2<T>( const arrayWrap2<T>& a ) : arrayBase<T>(a)
00108 {
00109         mSlice = a.mSlice;
00110 }
00111 
00112 // Assignment Op
00113 template <class T>
00114 arrayWrap2<T>& arrayWrap2<T>::operator=(const arrayWrap2<T>& a)
00115 {
00116         if( this != &a ) {
00117                 arrayBase<T>::operator=(a);// Call base class assign. op
00118                 mSlice = a.mSlice;
00119         }
00120         return *this;
00121 }
00122 
00123 // Destructor
00124 template <class T>
00125 arrayWrap2<T>::~arrayWrap2()
00126 {}
00127 
00128 // Accessing Operators
00129 template <class T>
00130 inline arrayWrap1<T>& arrayWrap2<T>::operator[] (int i)                                 
00131 { 
00132         setSlice(i);
00133         return mSlice;
00134 }
00135 
00136 template <class T>
00137 inline const arrayWrap1<T>& arrayWrap2<T>::operator[] (int i) const     
00138 { 
00139         setSlice(i);
00140         return mSlice;
00141 }
00142 
00143 // Set 'mSlice' to be the ith slice of 'mData'
00144 template <class T>
00145 inline void arrayWrap2<T>::setSlice( int i ) const
00146 {
00147         int a = i * mAxisStride[0]; 
00148         assert( a < mSize ); 
00149         mSlice.setData( mData + a );
00150 }
00151 
00152 template <class T>
00153 inline T& arrayWrap2<T>::operator() (int i, int j)
00154 {
00155         return mData[ address(i,j) ];
00156 }
00157 
00158 template <class T>
00159 inline const T& arrayWrap2<T>::operator() (int i, int j) const
00160 {
00161         return mData[ address(i,j) ];
00162 }
00163 
00164 template <class T>
00165 inline int arrayWrap2<T>::address( int i, int j) const 
00166 {
00167         return i*mAxisStride[0] + j;
00168 }
00169 
00170 
00171 ////////////////////////////////////
00172 // Typedefs
00173 ////////////////////////////////////
00174 
00175 typedef arrayWrap2<char>        arrayw2c;
00176 typedef arrayWrap2<uchar>       arrayw2uc;
00177 typedef arrayWrap2<char>        arrayw2b;
00178 typedef arrayWrap2<uchar>       arrayw2ub;
00179 typedef arrayWrap2<short>       arrayw2s;
00180 typedef arrayWrap2<ushort>      arrayw2us;
00181 typedef arrayWrap2<int>         arrayw2i;
00182 typedef arrayWrap2<uint>        arrayw2ui;
00183 typedef arrayWrap2<long>        arrayw2l;
00184 typedef arrayWrap2<ulong>       arrayw2ul;
00185 typedef arrayWrap2<llong>       arrayw2ll;
00186 typedef arrayWrap2<ullong>      arrayw2ull;
00187 typedef arrayWrap2<float>       arrayw2f;
00188 typedef arrayWrap2<double>      arrayw2d;
00189 
00190 #ifdef USING_MATHGUTZ
00191         typedef arrayWrap2<vec2ub> arrayw2v2ub;
00192         typedef arrayWrap2<vec2i>  arrayw2v2i;
00193         typedef arrayWrap2<vec2ui> arrayw2v2ui;
00194         typedef arrayWrap2<vec2f>  arrayw2v2f;
00195 
00196         typedef arrayWrap2<vec3ub> arrayw2v3ub;
00197         typedef arrayWrap2<vec3i>  arrayw2v3i;
00198         typedef arrayWrap2<vec3ui> arrayw2v3ui;
00199         typedef arrayWrap2<vec3f>  arrayw2v3f;
00200 
00201         typedef arrayWrap2<vec4ub> arrayw2v4ub;
00202         typedef arrayWrap2<vec4i>  arrayw2v4i;
00203         typedef arrayWrap2<vec4ui> arrayw2v4ui;
00204         typedef arrayWrap2<vec4f>  arrayw2v4f;
00205 
00206         typedef arrayWrap2<mat2ub> arrayw2m2ub;
00207         typedef arrayWrap2<mat2i>  arrayw2m2i;
00208         typedef arrayWrap2<mat2ui> arrayw2m2ui;
00209         typedef arrayWrap2<mat2f>  arrayw2m2f;
00210 
00211         typedef arrayWrap2<mat3ub> arrayw2m3ub;
00212         typedef arrayWrap2<mat3i>  arrayw2m3i;
00213         typedef arrayWrap2<mat3ui> arrayw2m3ui;
00214         typedef arrayWrap2<mat3f>  arrayw2m3f;
00215 
00216         typedef arrayWrap2<mat4ub> arrayw2m4ub;
00217         typedef arrayWrap2<mat4i>  arrayw2m4i;
00218         typedef arrayWrap2<mat4ui> arrayw2m4ui;
00219         typedef arrayWrap2<mat4f>  arrayw2m4f;
00220 #endif
00221 
00222 } // End of namespace gutz
00223 
00224 #endif // arrayWrap2_h

Send questions, comments, and bug reports to:
jmk