1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): David "BAILOPAN" Anderson
8 * ============================
11 #ifndef _INCLUDE_SMM_LIST_H
12 #define _INCLUDE_SMM_LIST_H
20 //This class is from CSDM for AMX Mod X
22 A circular, doubly-linked list with one sentinel node
26 m_Head->next = m_Head;
27 m_Head->prev = m_Head;
48 friend class iterator;
52 ListNode(const T & o) : obj(o) { };
59 // Initializes the sentinel node.
60 // BAIL used malloc instead of new in order to bypass the need for a constructor.
61 ListNode *_Initialize()
63 ListNode *n = (ListNode *)malloc(sizeof(ListNode));
69 List() : m_Head(_Initialize()), m_Size(0)
72 List(const List &src) : m_Head(_Initialize()), m_Size(0)
75 for (iter=src.begin(); iter!=src.end(); iter++)
82 // Don't forget to free the sentinel
89 void push_back(const T &obj)
91 ListNode *node = new ListNode(obj);
93 node->prev = m_Head->prev;
95 m_Head->prev->next = node;
107 ListNode *node = m_Head->next;
109 m_Head->next = m_Head;
110 m_Head->prev = m_Head;
112 // Iterate through the nodes until we find g_Head (the sentinel) again
113 while (node != m_Head)
123 return (m_Size == 0);
127 return m_Head->prev->obj;
141 iterator(const List &src)
145 iterator(ListNode *n) : m_This(n)
148 iterator(const iterator &where)
150 m_This = where.m_This;
153 iterator & operator--()
156 m_This = m_This->prev;
160 iterator operator--(int)
164 m_This = m_This->prev;
169 iterator & operator++()
172 m_This = m_This->next;
176 iterator operator++(int)
180 m_This = m_This->next;
184 const T & operator * () const
195 return &(m_This->obj);
197 const T * operator -> () const
199 return &(m_This->obj);
202 bool operator != (const iterator &where) const
204 return (m_This != where.m_This);
206 bool operator ==(const iterator &where) const
208 return (m_This == where.m_This);
214 iterator begin() const
216 return iterator(m_Head->next);
220 return iterator(m_Head);
222 iterator erase(iterator &where)
224 ListNode *pNode = where.m_This;
225 iterator iter(where);
229 // Works for all cases: empty list, erasing first element, erasing tail, erasing in the middle...
230 pNode->prev->next = pNode->next;
231 pNode->next->prev = pNode->prev;
239 iterator insert(iterator where, const T &obj)
241 // Insert obj right before where
243 ListNode *node = new ListNode(obj);
244 ListNode *pWhereNode = where.m_This;
246 pWhereNode->prev->next = node;
247 node->prev = pWhereNode->prev;
248 pWhereNode->prev = node;
249 node->next = pWhereNode;
253 return iterator(node);
257 void remove(const T & obj)
260 for (b=begin(); b!=end(); b++)
269 template <typename U>
270 iterator find(const U & equ) const
273 for (iter=begin(); iter!=end(); iter++)
275 if ( (*iter) == equ )
280 List & operator =(const List &src)
284 for (iter=src.begin(); iter!=src.end(); iter++)
285 push_back( (*iter) );
291 #endif //_INCLUDE_CSDM_LIST_H