ExtendableStore< T > Class Template Reference

ExtendableStore< T > Class Template Reference

#include <array.h>

Class Description

template<typename T>
class mudbox::ExtendableStore< T >

This class is similar to Store, except that adding elements to the end of the array is guaranteed to be a constant-time operation.

Random access is also constant-time, although slightly slower than the regular Store class. Use this class only when you need to add several elements to the array quickly.

Definition at line 688 of file array.h.

Public Member Functions

 ExtendableStore ()
 
 ~ExtendableStore ()
 
unsigned int ItemCount () const
 Returns the number of items in the array. More...
 
void SetItemCount (unsigned int iCount)
 Sets the logical size of the array. More...
 
void Clear ()
 Clears the array and deallocates its memory. More...
 
void Add (const T &t)
 Adds a new item to the array, increasing the array size by 1. More...
 
void Extend (unsigned int iIndex)
 Extends the logical size of the array. More...
 
void Fill (T cPattern)
 Fills the array with a specified element. More...
 
unsigned int IndexOf (const T &pValue) const
 Returns the first index of the element equal to pValue. More...
 
T & Last (void)
 Returns the last item in the array, or 0 if the array is empty. More...
 
T & operator[] (unsigned int iIndex)
 
const T & operator[] (unsigned int iIndex) const
 
void Serialize (Stream &s)
 

Constructor & Destructor Documentation

ExtendableStore ( )
inline

Definition at line 702 of file array.h.

702  :
703  m_iBlockItemCount(16384),
704  m_iBlockPow(14),
705  m_iBlockMask(0x3FFF),
706  m_iLogicalItemCount(0),
707  m_iAlignment(16)
708  {
709 
710  };
~ExtendableStore ( )
inline

Definition at line 712 of file array.h.

713  {
714  Clear();
715  };
void Clear()
Clears the array and deallocates its memory.
Definition: array.h:730

Member Function Documentation

unsigned int ItemCount ( void  ) const
inline

Returns the number of items in the array.

Definition at line 718 of file array.h.

718 { return m_iLogicalItemCount; }
void SetItemCount ( unsigned int  iCount)
inline

Sets the logical size of the array.

Returns true if successful.

If the new size is smaller than the current size, items at the end of the array will be discarded. If the new size is larger, then a new memory block will be allocated. In this case, the existing items will only be copied to the new block only if bKeepContent is set to true.

Parameters
[in]iCountThe new size of the array

Definition at line 725 of file array.h.

727  { if( iCount == 0 ) Clear(); else Extend( iCount-1 ); m_iLogicalItemCount = iCount; }
void Extend(unsigned int iIndex)
Extends the logical size of the array.
Definition: array.h:755
void Clear()
Clears the array and deallocates its memory.
Definition: array.h:730
void Clear ( )
inline

Clears the array and deallocates its memory.

Definition at line 730 of file array.h.

731  {
732  m_iLogicalItemCount = 0;
733  for( unsigned int i = 0; i < m_aBlocks.ItemCount(); ++i )
734  {
735  Block::AlignedFree( (void*)m_aBlocks[i] );
736  }
737  m_aBlocks.Clear();
738  }
static void AlignedFree(void *pData)
void Clear(bool bDestruct=false)
Clears the array and deallocates its memory.
Definition: array.h:394
unsigned int ItemCount(void) const
Returns the number of items in the array.
Definition: array.h:645
void Add ( const T &  t)
inline

Adds a new item to the array, increasing the array size by 1.

Parameters
[in]tThe element to be added to the end of the array

Definition at line 741 of file array.h.

744  {
745  const unsigned int c = ItemCount();
746  Extend(c);
747  operator[](c) = t;
748  }
const GLubyte * c
Definition: GLee.h:5419
void Extend(unsigned int iIndex)
Extends the logical size of the array.
Definition: array.h:755
unsigned int ItemCount() const
Returns the number of items in the array.
Definition: array.h:718
T & operator[](unsigned int iIndex)
Definition: array.h:804
GLdouble GLdouble t
Definition: GLee.h:1181
void Extend ( unsigned int  iIndex)
inline

Extends the logical size of the array.

If the specified size is not larger than the current array, nothing will happen. Otherwise the logical size of the array will be extended to one more than the specified index. New memory will be allocated as necessary to hold the new items.

Parameters
[in]iIndexThe array index to extend to. For example, if you specify 7, the new array will contain 8 elements (indexed 0 to 7).

Definition at line 755 of file array.h.

758  {
759  const unsigned int iBlock = iIndex >> m_iBlockPow;
760 
761  // extend the logical item count to include iIndex
762  m_iLogicalItemCount = Max( iIndex + 1, m_iLogicalItemCount );
763 
764  // if this block has already be allocated we are done.
765  if( iBlock < m_aBlocks.ItemCount() )
766  return;
767 
768  // allocate enough blocks to include iIndex
769  const unsigned int ic = m_aBlocks.ItemCount();
770  m_aBlocks.Extend( iBlock );
771  for( unsigned int i = ic; i < m_aBlocks.ItemCount(); ++i )
772  {
773  T* pNewBlock = (T*)Block::AlignedAlloc( sizeof(T) * m_iBlockItemCount, m_iAlignment );
774  MB_ASSERT( ((size_t) pNewBlock) % m_iAlignment == 0 );
775  m_aBlocks[i] = pNewBlock;
776  }
777  }
#define MB_ASSERT(condition)
Definition: mudbox.h:73
unsigned int ItemCount(void) const
Returns the number of items in the array.
Definition: array.h:645
type Max(type a, type b)
Definition: mudbox.h:186
bool Extend(unsigned int iIndex)
Extends the logical size of the array.
Definition: array.h:440
static void * AlignedAlloc(size_t iBytes, unsigned int iAlignment)
void Fill ( cPattern)
inline

Fills the array with a specified element.

Parameters
[in]cPatternThe value to which every item in the array will be set

Definition at line 780 of file array.h.

783  {
784  for ( unsigned int i = 0; i < m_aBlocks.ItemCount(); i++ )
785  for( unsigned int j = 0; j < m_iBlockItemCount; ++j )
786  m_aBlocks[i][j] = cPattern;
787  }
unsigned int ItemCount(void) const
Returns the number of items in the array.
Definition: array.h:645
unsigned int IndexOf ( const T &  pValue) const
inline

Returns the first index of the element equal to pValue.

Note this method requires that operator == be defined for the type of data stored in the array. Returns the index of the first occurance of pValue or 0xffffffff if the array does not contain pValue.

Definition at line 793 of file array.h.

794  {
795  for(unsigned int i = 0; i < ItemCount(); ++i )
796  if( (*this)[i] == pValue )
797  return i;
798  return 0xffffffff;
799  };
unsigned int ItemCount() const
Returns the number of items in the array.
Definition: array.h:718
T& Last ( void  )
inline

Returns the last item in the array, or 0 if the array is empty.

Definition at line 802 of file array.h.

802 { if ( ItemCount() ) return operator []( ItemCount()-1 ); else throw new Error( "ExtenableStore::LastItem called for an empty store" ); };
unsigned int ItemCount() const
Returns the number of items in the array.
Definition: array.h:718
T & operator[](unsigned int iIndex)
Definition: array.h:804
T& operator[] ( unsigned int  iIndex)
inline

Definition at line 804 of file array.h.

805  {
806  const unsigned int iBlock = iIndex >> m_iBlockPow;
807  const unsigned int iBlockItem = iIndex & m_iBlockMask;
808  return m_aBlocks[ iBlock ][iBlockItem];
809  }
const T& operator[] ( unsigned int  iIndex) const
inline

Definition at line 811 of file array.h.

812  {
813  const unsigned int iBlock = iIndex >> m_iBlockPow;
814  const unsigned int iBlockItem = iIndex & m_iBlockMask;
815  return m_aBlocks[ iBlock ][iBlockItem];
816  }
void Serialize ( Stream s)

Definition at line 331 of file stream.h.

332 {
333  s == m_iBlockItemCount;
334  s == m_iBlockPow;
335  s == m_iBlockMask;
336  s == m_iLogicalItemCount;
337  s == m_iAlignment;
338 
339  if( s.IsStoring() )
340  {
341  s << m_aBlocks.ItemCount();
342  for( unsigned int i = 0; i < m_aBlocks.ItemCount(); ++i )
343  s.Write( (void*)m_aBlocks[i], sizeof(T) * m_iBlockItemCount );
344  }
345  else
346  {
347  unsigned int iC = 0;
348  s >> iC;
349  m_aBlocks.SetItemCount( iC );
350  for( unsigned int i = 0; i < m_aBlocks.ItemCount(); ++i )
351  {
352  m_aBlocks[i] = (T*)Block::AlignedAlloc( sizeof(T) * m_iBlockItemCount, m_iAlignment );
353  s.Read( (void*)m_aBlocks[i], sizeof(T) * m_iBlockItemCount );
354  }
355  }
356 };
bool SetItemCount(unsigned int iSize, bool bKeepContent=false)
Sets the logical size of the array.
Definition: array.h:403
unsigned int ItemCount(void) const
Returns the number of items in the array.
Definition: array.h:645
GLdouble s
Definition: GLee.h:1173
static void * AlignedAlloc(size_t iBytes, unsigned int iAlignment)

The documentation for this class was generated from the following files: