aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/lllinkedqueue.h
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/lllinkedqueue.h
parentREADME.txt (diff)
downloadmeta-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 'linden/indra/llcommon/lllinkedqueue.h')
-rw-r--r--linden/indra/llcommon/lllinkedqueue.h310
1 files changed, 310 insertions, 0 deletions
diff --git a/linden/indra/llcommon/lllinkedqueue.h b/linden/indra/llcommon/lllinkedqueue.h
new file mode 100644
index 0000000..699667a
--- /dev/null
+++ b/linden/indra/llcommon/lllinkedqueue.h
@@ -0,0 +1,310 @@
1/**
2 * @file lllinkedqueue.h
3 * @brief Declaration of linked queue classes.
4 *
5 * Copyright (c) 2003-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_LLLINKEDQUEUE_H
29#define LL_LLLINKEDQUEUE_H
30
31#include "llerror.h"
32
33// node that actually contains the data
34template <class DATA_TYPE> class LLLinkedQueueNode
35{
36public:
37 DATA_TYPE mData;
38 LLLinkedQueueNode *mNextp;
39 LLLinkedQueueNode *mPrevp;
40
41
42public:
43 LLLinkedQueueNode();
44 LLLinkedQueueNode(const DATA_TYPE data);
45
46 // destructor does not, by default, destroy associated data
47 // however, the mDatap must be NULL to ensure that we aren't causing memory leaks
48 ~LLLinkedQueueNode();
49};
50
51
52
53template <class DATA_TYPE> class LLLinkedQueue
54{
55
56public:
57 LLLinkedQueue();
58
59 // destructor destroys list and nodes, but not data in nodes
60 ~LLLinkedQueue();
61
62 // Puts at end of FIFO
63 void push(const DATA_TYPE data);
64
65 // Takes off front of FIFO
66 BOOL pop(DATA_TYPE &data);
67 BOOL peek(DATA_TYPE &data);
68
69 void reset();
70
71 S32 getLength() const;
72
73 BOOL isEmpty() const;
74
75 BOOL remove(const DATA_TYPE data);
76
77 BOOL checkData(const DATA_TYPE data) const;
78
79private:
80 // add node to end of list
81 // set mCurrentp to mQueuep
82 void addNodeAtEnd(LLLinkedQueueNode<DATA_TYPE> *nodep);
83
84private:
85 LLLinkedQueueNode<DATA_TYPE> mHead; // head node
86 LLLinkedQueueNode<DATA_TYPE> mTail; // tail node
87 S32 mLength;
88};
89
90
91//
92// Nodes
93//
94
95template <class DATA_TYPE>
96LLLinkedQueueNode<DATA_TYPE>::LLLinkedQueueNode() :
97 mData(), mNextp(NULL), mPrevp(NULL)
98{ }
99
100template <class DATA_TYPE>
101LLLinkedQueueNode<DATA_TYPE>::LLLinkedQueueNode(const DATA_TYPE data) :
102 mData(data), mNextp(NULL), mPrevp(NULL)
103{ }
104
105template <class DATA_TYPE>
106LLLinkedQueueNode<DATA_TYPE>::~LLLinkedQueueNode()
107{ }
108
109
110//
111// Queue itself
112//
113
114template <class DATA_TYPE>
115LLLinkedQueue<DATA_TYPE>::LLLinkedQueue()
116: mHead(),
117 mTail(),
118 mLength(0)
119{ }
120
121
122// destructor destroys list and nodes, but not data in nodes
123template <class DATA_TYPE>
124LLLinkedQueue<DATA_TYPE>::~LLLinkedQueue()
125{
126 reset();
127}
128
129
130// put data into a node and stick it at the end of the list
131template <class DATA_TYPE>
132void LLLinkedQueue<DATA_TYPE>::push(const DATA_TYPE data)
133{
134 // make the new node
135 LLLinkedQueueNode<DATA_TYPE> *nodep = new LLLinkedQueueNode<DATA_TYPE>(data);
136
137 addNodeAtEnd(nodep);
138}
139
140
141// search the list starting at mHead.mNextp and remove the link with mDatap == data
142// set mCurrentp to mQueuep, or NULL if mQueuep points to node with mDatap == data
143// return TRUE if found, FALSE if not found
144template <class DATA_TYPE>
145BOOL LLLinkedQueue<DATA_TYPE>::remove(const DATA_TYPE data)
146{
147 BOOL b_found = FALSE;
148
149 LLLinkedQueueNode<DATA_TYPE> *currentp = mHead.mNextp;
150
151 while (currentp)
152 {
153 if (currentp->mData == data)
154 {
155 b_found = TRUE;
156
157 // if there is a next one, fix it
158 if (currentp->mNextp)
159 {
160 currentp->mNextp->mPrevp = currentp->mPrevp;
161 }
162 else // we are at end of list
163 {
164 mTail.mPrevp = currentp->mPrevp;
165 }
166
167 // if there is a previous one, fix it
168 if (currentp->mPrevp)
169 {
170 currentp->mPrevp->mNextp = currentp->mNextp;
171 }
172 else // we are at beginning of list
173 {
174 mHead.mNextp = currentp->mNextp;
175 }
176
177 // remove the node
178 delete currentp;
179 mLength--;
180 break;
181 }
182 currentp = currentp->mNextp;
183 }
184
185 return b_found;
186}
187
188
189// remove all nodes from the list but do not delete associated data
190template <class DATA_TYPE>
191void LLLinkedQueue<DATA_TYPE>::reset()
192{
193 LLLinkedQueueNode<DATA_TYPE> *currentp;
194 LLLinkedQueueNode<DATA_TYPE> *nextp;
195 currentp = mHead.mNextp;
196
197 while (currentp)
198 {
199 nextp = currentp->mNextp;
200 delete currentp;
201 currentp = nextp;
202 }
203
204 // reset mHead and mCurrentp
205 mHead.mNextp = NULL;
206 mTail.mPrevp = NULL;
207 mLength = 0;
208}
209
210template <class DATA_TYPE>
211S32 LLLinkedQueue<DATA_TYPE>::getLength() const
212{
213 return mLength;
214}
215
216template <class DATA_TYPE>
217BOOL LLLinkedQueue<DATA_TYPE>::isEmpty() const
218{
219 return mLength <= 0;
220}
221
222// check to see if data is in list
223// set mCurrentp and mQueuep to the target of search if found, otherwise set mCurrentp to mQueuep
224// return TRUE if found, FALSE if not found
225template <class DATA_TYPE>
226BOOL LLLinkedQueue<DATA_TYPE>::checkData(const DATA_TYPE data) const
227{
228 LLLinkedQueueNode<DATA_TYPE> *currentp = mHead.mNextp;
229
230 while (currentp)
231 {
232 if (currentp->mData == data)
233 {
234 return TRUE;
235 }
236 currentp = currentp->mNextp;
237 }
238 return FALSE;
239}
240
241template <class DATA_TYPE>
242BOOL LLLinkedQueue<DATA_TYPE>::pop(DATA_TYPE &data)
243{
244 LLLinkedQueueNode<DATA_TYPE> *currentp;
245
246 currentp = mHead.mNextp;
247 if (!currentp)
248 {
249 return FALSE;
250 }
251
252 mHead.mNextp = currentp->mNextp;
253 if (currentp->mNextp)
254 {
255 currentp->mNextp->mPrevp = currentp->mPrevp;
256 }
257 else
258 {
259 mTail.mPrevp = currentp->mPrevp;
260 }
261
262 data = currentp->mData;
263 delete currentp;
264 mLength--;
265 return TRUE;
266}
267
268template <class DATA_TYPE>
269BOOL LLLinkedQueue<DATA_TYPE>::peek(DATA_TYPE &data)
270{
271 LLLinkedQueueNode<DATA_TYPE> *currentp;
272
273 currentp = mHead.mNextp;
274 if (!currentp)
275 {
276 return FALSE;
277 }
278 data = currentp->mData;
279 return TRUE;
280}
281
282
283//////////////////////////////////////////////////////////////////////////////////////////
284// private members
285//////////////////////////////////////////////////////////////////////////////////////////
286
287
288// add node to end of list
289// set mCurrentp to mQueuep
290template <class DATA_TYPE>
291void LLLinkedQueue<DATA_TYPE>::addNodeAtEnd(LLLinkedQueueNode<DATA_TYPE> *nodep)
292{
293 // add the node to the end of the list
294 nodep->mNextp = NULL;
295 nodep->mPrevp = mTail.mPrevp;
296 mTail.mPrevp = nodep;
297
298 // if there's something in the list, fix its back pointer
299 if (nodep->mPrevp)
300 {
301 nodep->mPrevp->mNextp = nodep;
302 }
303 else // otherwise fix the head node
304 {
305 mHead.mNextp = nodep;
306 }
307 mLength++;
308}
309
310#endif