3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #ifndef _INCLUDE_SOURCEMOD_FASTLINK_H_
34 #define _INCLUDE_SOURCEMOD_FASTLINK_H_
41 friend class iterator;
47 unsigned int freeNode;
51 FastLink(unsigned int maxsize) : m_Size(0), m_FirstLink(0), m_FreeNodes(0), m_LastLink(0), m_UsableIndex(0)
54 m_Nodes = new FastLinkNode[m_MaxSize+1];
57 bool push_back(const T & obj)
59 unsigned int new_node = GetFreeIndex();
64 m_Nodes[new_node].obj = obj;
65 m_Nodes[new_node].next = 0;
68 m_Nodes[new_node].prev = 0;
69 m_FirstLink = new_node;
70 m_LastLink = new_node;
72 m_Nodes[new_node].prev = m_LastLink;
73 m_Nodes[m_LastLink].next = new_node;
74 m_LastLink = new_node;
84 unsigned int GetFreeIndex()
88 return m_Nodes[m_FreeNodes--].freeNode;
90 if (m_UsableIndex >= m_MaxSize)
101 friend class FastLink;
108 iterator(const FastLink *_link, unsigned int _position)
111 position = _position;
114 bool operator ==(const iterator &where) const
116 return (link == where.link && position == where.position);
118 bool operator !=(const iterator &where) const
120 return (link != where.link || position != where.position);
124 return link->m_Nodes[position].obj;
126 const T & operator *() const
128 return link->m_Nodes[position].obj;
130 iterator & operator++()
132 position = link->m_Nodes[position].next;
136 const FastLink *link;
137 unsigned int position;
140 iterator begin() const
142 return iterator(this, m_FirstLink);
146 return iterator(this, 0);
148 void remove(const T & obj)
150 for (iterator iter=begin(); iter!=end(); ++iter)
159 iterator erase(const iterator &where)
161 /* Whoa, this better be right! */
162 assert(where.link == this);
164 iterator iter(where.link, where.position);
167 unsigned int index = where.position;
169 /* Each case is different, we have no sentinel.
170 * CASE: We're the HEAD AND the TAIL */
171 if (index == m_FirstLink
172 && index == m_LastLink)
178 else if (index == m_FirstLink)
180 m_FirstLink = m_Nodes[index].next;
181 m_Nodes[m_FirstLink].prev = 0;
184 else if (index == m_LastLink)
186 m_LastLink = m_Nodes[index].prev;
187 m_Nodes[m_LastLink].next = 0;
189 /* We're in the middle! */
192 /* Patch forward reference */
193 m_Nodes[m_Nodes[index].next].prev = m_Nodes[index].prev;
194 /* Patch backward reference */
195 m_Nodes[m_Nodes[index].prev].next = m_Nodes[index].next;
198 /* Add us to the free list */
199 m_Nodes[++m_FreeNodes].freeNode = index;
205 #if defined MENU_DEBUG
211 unsigned int m_FirstLink;
212 unsigned int m_FreeNodes;
213 unsigned int m_LastLink;
214 unsigned int m_MaxSize;
215 unsigned int m_UsableIndex;
216 FastLinkNode *m_Nodes;
219 #endif //_INCLUDE_SOURCEMOD_FASTLINK_H_