mud++0.26/etc/
mud++0.26/etc/guilds/
mud++0.26/log/
mud++0.26/mudC/
mud++0.26/player/
mud++0.26/src/unix/
/*
....[@@@..[@@@..............[@.................. 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.h
*/

#ifndef ARRAY_H
#define ARRAY_H

#include "help.h"
#include "erratum.h"

const int DEFAULT_SIZE = 10;
const int DEFAULT_DELTA = 5;

template < class T >
class Array
{
	private:
		T * table;
		int count;	// how many important elements array has
		int size;	// real size of array
		int delta;	// how much will the array grow when needed
		int focus;	// for llist like interface 'current'
		
	public:
		Array()
		:	count(0), size( DEFAULT_SIZE ), delta( DEFAULT_DELTA )
		{
			table = new T[size];
		}

		Array( int siz, int del)
		: count(0), size(siz), delta(del)
		{
			table = new T[size];
		}

		Array( const Array & x )
		: count(x.count), size(x.size), delta(x.delta)
		{
			table = new T[size];
			memcpy ( table, x.table, count * sizeof(T) );
		}

		~Array() { delete table; }

		T & operator[]( int );
		int length() { return count; }
		int getDelta() { return delta; }
		void setDelta(int i ) { delta = i; }

		void add( const T & );
		void add( const T * obj) { add( *obj ); }
		void remove( int );
		void insert( int, const T & );
		void insert( int index, const T * obj ) { insert(index,*obj); }
		void clr() { count = 0; }
		void trim();
		void grow( int );
		void ensureCapacity();

		// LList interface to allow use of lovely for_each
		void reset() { focus = 0; }
		void next() { focus++; }
		T & peek() { return operator[](focus); }
		void destroyList();
};

template < class T >
inline void Array< T >::ensureCapacity()
{
	if ( count > size )
	{
		T * ntable = new T[size+delta];
		memcpy( ntable, table, size * sizeof(T) );
		size += delta;
		delete table;
		table = ntable;
	}
}

template < class T >
inline T & Array< T >::operator[]( int index )
{
	if ( index >= count )
		Error::outOfBounds();
	return table[index];
}

template <class T>
inline void Array< T >::add( const T & obj )
{
	count++;
	ensureCapacity();
	table[count-1] = obj;
}

#endif // ARRAYI_H