/*****************************************************************************************************************************************************
													      Define the array Template For the use of array 
***********************************************************************************************************************************************************/


#ifndef __ZOOARRAY_H__
#define __ZOOARRAY_H__


#include	"..\Basic\ZooStd.h"


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////      Define the array Template For the use of array ///////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<class T> class ZArray
{
	public:

		ZArray();
		~ZArray();

// Add an entry in  the array

		__forceinline	ZArray&		Add(T entry)
		{
			if(mCurNbEntries==mMaxNbEntries)	Resize();
			mEntries[mCurNbEntries++] = entry;
			return *this;
		}

// Insert an entry in the array

		__forceinline	ZArray&		Insert(T entry, int location)
		{
			if(mCurNbEntries==mMaxNbEntries)	Resize();

			for(int j=mCurNbEntries; j>location; j--)				mEntries[j] = mEntries[j-1];
			mEntries[location] = entry;

			mCurNbEntries++;
			
			return *this;
		}

// Add an unique entry in the array

		ZArray&		AddUnique(T entry)
		{
			if(!Contains(entry))	Add(entry);
			return *this;
		}

// Empty an Array

		__forceinline	ZArray&		Empty()
		{
			DELETEARRAY(mEntries);
			mCurNbEntries = mMaxNbEntries = 0;
			return *this;
		}

// Clear an array 

		__forceinline	ZArray&		Clear()
		{
			mCurNbEntries = 0;
			return *this;
		}



		/////////////////////////////////   Other functions for the management of Array  /////////////////////////////////////////////////////


						bool		Contains(T entry, int* location=NULL) const;

						bool		Delete(T entry);
		__forceinline	void		DeleteLastEntry()			{	if(mCurNbEntries)	mCurNbEntries--;	}
		__forceinline	void		DeleteIndex(int index)		{	mEntries[index] = mEntries[--mCurNbEntries];	}

						int			GetUsedRam()		const;
		__forceinline	int			Size()				const	{ return mCurNbEntries;	}	//!< Returns the current number of entries.
		__forceinline	int			SizeMax()			const	{ return mMaxNbEntries;	}	//!< Returns the current number of entries.
		__forceinline	T&			GetEntry(int i)		const	{ return mEntries[i];	}	//!< Returns ith entry
		__forceinline	T*			GetEntries()		const	{ return mEntries;		}	//!< Returns the list of entries.
		__forceinline	T&			operator[](int i)	const	{ ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i];	}



	private:

						bool		Resize();

						int			mMaxNbEntries;		//!< Maximum possible number of entries
						int			mCurNbEntries;		//!< Current number of entries
						T*			mEntries;			//!< List of entries
};


#include	"ZooArray.inl"


#endif
