3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
16 #ifndef _INCLUDE_SOURCEMOD_FASTLINK_H_
17 #define _INCLUDE_SOURCEMOD_FASTLINK_H_
24 friend class iterator;
30 unsigned int freeNode;
34 FastLink(unsigned int maxsize) : m_Size(0), m_FirstLink(0), m_FreeNodes(0), m_LastLink(0)
37 m_Nodes = new FastLinkNode[m_MaxSize+1];
40 bool push_back(const T & obj)
42 unsigned int new_node = GetFreeIndex();
47 m_Nodes[new_node].obj = obj;
48 m_Nodes[new_node].next = 0;
51 m_Nodes[new_node].prev = 0;
52 m_FirstLink = new_node;
53 m_LastLink = new_node;
55 m_Nodes[new_node].prev = m_LastLink;
56 m_Nodes[m_LastLink].next = new_node;
57 m_LastLink = new_node;
67 unsigned int GetFreeIndex()
71 return m_Nodes[m_FreeNodes--].freeNode;
73 if (m_LastLink >= m_MaxSize)
77 return m_LastLink + 1;
83 friend class FastLink;
90 iterator(const FastLink *_link, unsigned int _position)
96 bool operator ==(const iterator &where) const
98 return (link == where.link && position == where.position);
100 bool operator !=(const iterator &where) const
102 return (link != where.link || position != where.position);
106 return link->m_Nodes[position].obj;
108 const T & operator *() const
110 return link->m_Nodes[position].obj;
112 iterator & operator++()
114 position = link->m_Nodes[position].next;
118 const FastLink *link;
119 unsigned int position;
122 iterator begin() const
124 return iterator(this, m_FirstLink);
128 return iterator(this, 0);
130 void remove(const T & obj)
132 for (iterator iter=begin(); iter!=end(); ++iter)
141 iterator erase(const iterator &where)
143 /* Whoa, this better be right! */
144 assert(where.link == this);
146 iterator iter(where.link, where.position);
149 unsigned int index = where.position;
151 /* Each case is different, we have no sentinel.
152 * CASE: We're the HEAD AND the TAIL */
153 if (index == m_FirstLink
154 && index == m_LastLink)
160 else if (index == m_FirstLink)
162 m_FirstLink = m_Nodes[index].next;
163 m_Nodes[m_FirstLink].prev = 0;
166 else if (index == m_LastLink)
168 m_LastLink = m_Nodes[index].prev;
169 m_Nodes[m_LastLink].next = 0;
171 /* We're in the middle! */
174 /* Patch forward reference */
175 m_Nodes[m_Nodes[index].next].prev = m_Nodes[index].prev;
176 /* Patch backward reference */
177 m_Nodes[m_Nodes[index].prev].next = m_Nodes[index].next;
180 /* Add us to the free list */
181 m_Nodes[++m_FreeNodes].freeNode = index;
189 unsigned int m_FirstLink;
190 unsigned int m_FreeNodes;
191 unsigned int m_LastLink;
192 unsigned int m_MaxSize;
193 FastLinkNode *m_Nodes;
196 #endif //_INCLUDE_SOURCEMOD_FASTLINK_H_