kolibrios/programs/games/fara/trunk/mcarray.h

182 lines
3.0 KiB
C
Raw Permalink Normal View History

#define AR_CHUNK_SIZE 64
//
template<class TYPE>
class MCArray
{
protected:
TYPE * _dataPtr;
int _elementsCount;
int _capacity;
public:
MCArray();
virtual ~MCArray();
virtual int Add( const TYPE &element );
virtual int AddExclusive( const TYPE &element );
virtual TYPE & GetAt( int Ndx );
virtual TYPE & operator [] ( int Ndx );
virtual int Find( int startNdx, const TYPE & element );
virtual int RemoveAt( int Ndx );
virtual void Clear(void);
virtual int GetCount(void);
};
//
//
template<class TYPE>
MCArray<TYPE>::MCArray()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
this->_dataPtr = NULL;
this->_capacity = 0;
this->_elementsCount = 0;
}
//
template<class TYPE>
MCArray<TYPE>::~MCArray()
{
//
this->_capacity = 0;
this->_elementsCount = 0;
//
if ( this->_dataPtr != NULL )
{
delete this->_dataPtr;
}
}
//
template<class TYPE>
int MCArray<TYPE>::AddExclusive( const TYPE &element )
{
//
if ( this->Find( 0, element ) < 0 )
return this->Add( element );
else
return -1;
}
//
template<class TYPE>
int MCArray<TYPE>::Add( const TYPE &element )
{
TYPE * dPtr;
// <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>?
if ( this->_elementsCount >= this->_capacity )
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
dPtr = new TYPE [this->_capacity + AR_CHUNK_SIZE];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if ( dPtr == NULL )
{
//
return -1;
}
if ( this->_capacity > 0 )
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
memcpy( dPtr, this->_dataPtr, sizeof(TYPE) * this->_capacity );
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
delete this->_dataPtr;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
this->_capacity += AR_CHUNK_SIZE;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
this->_dataPtr = dPtr;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
this->_dataPtr[this->_elementsCount] = element;
//memcpy( this->_dataPtr + this->_elementsCount, &element, sizeof(TYPE) );
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return ++this->_elementsCount;
}
//
template<class TYPE>
TYPE & MCArray<TYPE>::GetAt( int Ndx )
{
//assert( Ndx >= 0 && Ndx < this->_elementsCount );
return this->_dataPtr[Ndx];
}
//
template<class TYPE>
TYPE & MCArray<TYPE>::operator [] ( int Ndx )
{
return this->GetAt( Ndx );
}
//
template<class TYPE>
int MCArray<TYPE>::Find( int startNdx, const TYPE & element )
{
int i;
if ( startNdx < 0 || startNdx >= this->_elementsCount )
{
return -1;
}
for ( i = startNdx; i < this->_elementsCount; i++ )
{
if ( element == this->_dataPtr[i] )
{
return i;
}
}
return -1;
}
//
template<class TYPE>
int MCArray<TYPE>::RemoveAt( int Ndx )
{
int mn;
if ( Ndx < 0 || Ndx >= this->_elementsCount )
{
return 0;
}
mn = this->_elementsCount - Ndx;
if ( mn != 1 )
{
memcpy( this->_dataPtr + Ndx, this->_dataPtr + Ndx + 1, sizeof(TYPE) * ( mn - 1 ) );
}
this->_elementsCount--;
return 1;
}
//
template<class TYPE>
void MCArray<TYPE>::Clear()
{
this->_elementsCount = 0;
}
//
template<class TYPE>
int MCArray<TYPE>::GetCount()
{
return this->_elementsCount;
}