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
18 //This class is from CSDM for AMX Mod X
24 friend class iterator;
28 ListNode(const T & o) : obj(o) { };
35 ListNode *_Initialize()
37 ListNode *n = (ListNode *)malloc(sizeof(ListNode));
43 List() : m_Head(_Initialize()), m_Size(0)
46 List(const List &src) : m_Head(_Initialize()), m_Size(0)
49 for (iter=src.begin(); iter!=src.end(); iter++)
61 void push_back(const T &obj)
63 ListNode *node = new ListNode(obj);
67 //link in the node as the first item
73 node->prev = m_Head->prev;
75 m_Head->prev->next = node;
86 ListNode *node = m_Head->next;
90 while (node && node != m_Head)
100 return (m_Head->next == NULL);
104 return m_Head->prev->obj;
118 iterator(const List &src)
122 iterator(ListNode *n) : m_This(n)
125 iterator(const iterator &where)
127 m_This = where.m_This;
129 iterator & operator--()
132 m_This = m_This->prev;
136 iterator & operator++()
139 m_This = m_This->next;
143 iterator operator++(int)
147 m_This = m_This->next;
150 T & operator * () const
158 T * operator -> () const
160 return &(m_This->obj);
162 bool operator != (const iterator &where) const
164 return (m_This != where.m_This);
166 bool operator ==(const iterator &where) const
168 return (m_This == where.m_This);
174 iterator begin() const
177 return iterator(m_Head->next);
179 return iterator(m_Head);
183 return iterator(m_Head);
185 iterator erase(iterator &where)
187 ListNode *pNode = where.m_This;
188 iterator iter(where);
191 //If we are both the head and tail...
192 if (m_Head->next == pNode && m_Head->prev == pNode)
196 } else if (m_Head->next == pNode) {
197 //we are only the first
198 pNode->next->prev = m_Head;
199 m_Head->next = pNode->next;
200 } else if (m_Head->prev == pNode) {
202 pNode->prev->next = m_Head;
203 m_Head->prev = pNode->prev;
206 pNode->prev->next = pNode->next;
207 pNode->next->prev = pNode->prev;
216 void remove(const T & obj)
219 for (b=begin(); b!=end(); b++)
228 template <typename U>
229 iterator find(const U & equ)
232 for (iter=begin(); iter!=end(); iter++)
234 if ( (*iter) == equ )
239 List & operator =(List &src)
242 for (iter=src.begin(); iter!=src.end(); iter++)
243 push_back( (*iter) );
249 #endif //_INCLUDE_CSDM_LIST_H