proj home

Files   Classes   Functions   Hierarchy  

dumbarray.h

Go to the documentation of this file.
00001 #ifndef DUMBARRAY_H
00002 #define DUMBARRAY_H
00003 
00004 #include <typedefs.h>
00005 
00006 
00039 template< typename T>
00040 class dumbarray
00041 {
00042 public:
00043 
00047   T ** xi;
00048 
00050   uint N;
00051 
00053   bool memoryManaged;
00054 
00056   dumbarray(dumbarray<T> const & x);
00057 
00059   dumbarray()
00060     : xi(0), N(0), memoryManaged(false) {}
00061 
00064   dumbarray
00065   (
00066     T ** xi_, 
00067     uintc N_,
00068     boolc memoryManaged_
00069   )
00070     : xi(xi_), N(N_), memoryManaged(memoryManaged_) {}
00071 
00073   ~dumbarray();
00074 
00076   dumbarray(uintc N_);
00077 
00079   void create(uintc N_);
00080 
00082   void clean();
00083 
00085   void operator = ( dumbarray<T> const & x );
00086 
00087  
00089   T & operator[](uintc i)
00090     { return *xi[i]; }
00091 
00093   T const operator[](uintc i) const
00094     { return *xi[i]; }
00095 
00097   T * & operator () (uint i)
00098     { return xi[i]; }
00099 
00100 };
00101 
00102 
00103 //----------------------------------------------------------
00104 //  Implementation
00105 
00106 template< typename T>
00107 void dumbarray<T>::create(uintc N_)
00108 {
00109   clean();
00110   N = N_;
00111   xi = new T*[N];
00112   memoryManaged=true;
00113 }
00114 
00115 template< typename T>
00116 dumbarray<T>::~dumbarray()
00117 {
00118   clean();
00119 }
00120 
00121 
00122 template< typename T>
00123 dumbarray<T>::dumbarray(uintc N_)
00124   : xi(0), memoryManaged(false)
00125 {
00126   create(N_);
00127 }
00128 
00129 template< typename T>
00130 dumbarray<T>::dumbarray(dumbarray<T> const & x)
00131 {
00132   xi = x.xi;
00133   N = x.N;
00134   memoryManaged = false;
00135 }
00136 
00137 template< typename T>
00138 void dumbarray<T>::operator = ( dumbarray<T> const & x )
00139 {
00140   if (&x==this)
00141     return;
00142 
00143   clean();
00144 
00145   xi = x.xi;
00146   N = x.N;
00147   memoryManaged = false;
00148 }
00149 
00150 
00151 
00152 template< typename T>
00153 void dumbarray<T>::clean()
00154 {
00155   if (memoryManaged) 
00156     delete[] xi;
00157   xi=0;
00158   N=0;
00159 }
00160 
00161 
00162 
00163 /*
00164 
00165   Purpose
00166 
00167   Why was this class written?  To plug into templates so
00168   an array could be accessed as a contiguous array but the
00169   elements are stored anywhere.
00170 
00171   This solves an fundamental problem between C and OOP.  Lets say two
00172   objects A and B process information in X variables.  If there is no
00173   global structure which both A and B see then how do they process
00174   the data efficiently?  The data could be copied. For example A processes
00175   X and copies it to B. B processes X. A and B are completely separate
00176   (which is good).
00177 
00178   OOP has a problem that C does not.  Data is locked up in objects which
00179   are not an array of contiguous memory. Further for efficiency the 
00180   processing of data is associated with its locality. 
00181   With this class this information
00182   can be formed into a contiguous memory (through the underneath pointer).
00183   
00184   Remember that in the example processing in A and B is both intensive.
00185   So if A owns the data then B will have to bend in some way to accomadate
00186   A.  Where redesignes are not practical the objects in B have the classes
00187   access A's data by the dumb array. 
00188 
00189 */
00190 
00191 #endif
00192 
00193 

Generated on Fri Mar 4 00:49:29 2011 for Chelton Evans Source by  doxygen 1.5.8