231 lines
3.9 KiB
C++
231 lines
3.9 KiB
C++
#ifndef _BLOCK_HPP_
|
|
#error BLOCK.HPP MUST PRECEDE BLOCK.TPP
|
|
#endif
|
|
|
|
#ifdef _EXPAND_BLOCK_TEMPLATES_
|
|
//#pragma option -Jgd
|
|
#endif
|
|
|
|
template <class T>
|
|
Block<T>::Block()
|
|
: mContainer(0), mSize(0), mLastIndexReferenced(-1), mLastObjectReferenced(0),
|
|
mLastObjectInserted(0)
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
Block<T>::~Block()
|
|
{
|
|
remove();
|
|
}
|
|
|
|
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>::operator [](LONG itemIndex)
|
|
{
|
|
Container<T> *Cursor;
|
|
|
|
assert(itemIndex<mSize);
|
|
if(!mContainer)return *((T*)0);
|
|
if(itemIndex==mLastIndexReferenced && -1!=mLastIndexReferenced)
|
|
return *mLastObjectReferenced->mItem;
|
|
if(itemIndex==mLastIndexReferenced+1 && -1!=mLastIndexReferenced)
|
|
{
|
|
mLastIndexReferenced++;
|
|
mLastObjectReferenced=mLastObjectReferenced->Next();
|
|
return *mLastObjectReferenced->mItem;
|
|
}
|
|
Cursor=mContainer;
|
|
for(Index i=0;i<mSize && i<itemIndex;i++)Cursor=Cursor->Next();
|
|
if(i==itemIndex)
|
|
{
|
|
mLastIndexReferenced=i;
|
|
mLastObjectReferenced=Cursor;
|
|
return *Cursor->mItem;
|
|
}
|
|
return *((T*)0);
|
|
}
|
|
|
|
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=mLastObjectInserted->Next();
|
|
mLastObjectInserted->mItem=new T(*item);
|
|
mSize++;
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
void Block<T>::remove(Block<T> &oldBlock)
|
|
{
|
|
LONG size(oldBlock.size());
|
|
for(Index i=0;i<size;i++)remove(&oldBlock[i]);
|
|
}
|
|
|
|
template <class T>
|
|
void Block<T>::remove(const T *item)
|
|
{
|
|
if(!mContainer)return;
|
|
Container<T> *Thumb=mContainer;
|
|
Container<T> *Prev=0;
|
|
|
|
while(Thumb)
|
|
{
|
|
if(*item==*(Thumb->mItem))
|
|
break;
|
|
Prev=Thumb;
|
|
Thumb=Thumb->mNext;
|
|
}
|
|
if(!Thumb)return;
|
|
if(Thumb==mContainer)
|
|
{
|
|
if(Thumb->Next())
|
|
mContainer=Thumb->Next();
|
|
else mContainer=0;
|
|
}
|
|
else
|
|
{
|
|
if(Thumb->Next())
|
|
Prev->Next(Thumb->Next());
|
|
else
|
|
Prev->Next(0);
|
|
}
|
|
delete Thumb;
|
|
mSize--;
|
|
synchronizeBlock();
|
|
}
|
|
|
|
template <class T>
|
|
void Block<T>::remove(LONG itemIndex)
|
|
{
|
|
if(!mContainer)return;
|
|
Container<T> *Thumb=mContainer;
|
|
Container<T> *Prev=0;
|
|
|
|
for(Index i=0;i<itemIndex && Thumb;i++)
|
|
{
|
|
if(itemIndex==i)break;
|
|
Prev=Thumb;
|
|
Thumb=Thumb->Next();
|
|
}
|
|
if(!Thumb)return;
|
|
if(Thumb==mContainer)
|
|
{
|
|
if(Thumb->Next())
|
|
mContainer=Thumb->Next();
|
|
else mContainer=0;
|
|
}
|
|
else
|
|
{
|
|
if(Thumb->Next())
|
|
Prev->Next(Thumb->Next());
|
|
else
|
|
Prev->Next(0);
|
|
}
|
|
delete Thumb;
|
|
mSize--;
|
|
synchronizeBlock();
|
|
}
|
|
|
|
template <class T>
|
|
void Block<T>::synchronizeBlock(void)
|
|
{
|
|
Container<T> *Thumb=mLastObjectInserted=mContainer;
|
|
|
|
mLastIndexReferenced=-1;
|
|
mLastObjectReferenced=0;
|
|
while(Thumb)
|
|
{
|
|
mLastObjectInserted=Thumb;
|
|
Thumb=Thumb->Next();
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
LONG Block<T>::size(void)const
|
|
{
|
|
return mSize;
|
|
}
|
|
|
|
template <class T>
|
|
int Block<T>::operator=(Block<T> &someBlock)
|
|
{
|
|
LONG blockSize(someBlock.size());
|
|
|
|
if(!blockSize)return FALSE;
|
|
remove();
|
|
for(LONG i=0;i<blockSize;i++)insert(&someBlock[i]);
|
|
return TRUE;
|
|
}
|
|
|
|
template <class T>
|
|
int Block<T>::operator=(Vector<T> &someVector)
|
|
{
|
|
LONG blockSize(someVector.size());
|
|
|
|
if(!blockSize)return FALSE;
|
|
remove();
|
|
for(LONG i=0;i<blockSize;i++)insert(&someVector[i]);
|
|
return TRUE;
|
|
}
|
|
|
|
// **************************************************************
|
|
|
|
template <class T>
|
|
Container<T>::Container()
|
|
: mNext(0), mItem(0)
|
|
{
|
|
}
|
|
|
|
template <class T>
|
|
Container<T>::~Container()
|
|
{
|
|
delete mItem;
|
|
}
|
|
|
|
template <class T>
|
|
Container<T> *Container<T>::Next(void)
|
|
{
|
|
return mNext;
|
|
}
|
|
|
|
template <class T>
|
|
void Container<T>::Next(Container<T> *nextItem)
|
|
{
|
|
mNext=nextItem;
|
|
} |