|
ds@1952
|
1 |
/** |
|
ds@1952
|
2 |
* vim: set ts=4 : |
|
ds@1952
|
3 |
* ============================================================================= |
|
ds@1952
|
4 |
* SourceMod |
|
ds@1952
|
5 |
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
|
ds@1952
|
6 |
* ============================================================================= |
|
ds@1952
|
7 |
* |
|
ds@1952
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
ds@1952
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
ds@1952
|
10 |
* Free Software Foundation. |
|
ds@1952
|
11 |
* |
|
ds@1952
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
ds@1952
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
ds@1952
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
ds@1952
|
15 |
* details. |
|
ds@1952
|
16 |
* |
|
ds@1952
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
ds@1952
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
ds@1952
|
19 |
* |
|
ds@1952
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
ds@1952
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
ds@1952
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
ds@1952
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
ds@1952
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
ds@1952
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
ds@1952
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
ds@1952
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
ds@1952
|
28 |
* |
|
ds@1952
|
29 |
* Version: $Id$ |
|
ds@1952
|
30 |
*/ |
|
ds@1952
|
31 |
|
|
ds@1952
|
32 |
#ifndef _INCLUDE_SOURCEMOD_FASTLINK_H_ |
|
ds@1952
|
33 |
#define _INCLUDE_SOURCEMOD_FASTLINK_H_ |
|
ds@1952
|
34 |
|
|
ds@1952
|
35 |
#include <assert.h> |
|
ds@1952
|
36 |
|
|
ds@1952
|
37 |
template <typename T> |
|
ds@1952
|
38 |
class FastLink |
|
ds@1952
|
39 |
{ |
|
ds@1952
|
40 |
friend class iterator; |
|
ds@1952
|
41 |
public: |
|
ds@1952
|
42 |
struct FastLinkNode |
|
ds@1952
|
43 |
{ |
|
ds@1952
|
44 |
unsigned int prev; |
|
ds@1952
|
45 |
unsigned int next; |
|
ds@1952
|
46 |
unsigned int freeNode; |
|
ds@1952
|
47 |
T obj; |
|
ds@1952
|
48 |
}; |
|
ds@1952
|
49 |
public: |
|
ds@1952
|
50 |
FastLink(unsigned int maxsize) : m_Size(0), m_FirstLink(0), m_FreeNodes(0), m_LastLink(0), m_UsableIndex(0) |
|
ds@1952
|
51 |
{ |
|
ds@1952
|
52 |
m_MaxSize = maxsize; |
|
ds@1952
|
53 |
m_Nodes = new FastLinkNode[m_MaxSize+1]; |
|
ds@1952
|
54 |
} |
|
ds@1952
|
55 |
public: |
|
ds@1952
|
56 |
bool push_back(const T & obj) |
|
ds@1952
|
57 |
{ |
|
ds@1952
|
58 |
unsigned int new_node = GetFreeIndex(); |
|
ds@1952
|
59 |
if (!new_node) |
|
ds@1952
|
60 |
{ |
|
ds@1952
|
61 |
return false; |
|
ds@1952
|
62 |
} |
|
ds@1952
|
63 |
m_Nodes[new_node].obj = obj; |
|
ds@1952
|
64 |
m_Nodes[new_node].next = 0; |
|
ds@1952
|
65 |
if (!m_FirstLink) |
|
ds@1952
|
66 |
{ |
|
ds@1952
|
67 |
m_Nodes[new_node].prev = 0; |
|
ds@1952
|
68 |
m_FirstLink = new_node; |
|
ds@1952
|
69 |
m_LastLink = new_node; |
|
ds@1952
|
70 |
} else { |
|
ds@1952
|
71 |
m_Nodes[new_node].prev = m_LastLink; |
|
ds@1952
|
72 |
m_Nodes[m_LastLink].next = new_node; |
|
ds@1952
|
73 |
m_LastLink = new_node; |
|
ds@1952
|
74 |
} |
|
ds@1952
|
75 |
m_Size++; |
|
ds@1952
|
76 |
return true; |
|
ds@1952
|
77 |
} |
|
ds@1952
|
78 |
size_t size() const |
|
ds@1952
|
79 |
{ |
|
ds@1952
|
80 |
return m_Size; |
|
ds@1952
|
81 |
} |
|
ds@1952
|
82 |
private: |
|
ds@1952
|
83 |
unsigned int GetFreeIndex() |
|
ds@1952
|
84 |
{ |
|
ds@1952
|
85 |
if (m_FreeNodes) |
|
ds@1952
|
86 |
{ |
|
ds@1952
|
87 |
return m_Nodes[m_FreeNodes--].freeNode; |
|
ds@1952
|
88 |
} else { |
|
ds@1952
|
89 |
if (m_UsableIndex >= m_MaxSize) |
|
ds@1952
|
90 |
{ |
|
ds@1952
|
91 |
return 0; |
|
ds@1952
|
92 |
} |
|
ds@1952
|
93 |
m_UsableIndex++; |
|
ds@1952
|
94 |
return m_UsableIndex; |
|
ds@1952
|
95 |
} |
|
ds@1952
|
96 |
} |
|
ds@1952
|
97 |
public: |
|
ds@1952
|
98 |
class iterator |
|
ds@1952
|
99 |
{ |
|
ds@1952
|
100 |
friend class FastLink; |
|
ds@1952
|
101 |
public: |
|
ds@1952
|
102 |
iterator() |
|
ds@1952
|
103 |
{ |
|
ds@1952
|
104 |
link = NULL; |
|
ds@1952
|
105 |
position = 0; |
|
ds@1952
|
106 |
} |
|
ds@1952
|
107 |
iterator(const FastLink *_link, unsigned int _position) |
|
ds@1952
|
108 |
{ |
|
ds@1952
|
109 |
link = _link; |
|
ds@1952
|
110 |
position = _position; |
|
ds@1952
|
111 |
} |
|
ds@1952
|
112 |
public: |
|
ds@1952
|
113 |
bool operator ==(const iterator &where) const |
|
ds@1952
|
114 |
{ |
|
ds@1952
|
115 |
return (link == where.link && position == where.position); |
|
ds@1952
|
116 |
} |
|
ds@1952
|
117 |
bool operator !=(const iterator &where) const |
|
ds@1952
|
118 |
{ |
|
ds@1952
|
119 |
return (link != where.link || position != where.position); |
|
ds@1952
|
120 |
} |
|
ds@1952
|
121 |
T & operator *() |
|
ds@1952
|
122 |
{ |
|
ds@1952
|
123 |
return link->m_Nodes[position].obj; |
|
ds@1952
|
124 |
} |
|
ds@1952
|
125 |
const T & operator *() const |
|
ds@1952
|
126 |
{ |
|
ds@1952
|
127 |
return link->m_Nodes[position].obj; |
|
ds@1952
|
128 |
} |
|
ds@1952
|
129 |
iterator & operator++() |
|
ds@1952
|
130 |
{ |
|
ds@1952
|
131 |
position = link->m_Nodes[position].next; |
|
ds@1952
|
132 |
return *this; |
|
ds@1952
|
133 |
} |
|
ds@1952
|
134 |
private: |
|
ds@1952
|
135 |
const FastLink *link; |
|
ds@1952
|
136 |
unsigned int position; |
|
ds@1952
|
137 |
}; |
|
ds@1952
|
138 |
public: |
|
ds@1952
|
139 |
iterator begin() const |
|
ds@1952
|
140 |
{ |
|
ds@1952
|
141 |
return iterator(this, m_FirstLink); |
|
ds@1952
|
142 |
} |
|
ds@1952
|
143 |
iterator end() const |
|
ds@1952
|
144 |
{ |
|
ds@1952
|
145 |
return iterator(this, 0); |
|
ds@1952
|
146 |
} |
|
ds@1952
|
147 |
void remove(const T & obj) |
|
ds@1952
|
148 |
{ |
|
ds@1952
|
149 |
for (iterator iter=begin(); iter!=end(); ++iter) |
|
ds@1952
|
150 |
{ |
|
ds@1952
|
151 |
if ((*iter) == obj) |
|
ds@1952
|
152 |
{ |
|
ds@1952
|
153 |
erase(iter); |
|
ds@1952
|
154 |
return; |
|
ds@1952
|
155 |
} |
|
ds@1952
|
156 |
} |
|
ds@1952
|
157 |
} |
|
ds@1952
|
158 |
iterator erase(const iterator &where) |
|
ds@1952
|
159 |
{ |
|
ds@1952
|
160 |
/* Whoa, this better be right! */ |
|
ds@1952
|
161 |
assert(where.link == this); |
|
ds@1952
|
162 |
|
|
ds@1952
|
163 |
iterator iter(where.link, where.position); |
|
ds@1952
|
164 |
++iter; |
|
ds@1952
|
165 |
|
|
ds@1952
|
166 |
unsigned int index = where.position; |
|
ds@1952
|
167 |
|
|
ds@1952
|
168 |
/* Each case is different, we have no sentinel. |
|
ds@1952
|
169 |
* CASE: We're the HEAD AND the TAIL */ |
|
ds@1952
|
170 |
if (index == m_FirstLink |
|
ds@1952
|
171 |
&& index == m_LastLink) |
|
ds@1952
|
172 |
{ |
|
ds@1952
|
173 |
m_FirstLink = 0; |
|
ds@1952
|
174 |
m_LastLink = 0; |
|
ds@1952
|
175 |
} |
|
ds@1952
|
176 |
/* We're the HEAD */ |
|
ds@1952
|
177 |
else if (index == m_FirstLink) |
|
ds@1952
|
178 |
{ |
|
ds@1952
|
179 |
m_FirstLink = m_Nodes[index].next; |
|
ds@1952
|
180 |
m_Nodes[m_FirstLink].prev = 0; |
|
ds@1952
|
181 |
} |
|
ds@1952
|
182 |
/* We're the TAIL */ |
|
ds@1952
|
183 |
else if (index == m_LastLink) |
|
ds@1952
|
184 |
{ |
|
ds@1952
|
185 |
m_LastLink = m_Nodes[index].prev; |
|
ds@1952
|
186 |
m_Nodes[m_LastLink].next = 0; |
|
ds@1952
|
187 |
} |
|
ds@1952
|
188 |
/* We're in the middle! */ |
|
ds@1952
|
189 |
else |
|
ds@1952
|
190 |
{ |
|
ds@1952
|
191 |
/* Patch forward reference */ |
|
ds@1952
|
192 |
m_Nodes[m_Nodes[index].next].prev = m_Nodes[index].prev; |
|
ds@1952
|
193 |
/* Patch backward reference */ |
|
ds@1952
|
194 |
m_Nodes[m_Nodes[index].prev].next = m_Nodes[index].next; |
|
ds@1952
|
195 |
} |
|
ds@1952
|
196 |
|
|
ds@1952
|
197 |
/* Add us to the free list */ |
|
ds@1952
|
198 |
m_Nodes[++m_FreeNodes].freeNode = index; |
|
ds@1952
|
199 |
|
|
ds@1952
|
200 |
m_Size--; |
|
ds@1952
|
201 |
|
|
ds@1952
|
202 |
return iter; |
|
ds@1952
|
203 |
} |
|
ds@1952
|
204 |
#if defined MENU_DEBUG |
|
ds@1952
|
205 |
public: |
|
ds@1952
|
206 |
#else |
|
ds@1952
|
207 |
private: |
|
ds@1952
|
208 |
#endif |
|
ds@1952
|
209 |
size_t m_Size; |
|
ds@1952
|
210 |
unsigned int m_FirstLink; |
|
ds@1952
|
211 |
unsigned int m_FreeNodes; |
|
ds@1952
|
212 |
unsigned int m_LastLink; |
|
ds@1952
|
213 |
unsigned int m_MaxSize; |
|
ds@1952
|
214 |
unsigned int m_UsableIndex; |
|
ds@1952
|
215 |
FastLinkNode *m_Nodes; |
|
ds@1952
|
216 |
}; |
|
ds@1952
|
217 |
|
|
ds@1952
|
218 |
#endif //_INCLUDE_SOURCEMOD_FASTLINK_H_ |