Main Page   Class Hierarchy   Compound List   File List   Compound Members  

gldynarray.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
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                 // Shellsort. Sedgewick, Algorithms in C++. p109
00297                 // Note N = count-1
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

Generated on Fri Feb 7 20:44:20 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001