Main Page   Class Hierarchy   Compound List   File List   Compound Members  

gldynarray.h

00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003 
00004 Grinning Lizard Utilities. Note that software that uses the 
00005 utility package (including Lilith3D and Kyra) have more restrictive
00006 licences which applies to code outside of the utility package.
00007 
00008 
00009 This software is provided 'as-is', without any express or implied 
00010 warranty. In no event will the authors be held liable for any 
00011 damages arising from the use of this software.
00012 
00013 Permission is granted to anyone to use this software for any 
00014 purpose, including commercial applications, and to alter it and 
00015 redistribute it freely, subject to the following restrictions:
00016 
00017 1. The origin of this software must not be misrepresented; you must 
00018 not claim that you wrote the original software. If you use this 
00019 software in a product, an acknowledgment in the product documentation 
00020 would be appreciated but is not required.
00021 
00022 2. Altered source versions must be plainly marked as such, and 
00023 must not be misrepresented as being the original software.
00024 
00025 3. This notice may not be removed or altered from any source 
00026 distribution.
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                 // Shellsort. Sedgewick, Algorithms in C++. p109
00257                 // Note N = count-1
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

Generated on Mon Sep 15 12:01:10 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001