00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _LINK_H_V001_INCLUDED_
00011 #define _LINK_H_V001_INCLUDED_
00012
00015 class Linkable
00016 {
00017 private:
00018 Linkable* pNext;
00019
00020 public:
00021
00022
00023 Linkable( Linkable* next = NULL ) { pNext = next; }
00024 virtual ~Linkable() {}
00025
00026 inline Linkable* GetNext(void) { return pNext; }
00027 inline void Insert(Linkable* prv, Linkable* nxt) { if(prv) prv->pNext = this; pNext = nxt; }
00028 inline void Remove(Linkable* prv) { if(prv) prv->pNext = pNext; }
00029 };
00032 class DLinkable
00033 {
00034 private:
00035 DLinkable* pPrev;
00036 DLinkable* pNext;
00037
00038 public:
00039
00040
00041 DLinkable( DLinkable* prev = NULL, DLinkable* next = NULL ) { pPrev = prev; pNext = next; }
00042 virtual ~DLinkable() {}
00043
00044 inline DLinkable* GetNext(void) { return pNext; }
00045 inline DLinkable* GetPrev(void) { return pPrev; }
00046 inline void InsertAfter( DLinkable* ptr ) { Insert( ptr, ptr? ptr->pNext : NULL ); }
00047 inline void InsertBefore( DLinkable* ptr ) { Insert( ptr? ptr->pNext : NULL, ptr ); }
00048
00049 inline void Insert(DLinkable* prv, DLinkable* nxt)
00050 {
00051 if(( pPrev = prv )) pPrev->pNext = this;
00052 if(( pNext = nxt )) pNext->pPrev = this;
00053 }
00054
00055 inline void Remove(void)
00056 {
00057 if( pPrev ) pPrev->pNext = pNext;
00058 if( pNext ) pNext->pPrev = pPrev;
00059 }
00060 };
00063
00064 class SList
00065 {
00066 private:
00067 Linkable* pHead;
00068 Linkable* pTail;
00069 int32s Count;
00070
00071 public:
00072 SList() { Clear(); }
00073 virtual ~SList() {}
00074
00075 inline void Clear(void) { pHead = pTail = NULL; Count = 0; }
00076 inline int32s GetCount(void) { return Count; }
00077 inline Linkable* GetHead(void) { return pHead; }
00078 inline Linkable* GetTail(void) { return pTail; }
00079
00080 inline void Append( Linkable* ptr ) { Insert( pTail, ptr, NULL ); }
00081 inline void Insert( Linkable* prv, Linkable* ptr, Linkable* nxt )
00082 {
00083 if( ptr )
00084 {
00085 if( prv == NULL || nxt == pHead ) pHead = ptr;
00086 if( nxt == NULL || prv == pTail ) pTail = ptr;
00087 ptr->Insert(prv, nxt);
00088 Count++;
00089 }
00090 }
00091
00092 inline Linkable* RemoveHead(void) { return Remove( NULL, pHead ); }
00093 inline Linkable* Remove( Linkable* prv, Linkable* ptr )
00094 {
00095 if( ptr )
00096 {
00097 if( ptr == pHead ) pHead = pHead->GetNext();
00098 if( ptr == pTail ) pTail = prv;
00099 ptr->Remove( prv );
00100 Count--;
00101 }
00102 return ptr;
00103 }
00104 void Iterator( void (*pf)(Linkable*) )
00105 {
00106 for( Linkable* ptr = pHead; ptr; ptr=ptr->GetNext() ) pf( ptr );
00107 }
00108 };
00111
00112 class DList
00113 {
00114 private:
00115 DLinkable* pHead;
00116 DLinkable* pTail;
00117 int32s Count;
00118
00119 public:
00120 DList() { Clear(); }
00121 virtual ~DList() {}
00122
00123 inline void Clear(void) { pHead = pTail = NULL; Count = 0; }
00124 inline int32s GetCount(void) { return Count; }
00125 inline DLinkable* GetHead(void) { return pHead; }
00126 inline DLinkable* GetTail(void) { return pTail; }
00127
00128 inline void Append( DLinkable* ptr ) { Insert( pTail, ptr, NULL ); }
00129 inline void InsertHead( DLinkable* ptr ) { Insert( NULL, ptr, pHead ); }
00130
00131 inline void Insert( DLinkable* prv, DLinkable* ptr, DLinkable* nxt )
00132 {
00133 if( ptr )
00134 {
00135 if( prv == NULL || nxt == pHead ) pHead = ptr;
00136 if( nxt == NULL || prv == pTail ) pTail = ptr;
00137 ptr->Insert(prv, nxt);
00138 Count++;
00139 }
00140 }
00141
00142 inline DLinkable* RemoveHead(void) { return Remove( pHead ); }
00143 inline DLinkable* RemoveTail(void) { return Remove( pTail ); }
00144 inline DLinkable* Remove( DLinkable* ptr )
00145 {
00146 if( ptr )
00147 {
00148 if( ptr == pHead ) pHead = pHead->GetNext();
00149 if( ptr == pTail ) pTail = pTail->GetPrev();
00150 ptr->Remove();
00151 Count--;
00152 }
00153 return ptr;
00154 }
00155
00156
00157 template < class T > void Iterator( void (T::*pf)(DLinkable*) )
00158 {
00159 for( DLinkable* ptr = pHead; ptr; ptr=ptr->GetNext() ) pf( ptr );
00160 }
00161 };
00164
00165 class CircDList
00166 {
00167 private:
00168 DLinkable* pFirst;
00169 int32s Count;
00170
00171 public:
00172 CircDList() { Clear(); }
00173 virtual ~CircDList() {}
00174
00175 inline void Clear(void) { pFirst = NULL; Count = 0; }
00176 inline int32s GetCount(void) { return Count; }
00177 inline DLinkable* GetFirst(void) { return pFirst; }
00178
00179 inline void Append( DLinkable* ptr ) { Insert( pFirst? pFirst->GetPrev(): NULL, ptr, pFirst ); }
00180 inline void Insert( DLinkable* prv, DLinkable* ptr, DLinkable* nxt )
00181 {
00182 if( ptr )
00183 {
00184 if( !prv || !nxt ) (pFirst = ptr)->Insert(ptr, ptr);
00185 else ptr->Insert(prv, nxt);
00186 Count++;
00187 }
00188 }
00189
00190 inline DLinkable* RemoveFirst(void) { return Remove( pFirst ); }
00191 inline DLinkable* Remove( DLinkable* ptr )
00192 {
00193 if( ptr )
00194 {
00195 if( ptr == pFirst ) pFirst = pFirst->GetNext();
00196 if( ptr == pFirst ) pFirst = NULL;
00197 ptr->Remove();
00198 Count--;
00199 }
00200 return ptr;
00201 }
00202
00203 template < class T > void Iterator( void (T::*pf)(DLinkable*) )
00204 {
00205 DLinkable* ptr = pFirst;
00206 do { pf( ptr ); } while( (ptr = ptr->GetNext()) != pFirst );
00207 }
00208 };
00209
00210 #endif