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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef KYRA_DYNARRAY_INCLUDED
00070 #define KYRA_DYNARRAY_INCLUDED
00071
00072 #include <limits.h>
00073 #include "glutil.h"
00074 #include "gldebug.h"
00075
00084 template <class T>
00085 class GlDynArray
00086 {
00087 public:
00088 GlDynArray( int _size ) { size = 0; count = 0; data = 0; Resize( _size ); }
00089 GlDynArray() { size = 0; count = 0; data = 0; }
00090 ~GlDynArray() { delete [] data; }
00091
00092 GlDynArray( const GlDynArray<T>& copy ) { size = 0; count = 0; data = 0; this->SetEqual( copy ); }
00093 void operator=( const GlDynArray<T>& copy ) { this->SetEqual( copy ); }
00094
00095 unsigned NotFound() const { return UINT_MAX; }
00096 bool Empty() const { return count == 0; }
00097
00098 void Clear() { delete [] data; size = 0; count = 0; data = 0; }
00099
00101 T& operator[] ( unsigned i ) const { GLASSERT( i<count );
00102 GLASSERT( i>=0 );
00103 return data[i]; }
00105 T* Memory() { return data; }
00106
00107 void SetEqual( const GlDynArray<T>& rhs )
00108 {
00109 delete [] data;
00110 data = new T[ rhs.size ];
00111 for ( unsigned i=0; i<rhs.count; i++ )
00112 {
00113 data[i] = rhs.data[i];
00114 }
00115 size = rhs.size;
00116 count = rhs.count;
00117 }
00118
00120 T Item( unsigned i ) const { GLASSERT( i<count );
00121 return data[i]; }
00123 T* ItemPointer( unsigned i ) const { GLASSERT( i<count );
00124 return &data[i]; }
00126 void SetItem( unsigned i, const T& t )
00127 { GLASSERT( i<count );
00128 data[i] = t; }
00129
00132 unsigned Count() const { return count; }
00133
00136 unsigned AllocatedSize() const { return size; }
00137
00141 void SetCount( unsigned _count ) { if ( count != _count )
00142 {
00143 if ( _count != size )
00144 ResizePower2( _count );
00145 count = _count;
00146 }
00147 }
00148
00156 unsigned PushBack( const T& t)
00157 {
00158 if ( count+1 > size )
00159 ResizePower2( count+1 );
00160 data[count] = t;
00161 count++;
00162 return count -1;
00163 }
00164
00166 T Back()
00167 {
00168 GLASSERT( count > 0 );
00169 return data[ count - 1 ];
00170 }
00171
00176 T PopBack()
00177 {
00178 GLASSERT( count > 0 );
00179 count--;
00180 return data[count];
00181 }
00182
00186 void Insert( T t, unsigned n )
00187 {
00188 if ( n > count )
00189 n = count;
00190 if ( count + 1 > size )
00191 ResizePower2( count + 1 );
00192 if ( count > 0 )
00193 {
00194 for ( unsigned i = count; i > n; i-- )
00195 data[i] = data[i-1];
00196 }
00197 data[n] = t;
00198 ++count;
00199 }
00200
00204 void Remove( unsigned n )
00205 {
00206 if ( n >= count )
00207 return;
00208
00209 GLASSERT( count > 0 );
00210 if ( count > 0 )
00211 {
00212 for ( unsigned i = n; i < count-1; i++ )
00213 data[i] = data[i+1];
00214 --count;
00215 }
00216 }
00217
00221 unsigned Find( const T& t ) const
00222 {
00223 for ( unsigned i=0; i<count; i++ )
00224 if ( data[i] == t )
00225 return i;
00226 return NotFound();
00227 }
00228
00232 bool FindAndDelete( const T& t )
00233 {
00234 for ( unsigned i=0; i<count; i++ )
00235 {
00236 if ( data[i] == t )
00237 {
00238 Remove( i );
00239 return true;
00240 }
00241 }
00242 return false;
00243 }
00244
00249 void Resize( unsigned _size )
00250 {
00251 if ( _size != size ) {
00252 T* newData = new T[_size];
00253 unsigned copy = GlMin( _size, count );
00254
00255 for( unsigned i=0; i<copy; i++ )
00256 newData[i] = data[i];
00257
00258 delete [] data;
00259 data = newData;
00260
00261 size = _size;
00262 count = GlMin( count, _size );
00263 }
00264 }
00265
00269 void ResizePower2( unsigned _size )
00270 {
00271 unsigned newSize = 1;
00272 while ( newSize < _size )
00273 newSize <<= 1;
00274 Resize( newSize );
00275 }
00276
00281 void Append( const GlDynArray<T>& rhs )
00282 {
00283 int addAt = size;
00284 ResizePower2( size + rhs.size );
00285
00286 for ( int i=0; i<rhs.size; i++ )
00287 data[i+addAt] = rhs.data[addAt];
00288 }
00289
00294 void Sort()
00295 {
00296
00297
00298 int i, j, h;
00299 T value;
00300 int N = count-1;
00301
00302 for( h = 1; h <= N/9; h = 3*h+1 )
00303 {}
00304
00305 for( ; h > 0; h /= 3 )
00306 {
00307 for( i = h+1; i <= N; ++i )
00308 {
00309 value = data[i];
00310 j = i;
00311 while( j>h && data[j-h]>value )
00312 {
00313 data[j] = data[j-h];
00314 j -= h;
00315 }
00316 data[j] = value;
00317 }
00318 }
00319 }
00320
00321 private:
00322 unsigned count;
00323 unsigned size;
00324 T* data;
00325
00326 };
00327
00328
00329 #endif