cgma
VGArray.cpp
Go to the documentation of this file.
00001 
00002 #include <algorithm>
00003 #include "VGArray.hpp"
00004 #include "CubitDefines.h"
00005 
00006 template <class T>
00007 VGArray<T>::VGArray( int init_size )
00008 {
00009   if( init_size <= 0 )
00010   {
00011     storage_ = 0;
00012     size_ = 0;
00013     data_ = 0;
00014   }
00015   else
00016   {
00017     size_ = init_size;
00018     storage_ = storage( init_size );
00019     data_ = new T[storage_];
00020   }
00021 }
00022 
00023 template <class T>
00024 VGArray<T>::~VGArray( )
00025 {
00026   delete [] data_;
00027   storage_ = size_ = 0;
00028   data_ = 0;
00029 }
00030 
00031 template <class T>
00032 void VGArray<T>::size( int new_size )
00033 {
00034   if( new_size <= 0 ) 
00035   {
00036     new_size = 0;
00037   }
00038   else if( new_size > storage_ )
00039   {
00040     int new_stor = storage(new_size);
00041     assert( new_stor > storage_ );
00042     T* new_array = new T[new_stor];
00043     if( data_ )
00044     {
00045       memcpy( new_array, data_, size_ * sizeof(T) );
00046       delete [] data_;
00047     }
00048     data_ = new_array;
00049     storage_ = new_stor;
00050   }
00051   size_ = new_size;
00052 }
00053 
00054 template <class T>
00055 void VGArray<T>::size_end( int new_size )
00056 {
00057   if( new_size <= 0 ) 
00058   {
00059     new_size = 0;
00060   }
00061   else if( new_size > storage_ )
00062   {
00063     int new_stor = storage(new_size);
00064     assert( new_stor > storage_ );
00065     T* new_array = new T[new_stor];
00066     if( data_ )
00067     {
00068       memcpy( new_array + new_size - size_, data_, size_ * sizeof(T) );
00069       delete [] data_;
00070     }
00071     data_ = new_array;
00072     storage_ = new_stor;
00073   }
00074   else if (new_size > size_)
00075   {
00076     T* read = data_ + size_;
00077     T* write = data_ + new_size;
00078     while ( read > data_ )
00079       *(--write) = *(--read);
00080   }
00081   else if (new_size < size_)
00082   {
00083     T* read = data_ + size_ - new_size;
00084     T* write = data_;
00085     T* end = data_ + size_;
00086     while ( read < end )
00087       *(write--) = *(read--);
00088   }
00089   size_ = new_size;
00090 }
00091 
00092 template <class T>
00093 int VGArray<T>::storage( int size )
00094 {
00095   assert( size <= 0x40000000 ); //no power of 2 greater than size (overflow)
00096 
00097   int result;
00098   for( result = 1; result < size; result = result << 1 );
00099   assert(result>=size);
00100   return result;
00101 }
00102 
00103 template <class T>
00104 void VGArray<T>::remove( int index )
00105 {
00106   assert( index >= 0 && index < size_ );
00107   size_--;
00108   T* ptr = data_ + index;
00109   T* end = data_ + size_;
00110   while( ptr < end )
00111   {
00112     T* next = ptr + 1;
00113     *ptr = *next;
00114     ptr = next;
00115   }
00116 }
00117 
00118 template <class T>
00119 void VGArray<T>::insert( const T& entry, int index )
00120 {
00121   assert( index >= 0 && index <= size_ );
00122 
00123   size( size_ + 1 );
00124   T* ptr = data_ + size_ - 1;
00125   T* end = data_ + index;
00126 
00127   while( ptr > end )
00128   {
00129     T* next = ptr - 1;
00130     *ptr = *next;
00131     ptr = next;
00132   }
00133 
00134   data_[index] = entry;
00135 }
00136 
00137 template <class T>
00138 int VGArray<T>::find( const T& entry, int search_from ) const
00139 {
00140   assert( search_from >= 0 );
00141   
00142   if( data_ )
00143   {
00144     T* ptr = data_ + search_from;
00145     T* end = data_ + size_;
00146     for(; ptr < end; ptr++ )
00147     {
00148       if( *ptr == entry )
00149         return ptr - data_;
00150     }
00151   }
00152   return -1;
00153 }
00154 
00155 template <class T>
00156 void VGArray<T>::reverse()
00157 {
00158   T* start = data_;
00159   T* end   = data_ + size_ - 1;
00160   while (start < end)
00161   {
00162     std::swap(*start,*end);
00163     start++;
00164     end--;
00165   }
00166 }
00167 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines