summaryrefslogtreecommitdiffstats
path: root/tests/manual/kinectsurface/QtKinectWrapper/OpenNI/Include/XnListT.h
blob: 8cbe992d9abd8fa90ad8ec6eed687cf060535249 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#ifndef _XNLISTT_H_
#define _XNLISTT_H_ 

//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnPlatform.h>
#include <XnDataTypes.h>
#include <XnOS.h>

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------

/** 
 * A node in a linked list.
 *
 * @tparam	T	the type of value in the list.
 */
template<class T>
struct XnLinkedNodeT
{
	XnLinkedNodeT() : pPrev(NULL), pNext(NULL) {}
	XnLinkedNodeT(T const& value) : pPrev(NULL), pNext(NULL), value(value) {}

	struct XnLinkedNodeT<T>* pPrev;
	struct XnLinkedNodeT<T>* pNext;
	T value;
};

/**
 * A default allocator for nodes in the linked list. The default allocator calls 'new' for allocating
 * new nodes and 'delete' for deallocating them.
 *
 * For information on how to use allocator, see @ref XnListT.
 *
 * @tparam	T	the type of value in the list.
 */
template<class T>
class XnLinkedNodeDefaultAllocatorT
{
public:
	typedef XnLinkedNodeT<T> LinkedNode;

	static LinkedNode* Allocate(T const& value)
	{
		return XN_NEW(LinkedNode, value);
	}

	static void Deallocate(LinkedNode* pNode)
	{
		XN_DELETE(pNode);
	}
};

/**
 * A linked list.
 *
 * @tparam	T		The type of value in the list
 * @tparam	TAlloc	[Optional] A class for allocating and deallocating nodes in the list.
 *					The allocator must have two static methods: Allocate() and Deallocate().
 */
template<class T, class TAlloc = XnLinkedNodeDefaultAllocatorT<T> >
class XnListT
{
public:
	typedef XnLinkedNodeT<T> LinkedNode;
	typedef T TValue;
	typedef TAlloc TAllocator;

	/**
	* An iterator for iterating the list without modifying values.
	*/
	class ConstIterator
	{
	public:
		inline ConstIterator() : m_pCurrent(NULL) {}

		inline ConstIterator(LinkedNode* pNode) : m_pCurrent(pNode) {}

		inline ConstIterator(const ConstIterator& other) : m_pCurrent(other.m_pCurrent) {}

		/**
		* Support ++iterator, go to the next object in the list
		*/
		inline ConstIterator& operator++()
		{
			m_pCurrent = m_pCurrent->pNext;
			return *this;
		}

		/**
		* Support iterator++, go to the next object in the list, returning the old value
		*/
		inline ConstIterator operator++(int)
		{
			ConstIterator retVal(*this);
			++*this;
			return retVal;
		}

		/**
		* Support --iterator, go to the next object in the list
		*/
		inline ConstIterator& operator--()
		{
			m_pCurrent = m_pCurrent->pPrev;
			return *this;
		}

		/**
		* Support iterator--, go to the next object in the list, returning the old value
		*/
		inline ConstIterator operator--(int)
		{
			ConstIterator retVal(*this);
			--*this;
			return retVal;
		}

		/**
		* Operator to check if two iterators point to the same object
		* 
		* @param	other	[in]	instance to compare with
		*/
		inline XnBool operator==(const ConstIterator& other) const
		{
			return m_pCurrent == other.m_pCurrent;
		}

		/**
		* Operator to check if two iterators point to different objects
		* 
		* @param	other	[in]	instance to compare with
		*/
		inline XnBool operator!=(const ConstIterator& other) const
		{
			return m_pCurrent != other.m_pCurrent;
		}

		/**
		* Get the value of the current object (const version)
		*/
		inline T const& operator*() const
		{
			return m_pCurrent->value;
		}

		/**
		* Get a pointer to the value of the current object (const version)
		*/
		inline T const* operator->() const
		{
			return &m_pCurrent->value;
		}

	protected:
		friend class XnListT;

		/** The current XnNode */
		LinkedNode* m_pCurrent;
	};

	/**
	* An iterator for iterating the list
	*/
	class Iterator : public ConstIterator
	{
	public:
		inline Iterator() : ConstIterator() {}

		inline Iterator(LinkedNode* pNode) : ConstIterator(pNode) {}

		inline Iterator(const Iterator& other) : ConstIterator(other) {}

		/**
		* Support ++iterator, go to the next object in the list
		*/
		inline Iterator& operator++() 
		{
			++(*(ConstIterator*)this);
			return (*this);
		}

		/**
		* Support iterator++, go to the next object in the list, returning the old value
		*/
		inline Iterator operator++(int) 
		{ 
			Iterator retVal(*this);
			++*this;
			return (retVal);
		}
		
		/**
		* Support --iterator, go to the next object in the list
		*/
		inline Iterator& operator--() 
		{ 
			--(*(ConstIterator*)this); 
			return (*this);
		}
		/**
		* Support iterator--, go to the next object in the list, returning the old value
		*/
		inline Iterator operator--(int)
		{ 
			Iterator retVal(*this);
			--*this;
			return (retVal);
		}

		/**
		* Get the value of the current object
		*/
		inline T& operator*() const 
		{
			return this->m_pCurrent->value;
		}

		/**
		* Get a pointer to the value of the current object
		*/
		inline T* operator->() const
		{
			return &this->m_pCurrent->value;
		}
	};

public:
	XnListT()
	{
		Init();
	}

	XnListT(const XnListT& other)
	{
		Init();
		*this = other;
	}

	XnListT& operator=(const XnListT& other)
	{
		Clear();

		XnStatus nRetVal = XN_STATUS_OK;

		for (ConstIterator it = other.Begin(); it != other.End(); ++it)
		{
			nRetVal = AddLast(*it);
			XN_ASSERT(nRetVal == XN_STATUS_OK);
		}

		return *this;
	}

	~XnListT()
	{
		Clear();
	}

	/**
	* An iterator to the first entry of the list (non-const version)
	*/
	Iterator Begin()
	{
		return Iterator(m_anchor.pNext);
	}

	/**
	* An iterator to the first entry of the list (const version)
	*/
	ConstIterator Begin() const
	{
		return ConstIterator(const_cast<LinkedNode*>(m_anchor.pNext));
	}

	/**
	* An iterator 1to the end of the list (non-const version). This position is invalid.
	*/
	Iterator End()
	{
		return Iterator(&m_anchor);
	}

	/**
	* An iterator to the end of the list (const version). This position is invalid.
	*/
	ConstIterator End() const
	{
		return ConstIterator(const_cast<LinkedNode*>(&m_anchor));
	}

	/**
	* An iterator to the last entry of the list (non-const version)
	*/
	Iterator ReverseBegin()
	{
		return Iterator(m_anchor.pPrev);
	}

	/**
	* An iterator to the last entry of the list (const version)
	*/
	ConstIterator ReverseBegin() const
	{
		return ConstIterator(const_cast<LinkedNode*>(m_anchor.pPrev));
	}

	/**
	* An iterator to the beginning of the list (non-const version). This position is invalid.
	*/
	Iterator ReverseEnd()
	{
		return Iterator(&m_anchor);
	}

	/**
	* An iterator to the beginning of the list (const version). This position is invalid.
	*/
	ConstIterator ReverseEnd() const
	{
		return ConstIterator(const_cast<LinkedNode*>(&m_anchor));
	}

	/**
	* Add a new value after the object pointed to by the iterator
	* 
	* @param	where	[in]	iterator to the position after which to add the new value
	* @param	value	[in]	The value to add to the list
	*
	* @return	XN_STATUS_ALLOC_FAILED		failed to allocate new node
	*			XN_STATUS_ILLEGAL_POSITION	iterator is invalid
	*/
	XnStatus AddAfter(ConstIterator where, T const& value)
	{
		if (where == End())
		{
			return XN_STATUS_ILLEGAL_POSITION;
		}

		return InsertAfter(where.m_pCurrent, value);
	}

	/**
	* Add a new value before the object pointed to by the iterator
	* 
	* @param	where	[in]	iterator to the position before which to add the new value
	* @param	value		[in]	The value to add to the list
	*
	* @return	XN_STATUS_ALLOC_FAILED		failed to allocate new node
	*			XN_STATUS_ILLEGAL_POSITION	iterator is invalid
	*/
	XnStatus AddBefore(ConstIterator where, T const& value)
	{
		if (where == End())
		{
			return XN_STATUS_ILLEGAL_POSITION;
		}

		return InsertAfter(where.m_pCurrent->pPrev, value);
	}

	/**
	* Add a new value at the beginning of list
	* 
	* @param	value	[in]	The value to add to the head of the list
	*
	* @return	XN_STATUS_ALLOC_FAILED		failed to allocate new node
	*/
	XnStatus AddFirst(T const& value)
	{
		return InsertAfter(&m_anchor, value);
	}

	/**
	* Add a new value at the end of the list
	* 
	* @param	value	[in]	The value to add to the tail of the list
	*
	* @return	XN_STATUS_ALLOC_FAILED		failed to allocate new node
	*/
	XnStatus AddLast(T const& value)
	{
		return InsertAfter(ReverseBegin().m_pCurrent, value);
	}

	/**
	* Get an iterator pointing to a value in the list.
	* 
	* @param	value	[in]	The searched value 
	*
	* @return	End()	if value doesn't exist
	*/
	ConstIterator Find(T const& value) const
	{
		ConstIterator iter = Begin();
		for (; iter != End(); ++iter)
		{
			if (*iter == value)
				break;
		}
		return iter;
	}

	/**
	* Get an iterator pointing to a value in the list.
	* 
	* @param	value	[in]	The searched value 
	*
	* @return	End()	if value doesn't exist
	*/
	Iterator Find(T const& value)
	{
		ConstIterator iter = const_cast<const XnListT<T>*>(this)->Find(value);
		return Iterator(iter.m_pCurrent);
	}

	/**
	* Remove a value from the list
	* 
	* @param	where	[in]	Iterator pointing to an entry in the list
	*
	* @return XN_STATUS_ILLEGAL_POSITION	iterator was invalid
	*/
	XnStatus Remove(ConstIterator where)
	{
		// Verify iterator is valid
		if (where == End())
		{
			return XN_STATUS_ILLEGAL_POSITION;
		}

		XnLinkedNodeT<T>* pToRemove = where.m_pCurrent;

		// Connect other nodes to bypass the one removed
		pToRemove->pPrev->pNext = pToRemove->pNext;
		pToRemove->pNext->pPrev = pToRemove->pPrev;

		--m_nSize;

		// Free memory
		TAlloc::Deallocate(pToRemove);

		return XN_STATUS_OK;
	}

	/**
	* Removes the first occurrence of a value from the list
	* 
	* @param	value	[in]	The value to be removed
	*
	* @return XN_STATUS_NO_MATCH	value wasn't found.
	*/
	XnStatus Remove(T const& value)
	{
		ConstIterator it = Find(value);
		if (it != End())
		{
			return Remove(it);
		}
		else
		{
			return XN_STATUS_NO_MATCH;
		}
	}

	/**
	* Remove all entries from the list
	*/
	XnStatus Clear()
	{
		while (!IsEmpty())
			Remove(Begin());

		return XN_STATUS_OK;
	}

	/**
	* Check if list is empty
	*/
	XnBool IsEmpty() const
	{
		return (m_nSize == 0);
	}

	/**
	* Gets the current size of the list
	*/
	XnUInt32 Size() const
	{
		return m_nSize;
	}

	/**
	* Copies all values in the list to an array.
	*
	* @param	pArray	A pre-allocated array that values should be copied to. The allocation size can be 
						determined using @ref Size().
	*/
	void CopyTo(T* pArray) const
	{
		XN_ASSERT(pArray != NULL);

		XnUInt32 i = 0;
		for (ConstIterator iter = Begin(); iter != End(); ++iter, ++i)
		{
			pArray[i] = *iter;
		}
	}
	
protected:
	/**
	* Add a new value to the list
	* 
	* @param	pAfter	[in]	The node after which to add the new value
	* @param	val		[in]	The value to add to the list
	*
	* @return	XN_STATUS_ALLOC_FAILED		Failed to add to the list because no nodes are available,
	*/
	XnStatus InsertAfter(LinkedNode* pAfter, T const& val)
	{
		// Get a node from the pool for the entry
		LinkedNode* pNewNode = TAlloc::Allocate(val);
		if (pNewNode == NULL)
		{
			XN_ASSERT(FALSE);
			return XN_STATUS_ALLOC_FAILED;
		}
		pNewNode->pPrev = pAfter;
		pNewNode->pNext = pAfter->pNext;

		// push new node to position
		pAfter->pNext->pPrev = pNewNode;
		pAfter->pNext = pNewNode;

		++m_nSize;

		return XN_STATUS_OK;
	}

	// A dummy node, pointing to first node, and last node points back to it.
	LinkedNode m_anchor;

	XnUInt32 m_nSize;

private:
	void Init()
	{
		m_anchor.pNext = &m_anchor;
		m_anchor.pPrev = &m_anchor;
		m_nSize = 0;
	}
};

#endif // _XNLISTT_H_