/
mud++0.33/etc/
mud++0.33/etc/guilds/
mud++0.33/help/propert/
mud++0.33/mudC/
mud++0.33/player/
mud++0.33/src/
mud++0.33/src/bcppbuilder/
mud++0.33/src/unix/
mud++0.33/src/vm/
/*
....[@@@..[@@@..............[@.................. 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-list@mailhost.net
------------------------------------------------------------------------------
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 )
{
	if ( pos >= count )
		Error::dumpf("Array index %d out of bounds(count = %d)", index, count);
	memmove( &table[pos], &table[pos+1], count - pos );
	count--;
}

template < class T >
T Array< T >::removeTop()
{
	if ( count <= 0 )
		Error::dump("Tried to removeTop from empty array");
	count--;
	return table[count+1];
}

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;
}