OpenBarnyard
 
Loading...
Searching...
No Matches
T2SList.h
Go to the documentation of this file.
1#pragma once
2#include "Toshi/Typedefs.h"
3
5
7{
8public:
9 class Node
10 {
11 public:
12 friend class T2GenericSList;
13
14 public:
16 {
17 Reset();
18 }
19
20 void Reset()
21 {
22 m_pNext = this;
23 }
24
25 Node* Next() const
26 {
27 return m_pNext;
28 }
29
31 {
32 return m_pNext != this;
33 }
34
35 void InsertAfter( Node* a_pNode )
36 {
37 TASSERT( !IsLinked() );
38 m_pNext = a_pNode->m_pNext;
39 a_pNode->m_pNext = this;
40 }
41
42 private:
43 Node* m_pNext;
44 };
45
46public:
47 T2GenericSList() = default;
48 ~T2GenericSList() = default;
49
50 // Calling this won't clear the list so the items will still be linked to each other!
51 // Use this only if you're sure the nodes are freed
52 void Reset()
53 {
54 m_oRoot.Reset();
55 }
56
57 Node* Begin() const
58 {
59 return m_oRoot.Next();
60 }
61
62 Node* RBegin() const
63 {
64 return m_oRoot.Next();
65 }
66
67 Node* End() const
68 {
69 return &m_oRoot;
70 }
71
72 Node* REnd() const
73 {
74 return &m_oRoot;
75 }
76
78 {
79 return m_oRoot.m_pNext != &m_oRoot;
80 }
81
82 TBOOL IsEmpty() const
83 {
84 return m_oRoot.m_pNext == &m_oRoot;
85 }
86
87 Node* Back() const
88 {
89 Node* pBack = End();
90
91 for ( auto it = Begin(); it != End(); it = it->Next() )
92 {
93 pBack = it;
94 }
95
96 return pBack;
97 }
98
100 {
101 Node* pResult = End();
102
103 for ( auto it = Begin(); it != a_pNode; it = it->Next() )
104 {
105 TASSERT( it != End() );
106 pResult = it;
107 }
108
109 return pResult;
110 }
111
112 void PushBack( Node* a_pNode )
113 {
114 a_pNode->InsertAfter( Back() );
115 }
116
117 void PushFront( Node* a_pNode )
118 {
119 a_pNode->InsertAfter( &m_oRoot );
120 }
121
123 {
124 Node* pBack = Back();
125 Node* pNodeBefore = FindNodeBefore( pBack );
126
127 pNodeBefore->m_pNext = &m_oRoot;
128 pBack->m_pNext = pBack;
129
130 return pBack;
131 }
132
134 {
135 Node* pNode = Begin();
136 m_oRoot.m_pNext = pNode->m_pNext;
137 pNode->m_pNext = pNode;
138 return pNode;
139 }
140
141 Node* Erase( Node* a_pFrom, Node* a_pTo )
142 {
143 Node* pNodeBefore = FindNodeBefore( a_pFrom );
144
145 while ( a_pFrom != a_pTo )
146 {
147 Node* pNext = a_pFrom->m_pNext;
148 a_pFrom->m_pNext = a_pFrom;
149 a_pFrom = pNext;
150 }
151
152 pNodeBefore->m_pNext = a_pTo;
153 return a_pTo;
154 }
155
156 void Clear()
157 {
158 Node* pNode = Begin();
159
160 while ( pNode != End() )
161 {
162 Node* pNext = pNode->Next();
163 pNode->m_pNext = pNode;
164 pNode = pNext;
165 }
166
167 m_oRoot.m_pNext = &m_oRoot;
168 }
169
170 TUINT Size() const
171 {
172 TUINT uiSize = 0;
173
174 for ( auto it = Begin(); it != End(); it = it->Next() )
175 {
176 uiSize++;
177 }
178
179 return uiSize;
180 }
181
182 Node* Transfer( Node* a_pNode, T2GenericSList& a_rList )
183 {
184 Node* pNodeBefore = FindNodeBefore( a_pNode );
185 pNodeBefore->m_pNext = a_pNode->m_pNext;
186 a_pNode->m_pNext = a_pNode;
187 a_rList.PushBack( a_pNode );
188
189 return pNodeBefore;
190 }
191
192private:
193 mutable Node m_oRoot;
194};
195
196//-----------------------------------------------------------------------------
197// TOSHI 2.0 - Singly linked list
198// Usage example:
199//
200// class AMyClass : public Toshi::T2SList<AMyClass>::Node { ... };
201//
202// Toshi::T2SList<AMyClass> list;
203// list.PushBack(new AMyClass);
204//-----------------------------------------------------------------------------
205template <class T, TINT Unknown = 0>
207{
208public:
210 {
211 public:
212 T* Next() const
213 {
215 }
216
217 T* Prev() const
218 {
219 TASSERT( !"Nah, you can't do this" );
220 return TNULL;
221 }
222 };
223
226
227public:
230
231 void Delete( Iterator iter )
232 {
233 iter->Remove();
234 delete TSTATICCAST( T, iter );
235 }
236
238 {
239 while ( Begin() != End() )
240 {
241 Delete( Begin() );
242 }
243 }
244
245 Iterator Begin() const { return T2GenericSList::Begin(); }
246 Iterator RBegin() const { return T2GenericSList::RBegin(); }
247
248 Iterator End() const { return T2GenericSList::End(); }
249 Iterator REnd() const { return T2GenericSList::REnd(); }
250
251 Iterator Head() const { return T2GenericSList::Begin(); }
252 Iterator Back() const { return T2GenericSList::Back(); }
253
254 Iterator FindNodeBefore( Iterator a_Node ) { return T2GenericSList::FindNodeBefore( a_Node ); }
255
256 Iterator PopBack() { return T2GenericSList::PopBack(); }
257 Iterator PopFront() { return T2GenericSList::PopFront(); }
258
259 Iterator Erase( Iterator a_Iterator ) { return T2GenericSList::Erase( a_Iterator, a_Iterator.Next() ); }
260 Iterator Erase( Iterator a_From, Iterator a_To ) { return T2GenericSList::Erase( a_From, a_To ); }
261 Iterator Transfer( Node* a_pNode, T2GenericSList& a_rList ) { return T2GenericSList::Transfer( a_pNode, a_rList ); }
262};
263
264//-----------------------------------------------------------------------------
265// Purpose: wraps typename T making it T2SList::Node
266//-----------------------------------------------------------------------------
267template <typename T>
268class T2SListNodeWrapper : public T2SList<T2SListNodeWrapper<T>>::Node
269{
270public:
271 using Type = T;
272
273public:
274 // constructors/destructor
275 constexpr T2SListNodeWrapper() = default;
276
277 constexpr T2SListNodeWrapper( T* a_pValue )
278 : m_pValue( a_pValue ) {}
279
281
282 constexpr T* GetNodeValue() { return m_pValue; }
283 constexpr const T* GetNodeValue() const { return m_pValue; }
284 constexpr T* operator->() { return m_pValue; }
285 constexpr const T* operator->() const { return m_pValue; }
286
287 constexpr void SetNodeValue( T* a_pValue ) { m_pValue = a_pValue; }
288
289 constexpr T2SListNodeWrapper& operator=( T* a_pValue )
290 {
291 SetNodeValue( a_pValue );
292 return *this;
293 }
294
295private:
296 T* m_pValue;
297};
298
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TOSHI_API
Definition Defines.h:41
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
unsigned int TUINT
Definition Typedefs.h:8
#define TNULL
Definition Typedefs.h:23
bool TBOOL
Definition Typedefs.h:6
Node * Back() const
Definition T2SList.h:87
Node * Transfer(Node *a_pNode, T2GenericSList &a_rList)
Definition T2SList.h:182
void PushBack(Node *a_pNode)
Definition T2SList.h:112
TBOOL IsLinked() const
Definition T2SList.h:77
Node * End() const
Definition T2SList.h:67
void Clear()
Definition T2SList.h:156
T2GenericSList()=default
Node * Begin() const
Definition T2SList.h:57
Node * REnd() const
Definition T2SList.h:72
TBOOL IsEmpty() const
Definition T2SList.h:82
Node * Erase(Node *a_pFrom, Node *a_pTo)
Definition T2SList.h:141
void Reset()
Definition T2SList.h:52
void PushFront(Node *a_pNode)
Definition T2SList.h:117
~T2GenericSList()=default
Node * PopFront()
Definition T2SList.h:133
Node * RBegin() const
Definition T2SList.h:62
TUINT Size() const
Definition T2SList.h:170
Node * PopBack()
Definition T2SList.h:122
Node * FindNodeBefore(Node *a_pNode)
Definition T2SList.h:99
Node * Next() const
Definition T2SList.h:25
void InsertAfter(Node *a_pNode)
Definition T2SList.h:35
TBOOL IsLinked() const
Definition T2SList.h:30
friend class T2GenericSList
Definition T2SList.h:12
Iterator Erase(Iterator a_Iterator)
Definition T2SList.h:259
~T2SList()
Definition T2SList.h:229
Iterator PopBack()
Definition T2SList.h:256
Iterator End() const
Definition T2SList.h:248
T2_DEFINE_ITERATOR_FRIEND()
Iterator FindNodeBefore(Iterator a_Node)
Definition T2SList.h:254
T2SList()
Definition T2SList.h:228
Iterator Back() const
Definition T2SList.h:252
Iterator Erase(Iterator a_From, Iterator a_To)
Definition T2SList.h:260
Iterator PopFront()
Definition T2SList.h:257
Iterator RBegin() const
Definition T2SList.h:246
Iterator Head() const
Definition T2SList.h:251
void DeleteAll()
Definition T2SList.h:237
void Delete(Iterator iter)
Definition T2SList.h:231
Iterator Begin() const
Definition T2SList.h:245
Iterator Transfer(Node *a_pNode, T2GenericSList &a_rList)
Definition T2SList.h:261
Iterator REnd() const
Definition T2SList.h:249
T2_DEFINE_ITERATOR(T, T2GenericSList::Node)
T * Prev() const
Definition T2SList.h:217
T * Next() const
Definition T2SList.h:212
~T2SListNodeWrapper()=default
constexpr const T * GetNodeValue() const
Definition T2SList.h:283
constexpr T * operator->()
Definition T2SList.h:284
constexpr T2SListNodeWrapper & operator=(T *a_pValue)
Definition T2SList.h:289
constexpr T2SListNodeWrapper()=default
constexpr const T * operator->() const
Definition T2SList.h:285
constexpr T * GetNodeValue()
Definition T2SList.h:282
constexpr void SetNodeValue(T *a_pValue)
Definition T2SList.h:287
constexpr T2SListNodeWrapper(T *a_pValue)
Definition T2SList.h:277