proj home

Files   Classes   Functions   Hierarchy  

dumbarray< T > Class Template Reference

Array of pointers to elements accessed by overloaded [] operator. More...

#include <dumbarray.h>

Inheritance diagram for dumbarray< T >:
Collaboration diagram for dumbarray< T >:

List of all members.

Public Member Functions

 dumbarray (dumbarray< T > const &x)
 Copy without passing ownership of xi.
 dumbarray ()
 Default - do something later constructor.
 dumbarray (T **xi_, uintc N_, boolc memoryManaged_)
 The client initializes xi and can pass ownership to this class.
 ~dumbarray ()
 Cleanup if requested.
 dumbarray (uintc N_)
 Create a N dimensional array of T* pointers.
void create (uintc N_)
 Create a N dimensional array of T* pointers.
void clean ()
 If memory allocated delete it.
void operator= (dumbarray< T > const &x)
 Memory ownership of xi is not transfered.
Toperator[] (uintc i)
 Access an element and can modify value.
T const operator[] (uintc i) const
 Access an element.
T *& operator() (uint i)
 Access a pointer to an element and can modify the pointers value.

Public Attributes

T ** xi
 Array of pointers.
uint N
 The number of dimensions.
bool memoryManaged
 Is this class managing the xi array?


Detailed Description

template<typename T>
class dumbarray< T >

Array of pointers to elements accessed by overloaded [] operator.

The direct analogy is an array of pointers to the objects. However there is no need for contiguous memory.

It is a smart pointer for a few reasons, however I call it a dumb array because each element access de references a pointer as compared with the usual array access on contiguous memory. Roughly doubling the access time.

The client can manage the memory directly. Assignment does not pass control of xi because in the end someone has to clean the memory up and there can only be one de allocation point. (This is a more primitive technique than reference counting and can have dangling pointers)

Examples
  dumbarray<double> vi(3);
  cout << "Set the pointers" << endl;
  vi(0) = & a;
  vi(1) = & b;
  vi(2) = & c;
  ...
  cout << "Access an element" << endl;
  vi[2] = 2.5;  // c=2.5

  cout << "Have multiple arrays to same array" << endl; 
  dumbarray<double> v2();
  v2 = vi;
  y= v2[1];  // y=b

Definition at line 40 of file dumbarray.h.


Constructor & Destructor Documentation

template<typename T>
dumbarray< T >::dumbarray ( dumbarray< T > const &  x  )  [inline]

Copy without passing ownership of xi.

Definition at line 130 of file dumbarray.h.

References dumbarray< T >::memoryManaged, dumbarray< T >::N, and dumbarray< T >::xi.

00131 {
00132   xi = x.xi;
00133   N = x.N;
00134   memoryManaged = false;
00135 }

template<typename T>
dumbarray< T >::dumbarray (  )  [inline]

Default - do something later constructor.

Definition at line 59 of file dumbarray.h.

00060     : xi(0), N(0), memoryManaged(false) {}

template<typename T>
dumbarray< T >::dumbarray ( T **  xi_,
uintc  N_,
boolc  memoryManaged_ 
) [inline]

The client initializes xi and can pass ownership to this class.

Definition at line 65 of file dumbarray.h.

00070     : xi(xi_), N(N_), memoryManaged(memoryManaged_) {}

template<typename T >
dumbarray< T >::~dumbarray (  )  [inline]

Cleanup if requested.

Definition at line 116 of file dumbarray.h.

References dumbarray< T >::clean().

00117 {
00118   clean();
00119 }

template<typename T>
dumbarray< T >::dumbarray ( uintc  N_  )  [inline]

Create a N dimensional array of T* pointers.

Definition at line 123 of file dumbarray.h.

References dumbarray< T >::create().

00124   : xi(0), memoryManaged(false)
00125 {
00126   create(N_);
00127 }


Member Function Documentation

template<typename T >
void dumbarray< T >::clean (  )  [inline]

If memory allocated delete it.

Definition at line 153 of file dumbarray.h.

References dumbarray< T >::memoryManaged, dumbarray< T >::N, and dumbarray< T >::xi.

Referenced by dumbarray< T >::create(), dumbarray< T >::operator=(), and dumbarray< T >::~dumbarray().

00154 {
00155   if (memoryManaged) 
00156     delete[] xi;
00157   xi=0;
00158   N=0;
00159 }

template<typename T >
void dumbarray< T >::create ( uintc  N_  )  [inline]

Create a N dimensional array of T* pointers.

Definition at line 107 of file dumbarray.h.

References dumbarray< T >::clean(), dumbarray< T >::memoryManaged, dumbarray< T >::N, and dumbarray< T >::xi.

Referenced by dumbarray< T >::dumbarray().

00108 {
00109   clean();
00110   N = N_;
00111   xi = new T*[N];
00112   memoryManaged=true;
00113 }

template<typename T>
T* & dumbarray< T >::operator() ( uint  i  )  [inline]

Access a pointer to an element and can modify the pointers value.

Definition at line 97 of file dumbarray.h.

00098     { return xi[i]; }

template<typename T>
void dumbarray< T >::operator= ( dumbarray< T > const &  x  )  [inline]

Memory ownership of xi is not transfered.

Definition at line 138 of file dumbarray.h.

References dumbarray< T >::clean(), dumbarray< T >::memoryManaged, dumbarray< T >::N, and dumbarray< T >::xi.

00139 {
00140   if (&x==this)
00141     return;
00142 
00143   clean();
00144 
00145   xi = x.xi;
00146   N = x.N;
00147   memoryManaged = false;
00148 }

template<typename T>
T const dumbarray< T >::operator[] ( uintc  i  )  const [inline]

Access an element.

Definition at line 93 of file dumbarray.h.

00094     { return *xi[i]; }

template<typename T>
T& dumbarray< T >::operator[] ( uintc  i  )  [inline]

Access an element and can modify value.

Definition at line 89 of file dumbarray.h.

00090     { return *xi[i]; }


Member Data Documentation

template<typename T>
bool dumbarray< T >::memoryManaged

Is this class managing the xi array?

Definition at line 53 of file dumbarray.h.

Referenced by dumbarray< T >::clean(), dumbarray< T >::create(), dumbarray< T >::dumbarray(), and dumbarray< T >::operator=().

template<typename T>
uint dumbarray< T >::N

The number of dimensions.

Definition at line 50 of file dumbarray.h.

Referenced by dumbarray< T >::clean(), dumbarray< T >::create(), dumbarray< T >::dumbarray(), and dumbarray< T >::operator=().

template<typename T>
T** dumbarray< T >::xi

Array of pointers.

To modify the pointer the client writes to xi. To modify the value use the [] operator.

Definition at line 47 of file dumbarray.h.

Referenced by dumbarray< T >::clean(), dumbarray< T >::create(), dumbarray< T >::dumbarray(), dumbarray< double >::operator()(), dumbarray< T >::operator=(), and dumbarray< double >::operator[]().


The documentation for this class was generated from the following file:

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