12#ifndef TT_INCLUDE__SLIST_H
13#define TT_INCLUDE__SLIST_H
17#include "engine_vector.h"
22 public AutoPoolClass<GenericSLNode, 256>
27 void Internal_Set_Next(
void *n)
31 void Internal_Set_Data(
void *d)
37 GenericSLNode(
void *obj)
42 GenericSLNode(
void* data, GenericSLNode* next)
49template<
class T>
class SList;
51class SLNode :
public GenericSLNode
55 friend class SList<T>;
56 void Set_Next(SLNode<T>* n)
60 SLNode<T> *Next()
const
62 return (SLNode<T>*)NodeNext;
69 SLNode(T *obj) : GenericSLNode(obj) {}
70 SLNode(T* data, SLNode<T>* next) : GenericSLNode(data, next) {}
85 HeadNode(0), TailNode(0)
95 virtual bool Add_Head(T* data)
101 SLNode<T> *temp =
new SLNode<T>(data);
102 temp->Set_Next(HeadNode);
112 virtual bool Add_Head(SList<T>& list)
114 if (list.HeadNode == NULL)
118 SLNode<T> *addpoint = NULL;
119 for (SLNode<T> *cur = list.HeadNode; cur; cur = cur->Next())
123 SLNode<T> *temp =
new SLNode<T>(cur->Data());
124 temp->Set_Next(addpoint->Next());
125 addpoint->Set_Next(temp);
130 Add_Head(cur->Data());
138 virtual bool Add_Tail(T* data)
144 SLNode<T> *temp =
new SLNode<T> (data);
145 if (HeadNode == NULL)
147 HeadNode = TailNode = temp;
151 TailNode->Set_Next(temp);
158 virtual bool Add_Tail(SList<T>& list)
160 if (list.HeadNode == NULL)
164 for (SLNode<T> *cur = list.HeadNode; cur; cur = cur->Next())
166 Add_Tail(cur->Data());
172 virtual T* Remove_Head()
174 if (HeadNode == NULL)
178 SLNode<T> *temp = HeadNode;
179 HeadNode = HeadNode->Next();
180 if (HeadNode == NULL)
184 T *data = temp->Data();
190 virtual T* Remove_Tail()
192 if (HeadNode == NULL)
196 T* data = TailNode->Data();
197 return (Remove(data) ? data : (T*)NULL);
201 virtual bool Remove(
const T* element)
203 if (element == NULL || HeadNode == NULL)
207 if (HeadNode->Data() == element)
209 return Remove_Head() != NULL ? true :
false;
212 for (cur = HeadNode; cur->Next() && cur->Next()->Data() != element; cur=cur->Next())
215 if (cur->Next() != NULL && cur->Next()->Data() == element)
217 SLNode<T> *temp = cur->Next();
218 cur->Set_Next(temp->Next());
219 if (temp == TailNode)
230 virtual void Remove_All()
233 for (SLNode<T> *cur = HeadNode; cur; cur = next)
238 HeadNode = TailNode = NULL;
242 virtual bool Insert_Before(T* newnode,
const T* oldnode)
248 if (oldnode == NULL || HeadNode == NULL || HeadNode->Data() == oldnode)
250 return Add_Head(newnode);
253 for (cur=HeadNode; cur->Next() && cur->Next()->Data() != oldnode; cur=cur->Next())
256 if (cur->Next() != NULL && cur->Next()->Data() == oldnode)
258 SLNode<T> *temp =
new SLNode<T> (newnode);
259 temp->Set_Next(cur->Next());
266 virtual bool Insert_After(T* newnode,
const T* oldnode)
272 if (oldnode == NULL || HeadNode == NULL)
274 return(Add_Head(newnode));
277 for (cur = HeadNode; cur && cur->Data() != oldnode; cur = cur->Next())
280 if (cur != NULL && cur->Data() == oldnode)
284 return(Add_Tail(newnode));
286 SLNode<T> *temp =
new SLNode<T>(newnode);
287 temp->Set_Next(cur->Next());
295 virtual bool Is_Empty()
const
301 virtual uint32 Get_Count()
const
305 for (SLNode<T>* node = HeadNode; node; node = node->Next())
312 SLNode<T>* Head()
const
317 SLNode<T>* Tail()
const
322 SLNode<T> *Find_Node(T * data)
const
325 for (cur = HeadNode;cur && cur->Data() != data;cur = cur->Next())
331 void insertBefore(T* data, SLNode<T>& node)
334 SLNode<T>* nextNode =
new SLNode<T>(node.Data(), node.Next());
335 node.NodeNext = nextNode;
336 node.NodeData = data;
337 if (&node == TailNode)
341 void insertAfter(T* data, SLNode<T>& node)
343 SLNode<T>* nextNode =
new SLNode<T>(data, node.Next());
344 node.NodeNext = nextNode;
345 if (&node == TailNode)
355 SLIterator(SList<T>& list): Node(list.Head()) { };
357 bool IsDone()
const {
return !Node->Next(); }
358 T* operator ->() {
return Node->Data(); }
359 T* operator * () {
return Node->Data(); }
360 SLIterator<T>& operator ++ () { Node = Node->Next(); }