00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef KYRA_DYNARRAY_INCLUDED
00030 #define KYRA_DYNARRAY_INCLUDED
00031
00032 #include <limits.h>
00033 #include "glutil.h"
00034 #include "gldebug.h"
00035
00044 template <class T>
00045 class GlDynArray
00046 {
00047 public:
00048 GlDynArray( int _size ) { size = 0; count = 0; data = 0; Resize( _size ); }
00049 GlDynArray() { size = 0; count = 0; data = 0; }
00050 ~GlDynArray() { delete [] data; }
00051
00052 GlDynArray( const GlDynArray<T>& copy ) { size = 0; count = 0; data = 0; this->SetEqual( copy ); }
00053 void operator=( const GlDynArray<T>& copy ) { this->SetEqual( copy ); }
00054
00055 unsigned NotFound() const { return UINT_MAX; }
00056 bool Empty() const { return count == 0; }
00057
00058 void Clear() { delete [] data; size = 0; count = 0; data = 0; }
00059
00061 T& operator[] ( unsigned i ) const { GLASSERT( i<count );
00062 GLASSERT( i>=0 );
00063 return data[i]; }
00065 T* Memory() { return data; }
00066
00067 void SetEqual( const GlDynArray<T>& rhs )
00068 {
00069 delete [] data;
00070 data = new T[ rhs.size ];
00071 for ( unsigned i=0; i<rhs.count; i++ )
00072 {
00073 data[i] = rhs.data[i];
00074 }
00075 size = rhs.size;
00076 count = rhs.count;
00077 }
00078
00080 T Item( unsigned i ) const { GLASSERT( i<count );
00081 return data[i]; }
00083 T* ItemPointer( unsigned i ) const { GLASSERT( i<count );
00084 return &data[i]; }
00086 void SetItem( unsigned i, const T& t )
00087 { GLASSERT( i<count );
00088 data[i] = t; }
00089
00092 unsigned Count() const { return count; }
00093
00096 unsigned AllocatedSize() const { return size; }
00097
00101 void SetCount( unsigned _count ) { if ( count != _count )
00102 {
00103 if ( _count != size )
00104 ResizePower2( _count );
00105 count = _count;
00106 }
00107 }
00108
00116 unsigned PushBack( const T& t)
00117 {
00118 if ( count+1 > size )
00119 ResizePower2( count+1 );
00120 data[count] = t;
00121 count++;
00122 return count -1;
00123 }
00124
00126 T Back()
00127 {
00128 GLASSERT( count > 0 );
00129 return data[ count - 1 ];
00130 }
00131
00136 T PopBack()
00137 {
00138 GLASSERT( count > 0 );
00139 count--;
00140 return data[count];
00141 }
00142
00146 void Insert( T t, unsigned n )
00147 {
00148 if ( n > count )
00149 n = count;
00150 if ( count + 1 > size )
00151 ResizePower2( count + 1 );
00152 if ( count > 0 )
00153 {
00154 for ( unsigned i = count; i > n; i-- )
00155 data[i] = data[i-1];
00156 }
00157 data[n] = t;
00158 ++count;
00159 }
00160
00164 void Remove( unsigned n )
00165 {
00166 if ( n >= count )
00167 return;
00168
00169 GLASSERT( count > 0 );
00170 if ( count > 0 )
00171 {
00172 for ( unsigned i = n; i < count-1; i++ )
00173 data[i] = data[i+1];
00174 --count;
00175 }
00176 }
00177
00181 unsigned Find( const T& t ) const
00182 {
00183 for ( unsigned i=0; i<count; i++ )
00184 if ( data[i] == t )
00185 return i;
00186 return NotFound();
00187 }
00188
00192 bool FindAndDelete( const T& t )
00193 {
00194 for ( unsigned i=0; i<count; i++ )
00195 {
00196 if ( data[i] == t )
00197 {
00198 Remove( i );
00199 return true;
00200 }
00201 }
00202 return false;
00203 }
00204
00209 void Resize( unsigned _size )
00210 {
00211 if ( _size != size ) {
00212 T* newData = new T[_size];
00213 unsigned copy = GlMin( _size, count );
00214
00215 for( unsigned i=0; i<copy; i++ )
00216 newData[i] = data[i];
00217
00218 delete [] data;
00219 data = newData;
00220
00221 size = _size;
00222 count = GlMin( count, _size );
00223 }
00224 }
00225
00229 void ResizePower2( unsigned _size )
00230 {
00231 unsigned newSize = 1;
00232 while ( newSize < _size )
00233 newSize <<= 1;
00234 Resize( newSize );
00235 }
00236
00241 void Append( const GlDynArray<T>& rhs )
00242 {
00243 int addAt = size;
00244 ResizePower2( size + rhs.size );
00245
00246 for ( int i=0; i<rhs.size; i++ )
00247 data[i+addAt] = rhs.data[addAt];
00248 }
00249
00254 void Sort()
00255 {
00256
00257
00258 int i, j, h;
00259 T value;
00260 int N = count-1;
00261
00262 for( h = 1; h <= N/9; h = 3*h+1 )
00263 {}
00264
00265 for( ; h > 0; h /= 3 )
00266 {
00267 for( i = h+1; i <= N; ++i )
00268 {
00269 value = data[i];
00270 j = i;
00271 while( j>h && data[j-h]>value )
00272 {
00273 data[j] = data[j-h];
00274 j -= h;
00275 }
00276 data[j] = value;
00277 }
00278 }
00279 }
00280
00281 private:
00282 unsigned count;
00283 unsigned size;
00284 T* data;
00285
00286 };
00287
00288
00289 #endif