1 /* ======== SourceMM ========
2 * Copyright (C) 2004-2005 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
19 //This class is from CSDM for AMX Mod X
25 friend class iterator;
29 ListNode(const T & o) : obj(o) { };
36 ListNode *_Initialize()
38 ListNode *n = (ListNode *)malloc(sizeof(ListNode));
44 List() : m_Head(_Initialize()), m_Size(0)
47 List(const List &src) : m_Head(_Initialize()), m_Size(0)
50 for (iter=src.begin(); iter!=src.end(); iter++)
62 void push_back(const T &obj)
64 ListNode *node = new ListNode(obj);
68 //link in the node as the first item
74 node->prev = m_Head->prev;
76 m_Head->prev->next = node;
87 ListNode *node = m_Head->next;
91 while (node && node != m_Head)
101 return (m_Head->next == NULL);
105 return m_Head->prev->obj;
119 iterator(const List &src)
123 iterator(ListNode *n) : m_This(n)
126 iterator(const iterator &where)
128 m_This = where.m_This;
131 iterator & operator--()
134 m_This = m_This->prev;
138 iterator operator--(int)
142 m_This = m_This->prev;
147 iterator & operator++()
150 m_This = m_This->next;
154 iterator operator++(int)
158 m_This = m_This->next;
162 const T & operator * () const
173 return &(m_This->obj);
175 const T * operator -> () const
177 return &(m_This->obj);
180 bool operator != (const iterator &where) const
182 return (m_This != where.m_This);
184 bool operator ==(const iterator &where) const
186 return (m_This == where.m_This);
192 iterator begin() const
195 return iterator(m_Head->next);
197 return iterator(m_Head);
201 return iterator(m_Head);
203 iterator erase(iterator &where)
205 ListNode *pNode = where.m_This;
206 iterator iter(where);
209 //If we are both the head and tail...
210 if (m_Head->next == pNode && m_Head->prev == pNode)
214 } else if (m_Head->next == pNode) {
215 //we are only the first
216 pNode->next->prev = m_Head;
217 m_Head->next = pNode->next;
218 } else if (m_Head->prev == pNode) {
220 pNode->prev->next = m_Head;
221 m_Head->prev = pNode->prev;
224 pNode->prev->next = pNode->next;
225 pNode->next->prev = pNode->prev;
234 void remove(const T & obj)
237 for (b=begin(); b!=end(); b++)
246 template <typename U>
247 iterator find(const U & equ)
250 for (iter=begin(); iter!=end(); iter++)
252 if ( (*iter) == equ )
257 List & operator =(List &src)
261 for (iter=src.begin(); iter!=src.end(); iter++)
262 push_back( (*iter) );
268 #endif //_INCLUDE_CSDM_LIST_H