aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llcommon/lldqueueptr.h
blob: eede1594ba1737b18e2d27d3a3b30e29a644062f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/** 
 * @file lldqueueptr.h
 * @brief LLDynamicQueuePtr declaration
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */
#ifndef LL_LLDQUEUEPTR_H
#define LL_LLDQUEUEPTR_H

template <class Type> 
class LLDynamicQueuePtr
{
public:
	enum
	{
		OKAY = 0,
		FAIL = -1
	};
	
	LLDynamicQueuePtr(const S32 size=8);
	~LLDynamicQueuePtr();

	void init();
	void destroy();
	void reset();
	void reallocate(U32 newsize);

	// ACCESSORS
	const Type& get(const S32 index) const;					// no bounds checking
	Type&       get(const S32 index);						// no bounds checking
	const Type& operator []	(const S32 index) const			{ return get(index); }
	Type&       operator []	(const S32 index)				{ return get(index); }
	S32			find(const Type &obj) const;

	S32			count() const								{ return (mLastObj >= mFirstObj ? mLastObj - mFirstObj : mLastObj + mMaxObj - mFirstObj); }
	S32			getMax() const								{ return mMaxObj; }
	S32			getFirst() const       { return mFirstObj; }
	S32			getLast () const       { return mLastObj; }

	// MANIPULATE
	S32         push(const Type &obj);					// add to end of Queue, returns index from start
	S32			pull(      Type &obj);			        // pull from Queue, returns index from start

	S32			remove   (S32 index);				    // remove by index
	S32			removeObj(const Type &obj);				// remove by object

protected:
	S32           mFirstObj, mLastObj, mMaxObj;
	Type*		  mMemory;

public:

	void print()
	{
		/*
		Convert this to llinfos if it's intended to be used - djs 08/30/02

		printf("Printing from %d to %d (of %d): ",mFirstObj, mLastObj, mMaxObj);

		if (mFirstObj <= mLastObj)
		{
			for (S32 i=mFirstObj;i<mLastObj;i++)
			{
				printf("%d ",mMemory[i]);
			}
		}
		else
		{
			for (S32 i=mFirstObj;i<mMaxObj;i++)
			{
				printf("%d ",mMemory[i]);
			}
			for (i=0;i<mLastObj;i++)
			{
				printf("%d ",mMemory[i]);
			}
		}
		printf("\n");
		*/
	}

};


//--------------------------------------------------------
// LLDynamicQueuePtrPtr implementation
//--------------------------------------------------------


template <class Type>
inline LLDynamicQueuePtr<Type>::LLDynamicQueuePtr(const S32 size)
{
	init();
	reallocate(size);
}

template <class Type>
inline LLDynamicQueuePtr<Type>::~LLDynamicQueuePtr()
{
	destroy();
}

template <class Type>
inline void LLDynamicQueuePtr<Type>::init()
{ 
	mFirstObj    = 0;
	mLastObj     = 0;
	mMaxObj      = 0;
	mMemory      = NULL;
}

template <class Type>
inline void LLDynamicQueuePtr<Type>::reallocate(U32 newsize)
{ 
	if (newsize)
	{
		if (mFirstObj > mLastObj && newsize > mMaxObj)
		{
			Type* new_memory = new Type[newsize];

			llassert(new_memory);

			S32 _count = count();
			S32 i, m = 0;
			for (i=mFirstObj; i < mMaxObj; i++)
			{
				new_memory[m++] = mMemory[i];
			}
			for (i=0; i <=mLastObj; i++)
			{
				new_memory[m++] = mMemory[i];
			}

			delete[] mMemory;
			mMemory = new_memory;

			mFirstObj = 0;
			mLastObj  = _count;
		}
		else
		{
			Type* new_memory = new Type[newsize];

			llassert(new_memory);

			S32 i, m = 0;
			for (i=0; i < mLastObj; i++)
			{
				new_memory[m++] = mMemory[i];
			}
			delete[] mMemory;
			mMemory = new_memory;
		}
	}
	else if (mMemory)
	{
		delete[] mMemory;
		mMemory = NULL;
	}

	mMaxObj = newsize;
}

template <class Type>
inline void LLDynamicQueuePtr<Type>::destroy()
{
	reset();
	delete[] mMemory;
	mMemory = NULL;
}


template <class Type>
void LLDynamicQueuePtr<Type>::reset()	   
{ 
	for (S32 i=0; i < mMaxObj; i++)
	{
		get(i) = NULL; // unrefs for pointers
	}

	mFirstObj    = 0;
	mLastObj     = 0;
}


template <class Type>
inline S32 LLDynamicQueuePtr<Type>::find(const Type &obj) const
{
	S32 i;
	if (mFirstObj <= mLastObj)
	{
		for ( i = mFirstObj; i < mLastObj; i++ )
		{
			if (mMemory[i] == obj)
			{
				return i;
			}
		}
	}
	else
	{
		for ( i = mFirstObj; i < mMaxObj; i++ )
		{
			if (mMemory[i] == obj)
			{
				return i;
			}
		}
		for ( i = 0; i < mLastObj; i++ )
		{
			if (mMemory[i] == obj)
			{
				return i;
			}
		}
	}

	return FAIL;
}

template <class Type>
inline S32 LLDynamicQueuePtr<Type>::remove(S32 i)
{
	if (mFirstObj > mLastObj)
	{
		if (i >= mFirstObj && i < mMaxObj)
		{
			while( i > mFirstObj)
			{
				mMemory[i] = mMemory[i-1];
				i--;
			}
			mMemory[mFirstObj] = NULL;
			mFirstObj++;
			if (mFirstObj >= mMaxObj) mFirstObj = 0;

			return count();
		}
		else if (i < mLastObj && i >= 0)
		{
			while(i < mLastObj)
			{
				mMemory[i] = mMemory[i+1];
				i++;
			}
			mMemory[mLastObj] = NULL;
			mLastObj--;
			if (mLastObj < 0) mLastObj = mMaxObj-1;

			return count();
		}
	}
	else if (i <= mLastObj && i >= mFirstObj)
	{
		while(i < mLastObj)
		{
			mMemory[i] = mMemory[i+1];
			i++;
		}
		mMemory[mLastObj] = NULL;
		mLastObj--;
		if (mLastObj < 0) mLastObj = mMaxObj-1;

		return count();
	}

	
	return FAIL;
}

template <class Type>
inline S32 LLDynamicQueuePtr<Type>::removeObj(const Type& obj)
{
	S32 ind = find(obj);
	if (ind >= 0)
	{
		return remove(ind);
	}
	return FAIL;
}

template <class Type>
inline S32	LLDynamicQueuePtr<Type>::push(const Type &obj) 
{
	if (mMaxObj - count() <= 1)
	{
		reallocate(mMaxObj * 2);
	}

	mMemory[mLastObj++] = obj;

	if (mLastObj >= mMaxObj) 
	{
		mLastObj = 0;
	}

	return count();
}

template <class Type>
inline S32	LLDynamicQueuePtr<Type>::pull(Type &obj) 
{
	obj = NULL;

	if (count() < 1) return -1;

	obj = mMemory[mFirstObj];
	mMemory[mFirstObj] = NULL;

	mFirstObj++;

	if (mFirstObj >= mMaxObj) 
	{
		mFirstObj = 0;
	}

	return count();
}

template <class Type>
inline const Type& LLDynamicQueuePtr<Type>::get(const S32 i) const
{
	return mMemory[i];
}

template <class Type>
inline Type& LLDynamicQueuePtr<Type>::get(const S32 i)
{
	return mMemory[i];
}


#endif // LL_LLDQUEUEPTR_H