forked from OpenNI/OpenNI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XnArray.h
312 lines (273 loc) · 9.36 KB
/
XnArray.h
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
/*****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2012 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*****************************************************************************/
#ifndef __XNARRAY_H__
#define __XNARRAY_H__
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
template <typename T>
class XnArray
{
public:
enum {BASE_SIZE = 8};
/** An iterator object that allows you to iterate over members in the array. **/
typedef T* Iterator;
/** A constant iterator object that allows you to iterate over members in the array as read-only values. **/
typedef const T* ConstIterator;
/** Default constructor. Initializes the array to BASE_SIZE. **/
XnArray(XnUInt32 nBaseSize = BASE_SIZE)
{
Init(nBaseSize);
}
/** Copy constructor. Initializes this array from another array of the same type. **/
XnArray(const XnArray& other) : m_pData(NULL), m_nSize(0), m_nAllocatedSize(0)
{
*this = other;
}
/** Destructor. Deallocates data in this array. **/
virtual ~XnArray()
{
XN_DELETE_ARR(m_pData);
}
/** Assignment operator. Copies data from another array of the same type. **/
XnArray& operator=(const XnArray& other)
{
CopyFrom(other);
return *this;
}
/** Copies data from another array of the same type. **/
XnStatus CopyFrom(const XnArray& other)
{
if (this != &other)
{
XnStatus nRetVal = SetData(other.m_pData, other.m_nSize);
XN_IS_STATUS_OK(nRetVal);
}
return XN_STATUS_OK;
}
/** Copies data from a raw buffer of the same type pointed to by pData, and sets size to nSize. **/
XnStatus SetData(const T* pData, XnUInt32 nSize)
{
Clear();
XnStatus nRetVal = SetSize(nSize);
XN_IS_STATUS_OK(nRetVal);
for (XnUInt32 i = 0; i < nSize; i++)
{
m_pData[i] = pData[i];
}
return XN_STATUS_OK;
}
/** @returns the raw data of this array as a read-only buffer. **/
const T* GetData() const
{
return m_pData;
}
/** @returns the raw data of this array as a modifiable buffer. **/
T* GetData()
{
return m_pData;
}
/** Reserves space in this array for the specified number of elements.
This saves you re-allocations and data copies if you know the size in advance. **/
XnStatus Reserve(XnUInt32 nReservedSize)
{
if (nReservedSize > m_nAllocatedSize)
{
//Calculate next power of 2 after nReservedSize
nReservedSize--;
nReservedSize = (nReservedSize >> 1) | nReservedSize;
nReservedSize = (nReservedSize >> 2) | nReservedSize;
nReservedSize = (nReservedSize >> 4) | nReservedSize;
nReservedSize = (nReservedSize >> 8) | nReservedSize;
nReservedSize = (nReservedSize >> 16) | nReservedSize;
nReservedSize++; // nReservedSize is now the next power of 2.
//Allocate new data
T* pNewData = XN_NEW_ARR(T, nReservedSize);
XN_VALIDATE_ALLOC_PTR(pNewData);
//Copy old data into new data
for (XnUInt32 i = 0; i < m_nSize; i++)
{
pNewData[i] = m_pData[i];
}
//Delete old data
XN_DELETE_ARR(m_pData);
//Point to new data
m_pData = pNewData;
m_nAllocatedSize = nReservedSize;
}
return XN_STATUS_OK;
}
/** @returns TRUE if this array is empty, FALSE otherwise. **/
XnBool IsEmpty() const
{
return (m_nSize == 0);
}
/** @returns The number of elements in this array. **/
XnUInt32 GetSize() const
{
return m_nSize;
}
/** Sets the size of this array to the specified number of elements.
Note that for primitive types, the data is uninitialized. **/
XnStatus SetSize(XnUInt32 nSize)
{
//TODO: Shrink allocated array if new size is smaller
XnStatus nRetVal = SetMinSize(nSize);
XN_IS_STATUS_OK(nRetVal);
m_nSize = nSize;
return XN_STATUS_OK;
}
/** Sets the size of this array to the specified number of elements, initializing all elements with the
value specified by fillVal. **/
XnStatus SetSize(XnUInt32 nSize, const T& fillVal)
{
//TODO: Shrink allocated array if new size is smaller
XnStatus nRetVal = SetMinSize(nSize, fillVal);
XN_IS_STATUS_OK(nRetVal);
m_nSize = nSize;
return XN_STATUS_OK;
}
/** Makes sure the array has at least nSize elements.
If nSize is less than the current size, there is no effect.
Note that for primitive types, any added elements are uninitialized. **/
XnStatus SetMinSize(XnUInt32 nSize)
{
if (nSize > m_nSize)
{
XnStatus nRetVal = Reserve(nSize);
XN_IS_STATUS_OK(nRetVal);
m_nSize = nSize;
}
return XN_STATUS_OK;
}
/** Makes sure the array has at least nSize elements.
If nSize is less than the current size, there is no effect.
Any added elements are initialized by the value specified by fillVal. **/
XnStatus SetMinSize(XnUInt32 nSize, const T& fillVal)
{
if (nSize > m_nSize)
{
XnStatus nRetVal = Reserve(nSize);
XN_IS_STATUS_OK(nRetVal);
for (XnUInt32 i = m_nSize; i < nSize; i++)
{
m_pData[i] = fillVal;
}
m_nSize = nSize;
}
return XN_STATUS_OK;
}
/** @returns The allocated size of this array. This is the maximum number of elements that the array can hold
without re-allocating and copying data. **/
XnUInt32 GetAllocatedSize() const
{
return m_nAllocatedSize;
}
/** Sets value at specified index, grows array if needed.
Note that when dealing with primitive types and adding beyond the current size of the array,
any added values that fill the gap are uninitialized**/
XnStatus Set(XnUInt32 nIndex, const T& val)
{
XnStatus nRetVal = SetMinSize(nIndex+1);
XN_IS_STATUS_OK(nRetVal);
m_pData[nIndex] = val;
return XN_STATUS_OK;
}
/** Sets value at specified index and grows array if needed, filling any resulting blank elements with fillVal. **/
XnStatus Set(XnUInt32 nIndex, const T& val, const T& fillVal)
{
XnStatus nRetVal = SetMinSize(nIndex+1, fillVal);
XN_IS_STATUS_OK(nRetVal);
m_pData[nIndex] = val;
return XN_STATUS_OK;
}
/** Adds one element to the end of this array. **/
XnStatus AddLast(const T& val)
{
return Set(m_nSize, val);
}
/** Adds a range of values to the end of this array. **/
XnStatus AddLast(const T* aValues, XnUInt32 nCount)
{
XN_VALIDATE_INPUT_PTR(aValues);
XnUInt32 nOffset = GetSize();
XnStatus nRetVal = SetMinSize(GetSize() + nCount);
XN_IS_STATUS_OK(nRetVal);
for (XnUInt32 i = 0; i < nCount; ++i)
{
m_pData[nOffset + i] = aValues[i];
}
return XN_STATUS_OK;
}
/** Resets the data of this array and sets its size to 0. **/
void Clear()
{
XN_DELETE_ARR(m_pData);
Init();
}
/** Element access operator. Returns a modifiable reference to the element at specified index. **/
T& operator[](XnUInt32 nIndex)
{
XN_ASSERT(nIndex < m_nSize);
return m_pData[nIndex];
}
/** Element access operator. Returns a const reference to the element at specified index. **/
const T& operator[](XnUInt32 nIndex) const
{
XN_ASSERT(nIndex < m_nSize);
return m_pData[nIndex];
}
/** @returns a modifiable iterator pointing to the beginning of this array. **/
Iterator begin()
{
return &m_pData[0];
}
/** @returns a const iterator pointing to the beginning of this array. **/
ConstIterator begin() const
{
return &m_pData[0];
}
/** @returns a modifiable itertor pointing to the end of this array. **/
Iterator end()
{
return m_pData + m_nSize;
}
/** @returns a const itertor pointing to the end of this array. **/
ConstIterator end() const
{
return m_pData + m_nSize;
}
private:
void Init(XnUInt32 nBaseSize = BASE_SIZE)
{
m_pData = XN_NEW_ARR(T, nBaseSize);
m_nAllocatedSize = nBaseSize;
m_nSize = 0;
}
T* m_pData;
XnUInt32 m_nSize;
XnUInt32 m_nAllocatedSize;
};
#endif // __XNARRAY_H__