cgma
VGArray.hpp
Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // Purpose       : Simple array template
00003 //
00004 // Special Notes : 
00005 //
00006 // Creator       : Jason Kraftcheck
00007 //
00008 // Creation Date : 01/11/02
00009 //-------------------------------------------------------------------------
00010 
00011 #ifndef VG_ARRAY_HPP
00012 #define VG_ARRAY_HPP
00013 
00014 #include <assert.h>
00015 
00016 template <class T> class VGArray
00017 {
00018   public:
00019   
00020     VGArray( int initial_size = 0 );
00021     
00022     ~VGArray();
00023     
00024     inline int size() const;
00025     // get current array size
00026     
00027     void size( int new_size );
00028     // increase or decrease size of array
00029     
00030     void size_end( int new_size );
00031     // increase or decrease size, populating the 
00032     // new array begining with the last element at the
00033     // previous size in the last slot of the new size,
00034     // and filling the array in decreasing index order.
00035     
00036     inline void push( const T& entry );
00037     // increase array size by 1 and add entry at last index
00038     
00039     inline const T& pop();
00040     // decrease array size by 1 and return removed entry
00041     
00042     inline T& operator[]( int index );
00043     
00044     inline const T& operator[]( int index ) const;
00045     
00046     void remove( int index );
00047     // remove entry at index and shift all higher indices down
00048     
00049     void insert( const T& entry, int index );
00050     // insert entry at specified index, shifting all higher indices up
00051     
00052     int find( const T& entry, int search_from = 0 ) const;
00053     // find the index of an occurance of the passed entry
00054     // in the array.  search starts from search_from and
00055     // ends at the last index.  -1 is returned if no match
00056     // is found.
00057     
00058     void reverse();
00059     // Reverse order of list.
00060     
00061   private:
00062   
00063     const VGArray& operator=( const VGArray<T>& );
00064     VGArray( const VGArray<T>& );
00065       //- do not allow assignment
00066   
00067     static int storage( int size );
00068       //- calculate storage to allocate for specified size
00069       //- (smallest power-2 greater than size)
00070   
00071     T* data_;
00072     int storage_;
00073     int size_;
00074 };
00075 
00076 #include "VGArray.cpp"
00077 
00078 template <class T> inline
00079 int VGArray<T>::size() const
00080   { return size_; }
00081 
00082 template <class T> inline
00083 T& VGArray<T>::operator[]( int index )
00084 {
00085   assert( index >= 0 && index < size_ );
00086   return data_[index];
00087 }
00088 
00089 template <class T> inline
00090 const T& VGArray<T>::operator[]( int index ) const
00091 {
00092   assert( index >= 0 && index < size_ );
00093   return data_[index];
00094 }
00095   
00096 template <class T> inline
00097 void VGArray<T>::push( const T& entry )
00098 {
00099   int s = size_;
00100   size( s + 1 );
00101   data_[s] = entry;
00102 }
00103 
00104 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines