#ifndef _LINKED_LIST_H_
#define _LINKED_LIST_H_
template <class C> class CList;
template <class C> class CListIterator;
template <class C> class CListConstIterator;
template <class C> class CListElmt {
friend class CList<C>;
friend class CListIterator<C>;
friend class CListConstIterator<C>;
private:
CListElmt(C* refP, int id) : _refP(refP), _nextP(0), _prevP(0), _id(id)
{
};
private:
int _id;
C *_refP;
#if defined(AIX)
CListElmt<C> *_nextP;
CListElmt<C> *_prevP;
#else
CListElmt *_nextP;
CListElmt *_prevP;
#endif
};
template <class C> class CList {
friend class CListIterator<C>;
friend class CListConstIterator<C>;
private:
CListElmt<C>* _headP;
CListElmt<C>* _tailP;
CListElmt<C>* Lookup(C * memberP)
{
CListElmt<C> *curEltP = _headP;
while (curEltP)
{
if (curEltP->_refP == memberP)
break;
curEltP = curEltP->_nextP;
};
return(curEltP);
};
int _nextId;
int _numElts;
int autoDestroy;
C* Remove(CListElmt<C> *elmtP)
{
C *recP = NULL;
if (elmtP)
{
if (elmtP->_nextP)
elmtP->_nextP->_prevP = elmtP->_prevP;
if (elmtP->_prevP)
elmtP->_prevP->_nextP = elmtP->_nextP;
if (elmtP == _tailP)
_tailP = elmtP->_prevP;
if (elmtP == _headP)
_headP = _headP->_nextP;
recP = elmtP->_refP;
_numElts--;
delete elmtP;
};
if ( (1==autoDestroy) && recP) {
delete recP;
recP = NULL;
}
return(recP);
};
public:
CList() : _headP(0), _tailP(0), _nextId(0), _numElts(0), autoDestroy(0)
{
};
void setAutoDestroy(int setting)
{
autoDestroy = setting;
};
virtual ~CList()
{
while (_headP)
Remove(_headP);
};
int NextId()
{
return _nextId;
};
int Append(C* refP)
{
CListElmt<C> *newElmtP = new CListElmt<C>(refP, _nextId++);
newElmtP->_prevP = _tailP;
if (_tailP)
_tailP->_nextP = newElmtP;
if (_headP == 0)
_headP = newElmtP;
_tailP = newElmtP;
_numElts++;
return(newElmtP->_id);
};
int Member(C* memberP)
{
return(Lookup(memberP) != 0);
};
int Delete(C* memberP)
{
CListElmt<C> *listElmtP = Lookup(memberP);
if (listElmtP)
{
(void) Remove(listElmtP);
return(1);
}
return(0);
};
int NumEntries() const { return _numElts; }
C* Find(C* memberP)
{
CListElmt<C> *curEltP = _headP;
while (curEltP)
{
if (curEltP->_refP == memberP)
break;
curEltP = curEltP->_nextP;
};
return(curEltP ? curEltP->_refP : 0);
};
C* First() { return(_headP ? _headP->_refP : 0); }
C* Last() { return(_tailP ? _tailP->_refP : 0); }
};
template <class C> class CListIterator {
private:
CList<C>* _listP;
int _curId;
public:
CListIterator(CList<C>* linkedListP) : _listP(linkedListP), _curId(-1)
{
};
virtual ~CListIterator()
{
_listP = NULL;
};
C* operator++()
{
C *valueP = NULL;
CListElmt<C> *curEltP = _listP->_headP;
while (curEltP)
{
if (curEltP->_id > _curId)
{
_curId = curEltP->_id;
return(curEltP->_refP);
}
curEltP = curEltP->_nextP;
}
_curId = -1;
return(NULL);
};
void operator()()
{
_curId = -1;
};
};
template <class C> class CListConstIterator {
private:
const CList<C>* _listP;
int _curId;
public:
CListConstIterator(const CList<C>* linkedListP)
: _listP(linkedListP), _curId(-1)
{
};
virtual ~CListConstIterator()
{
_listP = NULL;
};
const C* operator++()
{
const C *valueP = NULL;
const CListElmt<C> *curEltP = _listP->_headP;
while (curEltP)
{
if (curEltP->_id > _curId)
{
_curId = curEltP->_id;
return(curEltP->_refP);
}
curEltP = curEltP->_nextP;
}
_curId = -1;
return(NULL);
};
void operator()()
{
_curId = -1;
};
};
#endif