#ifndef _COMMON_BLOCK_TPP_
#define _COMMON_BLOCK_TPP_

template <class T>
void Block<T>::remove(void)
{
	if(!mContainer)return;
	Container<T> *Cursor=mContainer;
	Container<T> *Thumb;
	while(Cursor)
	{
		Thumb=Cursor;
		Cursor=Cursor->next();
		::delete Thumb;
	}
	mContainer=0;
	mLastIndexReferenced=-1L;
	mLastObjectReferenced=0;
	mLastObjectInserted=0;
	mSize=0;
}

template <class T>
T &Block<T>::find(LONG itemIndex)
{
	Container<T> *lpCursor=mContainer;
	for(Index i=0;i<mSize&&i<itemIndex;i++)lpCursor=lpCursor->next();
	if(i==itemIndex)
	{
		mLastIndexReferenced=i;
		mLastObjectReferenced=lpCursor;
		return *lpCursor->mItem;
	}
	return *((T*)0);
}

template <class T>
Block<T> &Block<T>::operator+=(const Block<T> &someBlock)
{
	size_t blockSize(someBlock.size());

	if(!blockSize)return *this;
	for(int itemIndex=0;itemIndex<blockSize;itemIndex++)insert(&((Block<T>&)someBlock)[itemIndex]);
	return *this;
}

template <class T>
bool Block<T>::insertAfter(Index itemIndex,const T *item)
{
  if(itemIndex>=mSize)return false;
  mLastObjectInserted->next(::new Container<T>);
  mLastObjectInserted->next()->prev(mLastObjectInserted);
  mLastObjectInserted=mLastObjectInserted->next();
  mLastObjectInserted->mItem=::new T();
  mSize++;
  for(int index=mSize-1;index>itemIndex+1;index--)
  {
    operator[](index)=operator[](index-1);  
  }
	operator[](itemIndex+1)=*item;
	return true;
}

template <class T>
void Block<T>::insert(Block<T> &newBlock)
{
	LONG size(newBlock.size());
	for(Index i=0;i<size;i++)insert(&newBlock[i]);
}

template <class T>
void Block<T>::insert(const T *item)
{
	if(!mContainer)
	{
		mContainer=::new Container<T>;
		mContainer->mItem=::new T(*item);
		mLastObjectInserted=mContainer;
		mSize++;
	}
	else
	{
		mLastObjectInserted->next(::new Container<T>);
		mLastObjectInserted->next()->prev(mLastObjectInserted);
		mLastObjectInserted=mLastObjectInserted->next();
		mLastObjectInserted->mItem=::new T(*item);
		mSize++;
	}
}

template <class T>
void Block<T>::remove(Block<T> &removeBlock)
{
	LONG size(removeBlock.size());

	if(!size)return;
  for(long itemIndex=size-1;itemIndex>=0;itemIndex--)remove(&removeBlock[itemIndex]);
}

template <class T>
void Block<T>::remove(const T *item)
{
	Container<T> *lpThumb=mContainer;

	if(!lpThumb)return;
	if(*item==*(mLastObjectInserted->mItem))lpThumb=mLastObjectInserted;
  else while(lpThumb){if(*item==*(lpThumb->mItem))break;lpThumb=lpThumb->next();}
	if(!lpThumb)return;
	if(lpThumb==mContainer)
	{
		if(lpThumb->next()){mContainer=lpThumb->next();mContainer->prev(0);}
		else {mContainer=0;mLastObjectInserted=0;}
	}
	else
	{
		if(lpThumb->next()){lpThumb->prev()->next(lpThumb->next());lpThumb->next()->prev(lpThumb->prev());}
		else {lpThumb->prev()->next(0);mLastObjectInserted=lpThumb->prev();}
	}
	::delete lpThumb;
	mSize--;
	mLastIndexReferenced=-1;
  mLastObjectReferenced=0;
}

template <class T>
void Block<T>::remove(LONG itemIndex)
{
	Container<T> *lpThumb=mContainer;

	if(!lpThumb||itemIndex>=mSize)return;
	if(itemIndex==mSize-1)lpThumb=mLastObjectInserted;
	else
	{
		for(Index i=0;i<itemIndex&&lpThumb;i++)
		{
			if(itemIndex==i)break;
			lpThumb=lpThumb->next();
		}
    if(!lpThumb)return;
	}
	if(lpThumb==mContainer)
	{
		if(lpThumb->next()){mContainer=lpThumb->next();mContainer->prev(0);}
		else {mContainer=0;mLastObjectInserted=0;}
	}
	else
	{
		if(lpThumb->next()){lpThumb->prev()->next(lpThumb->next());lpThumb->next()->prev(lpThumb->prev());}
		else {lpThumb->prev()->next(0);mLastObjectInserted=lpThumb->prev();}
	}
	::delete lpThumb;
	mSize--;
	mLastIndexReferenced=-1;
  mLastObjectReferenced=0;
}

template <class T>
Block<T> &Block<T>::operator=(const Block<T> &someBlock)
{
	LONG itemCount(someBlock.size());

	remove();
	if(!itemCount)return *this;
  for(LONG itemIndex=0;itemIndex<itemCount;itemIndex++)insert(&(((Block<T>&)someBlock)[itemIndex]));
	return *this;
}

template <class T>
WORD Block<T>::operator==(const Block<T> &someBlock)const
{
	LONG itemCount(size());

	if(itemCount!=someBlock.size())return FALSE;
	for(LONG itemIndex=0;itemIndex<itemCount;itemIndex++)
		if(!(((Block<T>&)*this).operator[](itemIndex)==((Block<T>&)someBlock)[itemIndex]))return FALSE;
	return TRUE;
}
#endif
