17 void MBDLL_DECL QuickSort(
void *pBase,
unsigned int iNum,
unsigned int iSize,
int ( * fComparator ) (
const void *,
const void * ) );
23 Block(
const char *sName,
unsigned int iItemSize )
28 if ( m_pNext ) m_pNext->m_pPrev =
this;
30 m_iItemSize = iItemSize;
39 inline static const Block *
Head(
void ) {
return s_pHead; };
40 inline const Block *
Next(
void )
const {
return m_pNext; };
41 inline unsigned int ItemSize(
void )
const {
return m_iItemSize; };
43 const char *Name(
void )
const;
44 long long Size(
void )
const;
45 long long Address(
void )
const;
46 static void LogAll(
float fSizeFilter = 0.01
f,
bool bSortByAddress =
false );
47 void Check(
void )
const;
48 static void CheckAll(
void );
49 static bool RegisterMemoryBlock(
long long iSize );
50 static bool UnregisterMemoryBlock(
long long iSize );
51 static void SetAllocatorID(
const char *pAllocatorID );
52 static void CopyMemoryBlock(
void *pDestination,
const void *pSource,
long long iSize );
55 static void* AlignedAlloc(
size_t iBytes,
unsigned int iAlignment );
58 static void AlignedFree(
void* pData );
64 template <
typename type >
76 Array(
const char *sName,
unsigned int iSize ) :
Block( sName, sizeof(
type) )
83 if ( RegisterMemoryBlock(
sizeof(
type)*iSize ) )
85 SetAllocatorID( sName );
86 m_pArray =
new type[iSize];
105 if ( RegisterMemoryBlock(
sizeof(
type)*iSize ) )
107 SetAllocatorID( sName );
108 m_pArray =
new type[iSize];
123 memcpy( m_pArray, pArray,
sizeof(
type)*iSize );
130 m_bData = bNoObjects;
146 void Clear(
bool bDestruct =
false )
148 bDestruct = bDestruct;
152 UnregisterMemoryBlock( (
long long)(
sizeof(
type)*m_iSize) );
166 if ( !a.
Alloc( m_iSize ) )
172 inline void Set(
unsigned int iStart,
unsigned int iSize,
unsigned char cPattern )
173 {
MB_ASSERT( m_pThis ==
this );
MB_ASSERT( iStart+iSize <= m_iSize ); memset( m_pArray+iStart,
int(cPattern),
sizeof(
type)*iSize ); };
175 inline bool Extend(
unsigned int iElementIndex )
177 if ( iElementIndex == 0xffffffff )
179 unsigned int iNewSize = m_iSize;
180 while ( iElementIndex >= iNewSize )
181 iNewSize = iNewSize*2+1;
182 if( iNewSize > m_iSize )
183 return Alloc( iNewSize );
189 if ( iNewSize == m_iSize )
195 if ( RegisterMemoryBlock(
sizeof(
type)*iNewSize ) )
197 SetAllocatorID( m_sName );
198 pNew =
new type[iNewSize];
220 UnregisterMemoryBlock(
sizeof(
type)*m_iSize );
222 CopyMemoryBlock( pNew, m_pArray,
Min( iNewSize, m_iSize )*
sizeof(
type) );
224 for (
unsigned int i = 0; i <
Min( iNewSize, m_iSize ); i++ )
225 pNew[i] = m_pArray[i];
233 inline type &operator [](
int iIndex )
236 MB_ONBUG( (
unsigned int)(iIndex) >= m_iSize )
238 return m_pArray[iIndex];
241 inline type &operator [](
unsigned int iIndex )
246 return m_pArray[iIndex];
249 inline const type &operator [](
unsigned int iIndex )
const
254 return m_pArray[iIndex];
269 mutable type *m_pArray;
333 template <
typename type >
339 const char *sName =
"unknown"
340 ) :
Array<
type>( sName ) { m_iSize = 0; };
353 ) :
Array<
type>( sName, iSize ) { m_iSize = iSize; };
363 const char *sName =
"unknown"
364 ) :
Array<
type>( sName, pArray, iSize ) { m_iSize = iSize; };
370 ) :
Array<
type>( sName, bNoObjects ) { m_iSize = 0; };
395 bool bDestruct =
false
405 bool bKeepContent =
false
408 if ( iSize <= m_iSize )
432 bool bKeepContent =
false
446 m_iSize =
Max( m_iSize, iIndex+1 );
459 ) {
return Extend( (
unsigned int)iIndex ); };
464 ) { SetItemCount( ItemCount()-iItemCount ); };
469 ) {
for (
unsigned int i = 0; i < m_iSize; i++ )
operator[](i) = cPattern; };
474 unsigned char cPattern
513 const type &operator [](
int iIndex )
const {
return operator[]( (
unsigned int)iIndex ); };
520 inline bool IsEmpty(
void )
const {
return ItemCount() == 0; };
523 inline operator bool(
void )
const {
return !IsEmpty(); };
526 inline bool operator !(
void )
const {
return IsEmpty(); };
586 for(
unsigned int i = 0; i < ItemCount(); ++i )
587 if( (*
this)[i] == pValue )
597 unsigned int j = 0, i = 0;
598 while ( i < ItemCount() )
600 if (
operator[]( i ) != e )
601 operator[]( j++ ) = operator[]( i );
613 while ( iIndex+1 < ItemCount( ) )
615 operator[]( iIndex ) = operator[]( iIndex+1 );
618 SetItemCount(
Min(iIndex,ItemCount()-1) );
629 if ( ItemCount() == 0 )
633 for (
unsigned int i = 0; i < ItemCount()-1; i++ )
635 if (
operator[]( i ) != operator[]( i+1 ) )
636 operator[]( j++ ) = operator[]( i );
638 operator[]( j++ ) = operator[]( ItemCount()-1 );
639 unsigned iRemoved = ItemCount()-j;
645 inline unsigned int ItemCount(
void )
const {
return m_iSize; };
648 inline type &
Last(
void ) {
if ( ItemCount() )
return operator []( ItemCount()-1 );
else throw new Error(
"Store::LastItem called for an empty store" ); };
662 static int Compare(
const void *
a,
const void *
b ) {
return (
int)(*((
type *)a)-*((
type *)b)); };
680 mutable unsigned int m_iSize;
690 unsigned int m_iLogicalItemCount;
692 const unsigned int m_iBlockItemCount;
693 const unsigned int m_iBlockPow;
694 const unsigned int m_iBlockMask;
696 const unsigned int m_iAlignment;
703 m_iBlockItemCount(16384),
705 m_iBlockMask(0x3FFF),
706 m_iLogicalItemCount(0),
718 inline unsigned int ItemCount()
const {
return m_iLogicalItemCount; }
727 ) {
if( iCount == 0 )
Clear();
else Extend( iCount-1 ); m_iLogicalItemCount = iCount; }
732 m_iLogicalItemCount = 0;
733 for(
unsigned int i = 0; i < m_aBlocks.
ItemCount(); ++i )
759 const unsigned int iBlock = iIndex >> m_iBlockPow;
762 m_iLogicalItemCount =
Max( iIndex + 1, m_iLogicalItemCount );
769 const unsigned int ic = m_aBlocks.
ItemCount();
770 m_aBlocks.
Extend( iBlock );
771 for(
unsigned int i = ic; i < m_aBlocks.
ItemCount(); ++i )
774 MB_ASSERT( ((
size_t) pNewBlock) % m_iAlignment == 0 );
775 m_aBlocks[i] = pNewBlock;
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;
793 inline unsigned int IndexOf(
const T& pValue )
const
795 for(
unsigned int i = 0; i <
ItemCount(); ++i )
796 if( (*
this)[i] == pValue )
806 const unsigned int iBlock = iIndex >> m_iBlockPow;
807 const unsigned int iBlockItem = iIndex & m_iBlockMask;
808 return m_aBlocks[ iBlock ][iBlockItem];
813 const unsigned int iBlock = iIndex >> m_iBlockPow;
814 const unsigned int iBlockItem = iIndex & m_iBlockMask;
815 return m_aBlocks[ iBlock ][iBlockItem];
unsigned int RemoveDuplicates(void)
Removes duplicated items from the array after sorting it.
GLuint GLuint GLsizei GLenum type
static void AlignedFree(void *pData)
Store(unsigned int iSize, const char *sName)
Creates an array with space for some elements already allocated.
void operator=(const Array< type > &a)
#define MBDLL_TEMPLATE_DECL
void Optimize(void)
Reduces the allocated memory size to match the size of the current elements in the array...
unsigned int Remove(const type &e)
Remove every occurrence of a specified item from the array. Returns the number of removed items...
type * indexedAddr(unsigned int i)
bool Extend(int iIndex)
Extends the logical size of the array.
bool Extend(unsigned int iElementIndex)
Array< type > Clone(void) const
Array(const char *sName, const Array< type > &a)
#define MB_ASSERT(condition)
void GetFrom(Store &s)
Transfers the contents of another array to this one, leaving the original array empty.
type * Find(type *a)
Returns a pointer to the item in the array with a particular value.
Block(const char *sName, unsigned int iItemSize)
unsigned int ItemSize(void) const
const type * baseAddr() const
type & Last(void)
Returns the last item in the array, or 0 if the array is empty.
This class is similar to Store, except that adding elements to the end of the array is guaranteed to ...
void Fill(T cPattern)
Fills the array with a specified element.
void Set(unsigned int iStart, unsigned int iSize, unsigned char cPattern)
type & operator[](int iIndex)
void MBDLL_DECL QuickSort(void *pBase, unsigned int iNum, unsigned int iSize, int(*fComparator)(const void *, const void *))
Store(bool bNoObjects, const char *sName)
Creates an empty array, allowing you to specify if elements have constructors or destructors that nee...
Holds information about an error.
Array(const char *sName, const type *pArray, int iSize)
void Clone(Store &s) const
Copies the contents of another array into this one, duplicating all data.
static const Block * Head(void)
bool Copy(Array< type > &a) const
void Sort(void)
Sorts the items in the array.
void Clear(bool bDestruct=false)
Clears the array and deallocates its memory.
void Serialize(Stream &s)
An internal helper class, representing an array. Use the Store class instead.
unsigned int IndexOf(type pValue)
Returns the first index of the element equal to pValue.
bool Alloc(unsigned int iNewSize)
void Fill(type cPattern)
Fills the array with a specified element.
static int Compare(const void *a, const void *b)
Array(const char *sName, bool bNoObjects)
bool SetItemCount(unsigned int iSize, bool bKeepContent=false)
Sets the logical size of the array.
void Add(const T &t)
Adds a new item to the array, increasing the array size by 1.
Store(const type *pArray, int iSize, const char *sName="unknown")
Creates an array and populates it will information copied from another array.
bool Copy(Store &s) const
Copies the contents of another array into this one, duplicating all data. Returns true if successful...
unsigned int Add(type &e)
Adds a new item to the array, increasing the array size by 1.
type & operator[](int iIndex)
Returns an indexed item from the array.
#define MB_ONBUG(condition)
void Clear(bool bDestruct=false)
T & Last(void)
Returns the last item in the array, or 0 if the array is empty.
const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Store(const Store &s)
Creates an array from another one. The other array becomes empty after the operation.
unsigned int ItemCount(void) const
Returns the number of items in the array.
unsigned int IndexOf(const T &pValue) const
Returns the first index of the element equal to pValue.
void SetItemCount(unsigned int iCount)
Sets the logical size of the array.
const type * indexedAddr(unsigned int i) const
Array(const char *sName, unsigned int iSize)
Store Clone(void) const
Returns a copy of this array. The returned copy will have a duplicate of all data in the original...
const type & operator[](int iIndex) const
Returns an indexed item from the array.
void Extend(unsigned int iIndex)
Extends the logical size of the array.
bool Allocate(unsigned int iSize, bool bKeepContent=false)
Preallocates memory for the an array, without changing the array's logical size.
unsigned int Add(const type &e)
Adds a new item to the array, increasing the array size by 1.
Class: ConvolutionKernel.
void RemoveTail(int iItemCount=1)
Removes the final items from the array.
const Array< type > * m_pThis
Store(const char *sName="unknown")
Creates an empty array.
GLubyte GLubyte GLubyte a
static void ThrowBadAlloc(void)
Throws a static bad alloc exception. (No memory allocation is required to report allocation problems...
void SetBuffer(type *pData, unsigned int iSize=0)
Makes a Store array from a regular C or C++ array.
void RemoveAt(unsigned int iIndex)
Removes the item at the specified index from the array.
Streams are used to read information from a file, or to write it to a file.
bool IsEmpty(void) const
Returns true if the array has no items.
const Block * Next(void) const
unsigned int ItemCount() const
Returns the number of items in the array.
T & operator[](unsigned int iIndex)
An internal helper class, representing a block of memory. Use the Store class instead.
~Store(void)
Destroys the contents of the array and deallocate its memory.
type * Find(type a)
Returns a pointer to the item in the array with a particular value.
void Clear()
Clears the array and deallocates its memory.
void ByteFill(unsigned char cPattern)
Fills the array with a specified byte pattern.
bool Extend(unsigned int iIndex)
Extends the logical size of the array.
static void * AlignedAlloc(size_t iBytes, unsigned int iAlignment)