OpenBarnyard
 
Loading...
Searching...
No Matches
TNodeList.h
Go to the documentation of this file.
1#pragma once
2
4
5template <class T>
7{
8public:
9 class TNode
10 {
11 protected:
12 friend TNodeList;
13
14 public:
15 constexpr TNode()
16 : m_pNext( TSTATICCAST( T, this ) ), m_pPrev( TSTATICCAST( T, this ) ), m_pList( TNULL )
17 {}
18
19 constexpr TNode( const TNode& a_rcNode )
20 : m_pNext( a_rcNode.m_pNext ), m_pPrev( a_rcNode.m_pPrev ), m_pList( a_rcNode.m_pList )
21 {}
22
23 TNode( TNode&& a_rNode )
24 : m_pNext( a_rNode.m_pNext ), m_pPrev( a_rNode.m_pPrev ), m_pList( a_rNode.m_pList )
25 {
26 a_rNode.m_pList = TNULL;
27 a_rNode.m_pNext = TNULL;
28 a_rNode.m_pPrev = TNULL;
29 }
30
31 T* Next() const { return m_pNext; }
32 T* Prev() const { return m_pPrev; }
33
34 void Remove() { GetList()->Remove( TSTATICCAST( T, this ) ); }
35
36 void SetList( TNodeList* list ) { m_pList = list; }
37 TNodeList* GetList() const { return m_pList; }
38
39 TBOOL IsLinked() const { return m_pList != TNULL; }
40
41 protected:
45 };
46
48 {
49 public:
50 constexpr Iterator()
51 : m_pPtr( TNULL )
52 {}
53
54 constexpr Iterator( T* a_pPtr )
55 : m_pPtr( a_pPtr )
56 {}
57
58 constexpr Iterator( const Iterator& other )
59 : m_pPtr( other.m_pPtr )
60 {}
61
62 T* Get() const
63 {
64 return m_pPtr;
65 }
66
67 void operator=( const Iterator& other )
68 {
69 m_pPtr = other.m_pPtr;
70 }
71
72 void operator=( T* pPtr )
73 {
74 m_pPtr = pPtr;
75 }
76
77 T* operator->() const
78 {
79 TASSERT( m_pPtr != TNULL );
80 return m_pPtr;
81 }
82
83 operator T*() const
84 {
85 TASSERT( m_pPtr != TNULL );
86 return m_pPtr;
87 }
88
89 TBOOL operator==( const Iterator& a_rcIterator ) const
90 {
91 return m_pPtr == a_rcIterator.m_pPtr;
92 }
93
95 {
96 TASSERT( m_pPtr != TNULL );
97 Iterator old = m_pPtr;
98 m_pPtr = m_pPtr->Next();
99 return old;
100 }
101
103 {
104 TASSERT( m_pPtr != TNULL );
105 Iterator old = m_pPtr;
106 m_pPtr = m_pPtr->Prev();
107 return old;
108 }
109
111 {
112 TASSERT( m_pPtr != TNULL );
113 m_pPtr = m_pPtr->Next();
114 return Iterator{ m_pPtr };
115 }
116
118 {
119 TASSERT( m_pPtr != TNULL );
120 m_pPtr = m_pPtr->Prev();
121 return Iterator{ m_pPtr };
122 }
123
124 protected:
126 };
127
128public:
130 {
131 m_iCount = 0;
132 }
133
135 {
136 DeleteAll();
137 }
138
139 void InsertAfter( T* insertAfter, T* newNode )
140 {
141 TASSERT( !newNode->IsLinked() );
142 newNode->SetList( this );
143 newNode->m_pNext = insertAfter->m_pNext;
144 newNode->m_pPrev = insertAfter;
145 insertAfter->m_pNext->m_pPrev = newNode;
146 insertAfter->m_pNext = newNode;
147 m_iCount++;
148 }
149
150 void InsertBefore( T* insertBefore, T* newNode )
151 {
152 TASSERT( !newNode->IsLinked() );
153 newNode->SetList( this );
154 newNode->m_pNext = insertBefore;
155 newNode->m_pPrev = insertBefore->m_pPrev;
156 insertBefore->m_pPrev->m_pNext = newNode;
157 insertBefore->m_pPrev = newNode;
158 m_iCount++;
159 }
160
161 T* Remove( T* pNode )
162 {
163 TASSERT( pNode->GetList() == this );
164 pNode->SetList( TNULL );
165 pNode->m_pPrev->m_pNext = pNode->m_pNext;
166 pNode->m_pNext->m_pPrev = pNode->m_pPrev;
167 pNode->m_pNext = pNode;
168 pNode->m_pPrev = pNode;
169 m_iCount--;
170 TASSERT( pNode->IsLinked() == TFALSE );
171 return pNode;
172 }
173
175 {
176 while ( !IsEmpty() )
177 {
178 Remove( m_oRoot.Next() );
179 }
180 }
181
183 {
184 if ( !IsEmpty() )
185 {
186 return Remove( m_oRoot.Next() );
187 }
188
189 return TNULL;
190 }
191
193 {
194 if ( !IsEmpty() )
195 {
196 return Remove( m_oRoot.Prev() );
197 }
198
199 return TNULL;
200 }
201
202 void DeleteHead() { Delete( Head() ); }
203 void DeleteTail() { Delete( Tail() ); }
204
206 {
207 while ( !IsEmpty() )
208 {
209 DeleteHead();
210 }
211 }
212
213 void Delete( T* pNode )
214 {
215 Remove( pNode );
216 delete pNode;
217 }
218
219 void InsertHead( T* a_pNode ) { InsertAfter( End(), a_pNode ); }
220 void InsertTail( T* a_pNode ) { InsertBefore( End(), a_pNode ); }
221
222 TBOOL IsEmpty() const { return Begin() == End(); }
223 TBOOL IsValid( const T* a_pNode ) const { return a_pNode != TNULL && a_pNode->m_pList == this; }
224
225 TSIZE Count() const { return m_iCount; }
226
227 Iterator Head() const { return m_oRoot.m_pNext; }
228 Iterator Tail() const { return m_oRoot.m_pPrev; }
229
230 Iterator Begin() const { return m_oRoot.m_pNext; }
231 Iterator End() const { return TSTATICCAST( T, &m_oRoot ); }
232
233 Iterator RBegin() const { return m_oRoot.m_pPrev; }
234 Iterator REnd() const { return TSTATICCAST( T, &m_oRoot ); }
235
236protected:
237 mutable TNode m_oRoot;
239};
240
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
bool TBOOL
Definition Typedefs.h:6
T * Remove(T *pNode)
Definition TNodeList.h:161
void DeleteTail()
Definition TNodeList.h:203
TINT m_iCount
Definition TNodeList.h:238
void InsertTail(T *a_pNode)
Definition TNodeList.h:220
TNode m_oRoot
Definition TNodeList.h:237
Iterator End() const
Definition TNodeList.h:231
TBOOL IsEmpty() const
Definition TNodeList.h:222
Iterator RemoveTail()
Definition TNodeList.h:192
void Delete(T *pNode)
Definition TNodeList.h:213
void RemoveAll()
Definition TNodeList.h:174
Iterator RBegin() const
Definition TNodeList.h:233
void DeleteAll()
Definition TNodeList.h:205
void InsertBefore(T *insertBefore, T *newNode)
Definition TNodeList.h:150
Iterator Head() const
Definition TNodeList.h:227
TSIZE Count() const
Definition TNodeList.h:225
TBOOL IsValid(const T *a_pNode) const
Definition TNodeList.h:223
void InsertHead(T *a_pNode)
Definition TNodeList.h:219
Iterator Begin() const
Definition TNodeList.h:230
void DeleteHead()
Definition TNodeList.h:202
Iterator REnd() const
Definition TNodeList.h:234
void InsertAfter(T *insertAfter, T *newNode)
Definition TNodeList.h:139
Iterator RemoveHead()
Definition TNodeList.h:182
Iterator Tail() const
Definition TNodeList.h:228
void SetList(TNodeList *list)
Definition TNodeList.h:36
constexpr TNode()
Definition TNodeList.h:15
constexpr TNode(const TNode &a_rcNode)
Definition TNodeList.h:19
TNodeList * m_pList
Definition TNodeList.h:42
T * Next() const
Definition TNodeList.h:31
TNode(TNode &&a_rNode)
Definition TNodeList.h:23
TNodeList * GetList() const
Definition TNodeList.h:37
T * Prev() const
Definition TNodeList.h:32
TBOOL IsLinked() const
Definition TNodeList.h:39
constexpr Iterator(const Iterator &other)
Definition TNodeList.h:58
TBOOL operator==(const Iterator &a_rcIterator) const
Definition TNodeList.h:89
constexpr Iterator(T *a_pPtr)
Definition TNodeList.h:54
Iterator operator--(TINT)
Definition TNodeList.h:102
T * Get() const
Definition TNodeList.h:62
Iterator operator++(TINT)
Definition TNodeList.h:94
constexpr Iterator()
Definition TNodeList.h:50
T * operator->() const
Definition TNodeList.h:77
Iterator operator--()
Definition TNodeList.h:117
void operator=(T *pPtr)
Definition TNodeList.h:72
void operator=(const Iterator &other)
Definition TNodeList.h:67
Iterator operator++()
Definition TNodeList.h:110