Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

CascadeArray.h

Go to the documentation of this file.
00001 //
00002 // CascadeArray.h - header file for class CascadeArray
00003 //
00004 // Copyright (c) 2002, Roku, LLC.  All rights reserved.
00005 //
00007 
00008 #ifndef _ROKU_INCLUDE_CASCADE_UTIL_CASCADEARRAY_H
00009 #define _ROKU_INCLUDE_CASCADE_UTIL_CASCADEARRAY_H
00010 
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <cascade/CascadeObject.h>
00014 
00045 class CascadeArray : public CascadeObject
00046 {
00047 public:
00048     CascadeArray( u32 inItemSize, u32 inDesiredItemCount = 0 );
00057 
00058     virtual ~CascadeArray();
00060 
00061     inline u32 Count() const { return mCount; };
00066 
00067     virtual bool Append( void *inItem, u32 &outIndex );
00079 
00080     virtual bool Insert( void *inItem, u32 &ioIndex );
00093 
00094     virtual bool Remove( void *inItemToRemove );
00101 
00102     bool RemoveAtIndex( u32 inIndex );
00109 
00110         inline void Empty( void ) { mCount = 0; }
00118     
00119     bool Swap( u32 inIndex1, u32 inIndex2 );
00127 
00128     bool AdjustAllocation( u32 inDesiredItemCount );
00141             
00142     void TrimAllocation( void );
00148 
00149     virtual bool FetchItem( u32 inIndex, void* outItem ) const;
00159 
00160     typedef int (ArraySortFunction)(const void *inItem1, const void *inItem2);
00172 
00173     virtual void Sort( ArraySortFunction *inFunction );
00180 
00181 protected:
00182     u32 mItemSize;
00183     u32 mArraySize;
00184     u32 mCount;
00185     void *mData;
00186 };
00187 
00188 
00197 template <class T> class CascadeTemplateArray : public CascadeArray
00198 {
00199 public:
00200     CascadeTemplateArray( u32 inDesiredItemCount = 0 )
00201                         : CascadeArray( sizeof(T), inDesiredItemCount ){}
00213         
00217     virtual ~CascadeTemplateArray() {};
00218     
00219     // NOTE: These four functions appear here to suppress a warning about
00220     // the template functions hiding the base class implementation.  When
00221     // using a templated array, we do not want people calling the functions
00222     // that take void* anyway.
00223     virtual bool Append( void *inItem, u32 &outIndex ) { return false; }
00226     virtual bool Insert( void *inItem, u32 &ioIndex ) { return false; }
00229     virtual bool Remove( void *inItemToRemove ) { return false; }
00232     virtual bool FetchItem( u32 inIndex, void *outItem ) const { return false; }
00235     
00236     virtual bool Append( T &inItem, u32 &outIndex )
00237         { return CascadeArray::Append( &inItem, outIndex ); }
00249 
00250     virtual bool Insert( T &inItem, u32 &ioIndex )
00251         { return CascadeArray::Insert( &inItem, ioIndex ); }
00264 
00265     virtual bool Remove( T &inItemToRemove )
00266         { return CascadeArray::Remove( &inItemToRemove ); }
00273 
00274     virtual bool FetchItem( u32 inIndex, T &outItem ) const
00275         { return CascadeArray::FetchItem( inIndex, &outItem ); }
00284 
00285     virtual bool AppendArray( const CascadeTemplateArray<T> &inArray )
00286         {
00287             bool bRetVal = false;
00288             if( AdjustAllocation( mCount + inArray.Count() ) )
00289             {
00290                 T* data = (T*)mData;
00291                 ::memcpy( &data[mCount], 
00292                           inArray.mData, 
00293                           inArray.Count() * sizeof(T) );
00294                 mCount += inArray.Count();
00295                 bRetVal = true;
00296             }
00297             return bRetVal;
00298         }
00313     
00314     virtual bool InsertArray( CascadeTemplateArray<T> &inArray, u32 inIdx )
00315         {
00316             bool bRetVal = false;
00317             u32 newArraySize = mCount + inArray.Count();
00318             if( AdjustAllocation( newArraySize ) )
00319             {
00320                 T* data = (T*)mData;
00321                 u32 idx = inIdx;
00322 
00323                 // Shift items from the index to the end out to their new home
00324                 if( idx < mCount )
00325                 {
00326                     // We don't seem to have memmove
00327                     u32 src = mCount - 1;
00328                     u32 dst = newArraySize - 1;
00329                     for(  ; src >= inIdx; src--, dst-- )
00330                         ::memcpy( &data[dst],
00331                                   &data[src],
00332                                   sizeof(T) );
00333                 }
00334                 else
00335                 {
00336                     idx = mCount;
00337                 }
00338 
00339                 // Copy the input items in.
00340                 ::memcpy( &data[idx], 
00341                           inArray.mData,
00342                           inArray.Count() * sizeof(T) );
00343                 mCount += inArray.Count();
00344                 bRetVal = true;
00345             }
00346             return bRetVal;
00347         }
00365 };
00366 
00367 #endif //_ROKU_INCLUDE_CASCADE_UTIL_CASCADEARRAY_H
00368 
00370 // LOG
00372 // 05-Feb-03    mjkobb          created
00373 // 11-Aug-03    mjkobb      Added AppendArray
00374 // 13-Apr-04    mjkobb      Made some functions const, added InsertArray
00375 // 12-Apr-05    mjkobb      Made AppendArray take a const array
00376 
00377 

Generated on Sun Jul 24 14:27:17 2005 for Cascade Library by  doxygen 1.4.1