1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2008 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;
101 void push_front(const T &obj)
103 insert(begin(), obj);
106 void push_sorted(const T &obj)
109 for (iter = begin(); iter != end(); ++iter)
127 ListNode *node = m_Head->next;
129 m_Head->next = m_Head;
130 m_Head->prev = m_Head;
132 // Iterate through the nodes until we find g_Head (the sentinel) again
133 while (node != m_Head)
143 return (m_Size == 0);
147 return m_Head->next->obj;
151 return m_Head->prev->obj;
165 iterator(const List &src)
169 iterator(ListNode *n) : m_This(n)
172 iterator(const iterator &where)
174 m_This = where.m_This;
177 iterator & operator--()
180 m_This = m_This->prev;
184 iterator operator--(int)
188 m_This = m_This->prev;
193 iterator & operator++()
196 m_This = m_This->next;
200 iterator operator++(int)
204 m_This = m_This->next;
208 const T & operator * () const
219 return &(m_This->obj);
221 const T * operator -> () const
223 return &(m_This->obj);
226 bool operator != (const iterator &where) const
228 return (m_This != where.m_This);
230 bool operator ==(const iterator &where) const
232 return (m_This == where.m_This);
237 return m_This != NULL;
243 iterator begin() const
245 return iterator(m_Head->next);
249 return iterator(m_Head);
251 iterator erase(iterator &where)
253 ListNode *pNode = where.m_This;
254 iterator iter(where);
258 // Works for all cases: empty list, erasing first element, erasing tail, erasing in the middle...
259 pNode->prev->next = pNode->next;
260 pNode->next->prev = pNode->prev;
268 iterator insert(iterator where, const T &obj)
270 // Insert obj right before where
272 ListNode *node = new ListNode(obj);
273 ListNode *pWhereNode = where.m_This;
275 pWhereNode->prev->next = node;
276 node->prev = pWhereNode->prev;
277 pWhereNode->prev = node;
278 node->next = pWhereNode;
282 return iterator(node);
286 void remove(const T & obj)
289 for (b=begin(); b!=end(); b++)
298 template <typename U>
299 iterator find(const U & equ) const
302 for (iter=begin(); iter!=end(); iter++)
304 if ( (*iter) == equ )
309 List & operator =(const List &src)
313 for (iter=src.begin(); iter!=src.end(); iter++)
314 push_back( (*iter) );
321 #endif //_INCLUDE_CSDM_LIST_H