diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llassoclist.h | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to '')
-rw-r--r-- | linden/indra/llcommon/llassoclist.h | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llassoclist.h b/linden/indra/llcommon/llassoclist.h new file mode 100644 index 0000000..d1d3eb8 --- /dev/null +++ b/linden/indra/llcommon/llassoclist.h | |||
@@ -0,0 +1,297 @@ | |||
1 | /** | ||
2 | * @file llassoclist.h | ||
3 | * @brief LLAssocList class header file | ||
4 | * | ||
5 | * Copyright (c) 2001-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_LLASSOCLIST_H | ||
29 | #define LL_LLASSOCLIST_H | ||
30 | |||
31 | //------------------------------------------------------------------------ | ||
32 | // LLAssocList is an associative list container class. | ||
33 | // | ||
34 | // The implementation is a single linked list. | ||
35 | // Both index and value objects are stored by value (not reference). | ||
36 | // If pointer values are specified for index and/or value, this | ||
37 | // container does NOT assume ownership of the referenced objects, | ||
38 | // and does NOT delete() them on removal or destruction of the container. | ||
39 | // | ||
40 | // Note that operations are generally not optimized, and may of them | ||
41 | // are O(n) complexity. | ||
42 | //------------------------------------------------------------------------ | ||
43 | |||
44 | #include <iostream> | ||
45 | |||
46 | template<class INDEX_TYPE, class VALUE_TYPE> | ||
47 | class LLAssocList | ||
48 | { | ||
49 | private: | ||
50 | // internal list node type | ||
51 | class Node | ||
52 | { | ||
53 | public: | ||
54 | Node(const INDEX_TYPE &index, const VALUE_TYPE &value, Node *next) | ||
55 | { | ||
56 | mIndex = index; | ||
57 | mValue = value; | ||
58 | mNext = next; | ||
59 | } | ||
60 | ~Node() { } | ||
61 | INDEX_TYPE mIndex; | ||
62 | VALUE_TYPE mValue; | ||
63 | Node *mNext; | ||
64 | }; | ||
65 | |||
66 | // head of the linked list | ||
67 | Node *mHead; | ||
68 | |||
69 | public: | ||
70 | // Constructor | ||
71 | LLAssocList() | ||
72 | { | ||
73 | mHead = NULL; | ||
74 | } | ||
75 | |||
76 | // Destructor | ||
77 | ~LLAssocList() | ||
78 | { | ||
79 | removeAll(); | ||
80 | } | ||
81 | |||
82 | // Returns TRUE if list is empty. | ||
83 | BOOL isEmpty() | ||
84 | { | ||
85 | return (mHead == NULL); | ||
86 | } | ||
87 | |||
88 | // Returns the number of items in the list. | ||
89 | U32 length() | ||
90 | { | ||
91 | U32 count = 0; | ||
92 | for ( Node *node = mHead; | ||
93 | node; | ||
94 | node = node->mNext ) | ||
95 | { | ||
96 | count++; | ||
97 | } | ||
98 | return count; | ||
99 | } | ||
100 | |||
101 | // Removes item with the specified index. | ||
102 | BOOL remove( const INDEX_TYPE &index ) | ||
103 | { | ||
104 | if (!mHead) | ||
105 | return FALSE; | ||
106 | |||
107 | if (mHead->mIndex == index) | ||
108 | { | ||
109 | Node *node = mHead; | ||
110 | mHead = mHead->mNext; | ||
111 | delete node; | ||
112 | return TRUE; | ||
113 | } | ||
114 | |||
115 | for ( Node *prev = mHead; | ||
116 | prev->mNext; | ||
117 | prev = prev->mNext ) | ||
118 | { | ||
119 | if (prev->mNext->mIndex == index) | ||
120 | { | ||
121 | Node *node = prev->mNext; | ||
122 | prev->mNext = prev->mNext->mNext; | ||
123 | delete node; | ||
124 | return TRUE; | ||
125 | } | ||
126 | } | ||
127 | return FALSE; | ||
128 | } | ||
129 | |||
130 | // Removes all items from the list. | ||
131 | void removeAll() | ||
132 | { | ||
133 | while ( mHead ) | ||
134 | { | ||
135 | Node *node = mHead; | ||
136 | mHead = mHead->mNext; | ||
137 | delete node; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | // Adds a new item to the head of the list, | ||
142 | // removing any existing item with same index. | ||
143 | void addToHead( const INDEX_TYPE &index, const VALUE_TYPE &value ) | ||
144 | { | ||
145 | remove(index); | ||
146 | Node *node = new Node(index, value, mHead); | ||
147 | mHead = node; | ||
148 | } | ||
149 | |||
150 | // Adds a new item to the end of the list, | ||
151 | // removing any existing item with the same index. | ||
152 | void addToTail( const INDEX_TYPE &index, const VALUE_TYPE &value ) | ||
153 | { | ||
154 | remove(index); | ||
155 | Node *node = new Node(index, value, NULL); | ||
156 | if (!mHead) | ||
157 | { | ||
158 | mHead = node; | ||
159 | return; | ||
160 | } | ||
161 | for ( Node *prev=mHead; | ||
162 | prev; | ||
163 | prev=prev->mNext ) | ||
164 | { | ||
165 | if (!prev->mNext) | ||
166 | { | ||
167 | prev->mNext=node; | ||
168 | return; | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | |||
173 | // Sets the value of a specified index. | ||
174 | // If index does not exist, a new value will be added only if | ||
175 | // 'addIfNotFound' is set to TRUE. | ||
176 | // Returns TRUE if successful. | ||
177 | BOOL setValue( const INDEX_TYPE &index, const VALUE_TYPE &value, BOOL addIfNotFound=FALSE ) | ||
178 | { | ||
179 | VALUE_TYPE *valueP = getValue(index); | ||
180 | if (valueP) | ||
181 | { | ||
182 | *valueP = value; | ||
183 | return TRUE; | ||
184 | } | ||
185 | if (!addIfNotFound) | ||
186 | return FALSE; | ||
187 | addToTail(index, value); | ||
188 | return TRUE; | ||
189 | } | ||
190 | |||
191 | // Sets the ith value in the list. | ||
192 | // A new value will NOT be addded, if the ith value does not exist. | ||
193 | // Returns TRUE if successful. | ||
194 | BOOL setValueAt( U32 i, const VALUE_TYPE &value ) | ||
195 | { | ||
196 | VALUE_TYPE *valueP = getValueAt(i); | ||
197 | if (valueP) | ||
198 | { | ||
199 | *valueP = value; | ||
200 | return TRUE; | ||
201 | } | ||
202 | return FALSE; | ||
203 | } | ||
204 | |||
205 | // Returns a pointer to the value for the specified index, | ||
206 | // or NULL if no item found. | ||
207 | VALUE_TYPE *getValue( const INDEX_TYPE &index ) | ||
208 | { | ||
209 | for ( Node *node = mHead; | ||
210 | node; | ||
211 | node = node->mNext ) | ||
212 | { | ||
213 | if (node->mIndex == index) | ||
214 | return &node->mValue; | ||
215 | } | ||
216 | return NULL; | ||
217 | } | ||
218 | |||
219 | // Returns a pointer to the ith value in the list, or | ||
220 | // NULL if i is not valid. | ||
221 | VALUE_TYPE *getValueAt( U32 i ) | ||
222 | { | ||
223 | U32 count = 0; | ||
224 | for ( Node *node = mHead; | ||
225 | node; | ||
226 | node = node->mNext ) | ||
227 | { | ||
228 | if (count == i) | ||
229 | return &node->mValue; | ||
230 | count++; | ||
231 | } | ||
232 | return NULL; | ||
233 | } | ||
234 | |||
235 | // Returns a pointer to the index for the specified index, | ||
236 | // or NULL if no item found. | ||
237 | INDEX_TYPE *getIndex( const INDEX_TYPE &index ) | ||
238 | { | ||
239 | for ( Node *node = mHead; | ||
240 | node; | ||
241 | node = node->mNext ) | ||
242 | { | ||
243 | if (node->mIndex == index) | ||
244 | return &node->mIndex; | ||
245 | } | ||
246 | return NULL; | ||
247 | } | ||
248 | |||
249 | // Returns a pointer to the ith index in the list, or | ||
250 | // NULL if i is not valid. | ||
251 | INDEX_TYPE *getIndexAt( U32 i ) | ||
252 | { | ||
253 | U32 count = 0; | ||
254 | for ( Node *node = mHead; | ||
255 | node; | ||
256 | node = node->mNext ) | ||
257 | { | ||
258 | if (count == i) | ||
259 | return &node->mIndex; | ||
260 | count++; | ||
261 | } | ||
262 | return NULL; | ||
263 | } | ||
264 | |||
265 | // Returns a pointer to the value for the specified index, | ||
266 | // or NULL if no item found. | ||
267 | VALUE_TYPE *operator[](const INDEX_TYPE &index) | ||
268 | { | ||
269 | return getValue(index); | ||
270 | } | ||
271 | |||
272 | // Returns a pointer to the ith value in the list, or | ||
273 | // NULL if i is not valid. | ||
274 | VALUE_TYPE *operator[](U32 i) | ||
275 | { | ||
276 | return getValueAt(i); | ||
277 | } | ||
278 | |||
279 | // Prints the list contents to the specified stream. | ||
280 | friend std::ostream &operator<<( std::ostream &os, LLAssocList &map ) | ||
281 | { | ||
282 | os << "{"; | ||
283 | for ( Node *node = map.mHead; | ||
284 | node; | ||
285 | node = node->mNext ) | ||
286 | { | ||
287 | os << "<" << node->mIndex << ", " << node->mValue << ">"; | ||
288 | if (node->mNext) | ||
289 | os << ", "; | ||
290 | } | ||
291 | os << "}"; | ||
292 | |||
293 | return os; | ||
294 | } | ||
295 | }; | ||
296 | |||
297 | #endif // LL_LLASSOCLIST_H | ||