/*
....[@@@..[@@@..............[@.................. MUD++ is a written from
....[@..[@..[@..[@..[@..[@@@@@....[@......[@.... scratch multi-user swords and
....[@..[@..[@..[@..[@..[@..[@..[@@@@@..[@@@@@.. sorcery game written in C++.
....[@......[@..[@..[@..[@..[@....[@......[@.... This server is an ongoing
....[@......[@..[@@@@@..[@@@@@.................. development project. All
................................................ contributions are welcome.
....Copyright(C).1995.Melvin.Smith.............. Enjoy.
------------------------------------------------------------------------------
Melvin Smith (aka Fusion) msmith@hom.net
MUD++ development mailing list mudpp@van.ml.org
------------------------------------------------------------------------------
array.cc
*/
// TEMPLATE FILE
// Growable array class
// It is different from LList and HashTable - they use pointers to objects
// this class use Objects themselves
// Example: to get functionality of LList< NPC > you have to declare
// Array< NPC * >
template < class T >
void Array< T >::remove( int pos )
{
#ifdef DEBUG_ARRAY
if ( pos >= count )
Error::dumpf("Array index %d out of bounds(count = %d)", pos, count);
#endif
memmove( &table[pos], &table[pos+1], count - pos );
count--;
}
template < class T >
void Array< T >::destroyList( )
{
// nothing for now
Error::dump("Usupported function called: Array::destroyList");
}
template < class T >
void Array< T >::insert( int pos, const T & obj )
{
count++;
ensureCapacity();
memmove( &table[pos+1], &table[pos], count - pos );
table[pos] = obj;
}
template < class T >
void Array< T >::trim()
{
if (count == size)
return;
T * ntable = new T[count];
memcpy( ntable, table, count * sizeof(T) );
delete table;
table = ntable;
size = count;
}
template < class T >
void Array< T >::grow( int s )
{
if ( s <= size )
return;
T * ntable = new T[s];
memcpy( ntable, table, count * sizeof(T) );
delete table;
table = ntable;
size = s;
}
template < class T >
int Array< T >::readFrom( StaticInput & inf)
{
int fsize;
inf.read( &fsize, sizeof(int) );
grow(fsize);
inf.read( table, fsize * sizeof( T ) );
count = fsize;
return 0;
}
template < class T >
int Array< T >::writeTo( Output & outf) const
{
outf.write( &count, sizeof(int) );
outf.write( table, count * sizeof(T) );
return 0;
}