template <class BaseClass,int Count> class TArray
{protected:
BaseClass *Array;
public:
TArray() {Array=new BaseClass[Count];};
BaseClass& operator [](int Index) {
	if (Index>=Count)
		return  Array[Count-1];
	else
		return Array[Index];};
~TArray() {
delete []Array;
Array=NULL;};
};

template <class BaseClass> class TListItem
{public:
TListItem* Prev;
TListItem* Next;
BaseClass Item;
};

template <class BaseClass> class TFILO
{public:
byte Empty;
word Count;
typedef TListItem <BaseClass> _TListItem;
_TListItem *Top;
TFILO() {Empty=0xff;
	Count=0;
	Top=NULL;
	};
~TFILO() {Clear();};
void Clear(void)
	{while (!Empty)
		Kill();
	};
void Kill(){if (Empty) return;
	if (Count>1)
		{Top=Top->Prev;
		delete Top->Next;
		Top->Next=NULL;
		Count--;
		return;}
	else
		{delete Top;
		Top=NULL;
		Count=0;
		Empty=0xff;};
};
void Push(BaseClass& Item){
	if (Empty)
		{Top=new TListItem <BaseClass>;
		Top->Prev=NULL;
		Top->Next=NULL;
		Top->Item=Item;
		Count=1;
		Empty=0;
		return;}
	else
		{Top->Next=new TListItem <BaseClass>;
		Top->Next->Prev=Top;
		Top=Top->Next;
		Top->Next=NULL;
		Top->Item=Item;
		Count++;};
	};
void Pop(BaseClass& Item){
	if (!Empty)
	if (Count>1)
		{Item=Top->Item;
		Top=Top->Prev;
		delete (Top->Next);
		Top->Next=NULL;
		Count--;
		return;}
	else
		{Item=Top->Item;
		delete Top;
		Top=NULL;
		Count=0;
		Empty=0xff;};
	};
BaseClass Pop(void){
	BaseClass Result;
	if (!Empty)
	if (Count>1)
		{Result=Top->Item;
		Top=Top->Prev;
		delete Top->Next;
		Top->Next=NULL;
		Count--;}
	else
		{Result=Top->Item;
		delete Top;
		Top=NULL;
		Count=0;
		Empty=0xff;};
	return Result;
	};
};

template <class BaseClass> class TFIFO
{public:
byte Empty;
word Count;
typedef TListItem <BaseClass> _TListItem;
_TListItem *Top;
_TListItem *Bottom;
TFIFO() {Empty=0xff;
	Count=0;
	Top=NULL;
	Bottom=NULL;
	};
void Clear(void)
	{while (!Empty)
		Kill();
	};
void Kill(){if (Empty) return;
	if (Count>1)
		{Top=Top->Prev;
		delete Top->Next;
		Top->Next=NULL;
		Count--;
		return;}
	else
		{delete Bottom;
		Top=NULL;
		Bottom=NULL;
		Count=0;
		Empty=0xff;};
};
void Push(BaseClass& Item){
	if (Empty)
		{Top=new TListItem <BaseClass>;
		Top->Prev=NULL;
		Top->Next=NULL;
		Top->Item=Item;
		Bottom=Top;
		Count=1;
		Empty=0;
		return;}
	else
		{Bottom->Prev=new TListItem <BaseClass>;
		Bottom->Prev->Next=Bottom;
		Bottom=Bottom->Prev;
		Bottom->Prev=NULL;
		Bottom->Item=Item;
		Count++;};
	};
void Pop(BaseClass& Item){
	if (Count>1)
		{Item=Top->Item;
		Top=Top->Prev;
		delete Top->Next;
		Top->Next=NULL;
		Count--;
		return;}
	else
		{Item=Bottom->Item;
		delete Bottom;
		Top=NULL;
		Bottom=NULL;
		Count=0;
		Empty=0xff;};
	};
BaseClass Pop(void){
	BaseClass Result;
	if (Count>1)
		{Result=Top->Item;
		Top=Top->Prev;
		delete Top->Next;
		Top->Next=NULL;
		Count--;
		}
	else
		{Result=Bottom->Item;
		delete Bottom;
		Top=NULL;
		Bottom=NULL;
		Count=0;
		Empty=0xff;};
	return Result;
	};
};