OpenNI 1.5.7
XnArray.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2012 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * Licensed under the Apache License, Version 2.0 (the "License"); *
9 * you may not use this file except in compliance with the License. *
10 * You may obtain a copy of the License at *
11 * *
12 * http://www.apache.org/licenses/LICENSE-2.0 *
13 * *
14 * Unless required by applicable law or agreed to in writing, software *
15 * distributed under the License is distributed on an "AS IS" BASIS, *
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17 * See the License for the specific language governing permissions and *
18 * limitations under the License. *
19 * *
20 *****************************************************************************/
21 #ifndef __XNARRAY_H__
22 #define __XNARRAY_H__
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include <XnOS.h>
28 
29 //---------------------------------------------------------------------------
30 // Types
31 //---------------------------------------------------------------------------
32 template <typename T>
33 class XnArray
34 {
35 public:
36  enum {BASE_SIZE = 8};
37 
39  typedef T* Iterator;
40 
42  typedef const T* ConstIterator;
43 
45  XnArray(XnUInt32 nBaseSize = BASE_SIZE)
46  {
47  Init(nBaseSize);
48  }
49 
51  XnArray(const XnArray& other) : m_pData(NULL), m_nSize(0), m_nAllocatedSize(0)
52  {
53  *this = other;
54  }
55 
57  virtual ~XnArray()
58  {
59  XN_DELETE_ARR(m_pData);
60  }
61 
63  XnArray& operator=(const XnArray& other)
64  {
65  CopyFrom(other);
66  return *this;
67  }
68 
70  XnStatus CopyFrom(const XnArray& other)
71  {
72  if (this != &other)
73  {
74  XnStatus nRetVal = SetData(other.m_pData, other.m_nSize);
75  XN_IS_STATUS_OK(nRetVal);
76  }
77  return XN_STATUS_OK;
78  }
79 
81  XnStatus SetData(const T* pData, XnUInt32 nSize)
82  {
83  Clear();
84  XnStatus nRetVal = SetSize(nSize);
85  XN_IS_STATUS_OK(nRetVal);
86  for (XnUInt32 i = 0; i < nSize; i++)
87  {
88  m_pData[i] = pData[i];
89  }
90  return XN_STATUS_OK;
91  }
92 
94  const T* GetData() const
95  {
96  return m_pData;
97  }
98 
100  T* GetData()
101  {
102  return m_pData;
103  }
104 
107  XnStatus Reserve(XnUInt32 nReservedSize)
108  {
109  if (nReservedSize > m_nAllocatedSize)
110  {
111  //Calculate next power of 2 after nReservedSize
112  nReservedSize--;
113  nReservedSize = (nReservedSize >> 1) | nReservedSize;
114  nReservedSize = (nReservedSize >> 2) | nReservedSize;
115  nReservedSize = (nReservedSize >> 4) | nReservedSize;
116  nReservedSize = (nReservedSize >> 8) | nReservedSize;
117  nReservedSize = (nReservedSize >> 16) | nReservedSize;
118  nReservedSize++; // nReservedSize is now the next power of 2.
119 
120  //Allocate new data
121  T* pNewData = XN_NEW_ARR(T, nReservedSize);
122  XN_VALIDATE_ALLOC_PTR(pNewData);
123 
124  //Copy old data into new data
125  for (XnUInt32 i = 0; i < m_nSize; i++)
126  {
127  pNewData[i] = m_pData[i];
128  }
129 
130  //Delete old data
131  XN_DELETE_ARR(m_pData);
132 
133  //Point to new data
134  m_pData = pNewData;
135  m_nAllocatedSize = nReservedSize;
136  }
137  return XN_STATUS_OK;
138  }
139 
141  XnBool IsEmpty() const
142  {
143  return (m_nSize == 0);
144  }
145 
147  XnUInt32 GetSize() const
148  {
149  return m_nSize;
150  }
151 
154  XnStatus SetSize(XnUInt32 nSize)
155  {
156  //TODO: Shrink allocated array if new size is smaller
157  XnStatus nRetVal = SetMinSize(nSize);
158  XN_IS_STATUS_OK(nRetVal);
159  m_nSize = nSize;
160  return XN_STATUS_OK;
161  }
162 
165  XnStatus SetSize(XnUInt32 nSize, const T& fillVal)
166  {
167  //TODO: Shrink allocated array if new size is smaller
168  XnStatus nRetVal = SetMinSize(nSize, fillVal);
169  XN_IS_STATUS_OK(nRetVal);
170  m_nSize = nSize;
171  return XN_STATUS_OK;
172  }
173 
177  XnStatus SetMinSize(XnUInt32 nSize)
178  {
179  if (nSize > m_nSize)
180  {
181  XnStatus nRetVal = Reserve(nSize);
182  XN_IS_STATUS_OK(nRetVal);
183  m_nSize = nSize;
184  }
185  return XN_STATUS_OK;
186  }
187 
191  XnStatus SetMinSize(XnUInt32 nSize, const T& fillVal)
192  {
193  if (nSize > m_nSize)
194  {
195  XnStatus nRetVal = Reserve(nSize);
196  XN_IS_STATUS_OK(nRetVal);
197  for (XnUInt32 i = m_nSize; i < nSize; i++)
198  {
199  m_pData[i] = fillVal;
200  }
201  m_nSize = nSize;
202  }
203 
204  return XN_STATUS_OK;
205  }
206 
209  XnUInt32 GetAllocatedSize() const
210  {
211  return m_nAllocatedSize;
212  }
213 
217  XnStatus Set(XnUInt32 nIndex, const T& val)
218  {
219  XnStatus nRetVal = SetMinSize(nIndex+1);
220  XN_IS_STATUS_OK(nRetVal);
221  m_pData[nIndex] = val;
222  return XN_STATUS_OK;
223  }
224 
226  XnStatus Set(XnUInt32 nIndex, const T& val, const T& fillVal)
227  {
228  XnStatus nRetVal = SetMinSize(nIndex+1, fillVal);
229  XN_IS_STATUS_OK(nRetVal);
230  m_pData[nIndex] = val;
231  return XN_STATUS_OK;
232  }
233 
235  XnStatus AddLast(const T& val)
236  {
237  return Set(m_nSize, val);
238  }
239 
241  XnStatus AddLast(const T* aValues, XnUInt32 nCount)
242  {
243  XN_VALIDATE_INPUT_PTR(aValues);
244  XnUInt32 nOffset = GetSize();
245  XnStatus nRetVal = SetMinSize(GetSize() + nCount);
246  XN_IS_STATUS_OK(nRetVal);
247  for (XnUInt32 i = 0; i < nCount; ++i)
248  {
249  m_pData[nOffset + i] = aValues[i];
250  }
251  return XN_STATUS_OK;
252  }
253 
255  void Clear()
256  {
257  XN_DELETE_ARR(m_pData);
258  Init();
259  }
260 
262  T& operator[](XnUInt32 nIndex)
263  {
264  XN_ASSERT(nIndex < m_nSize);
265  return m_pData[nIndex];
266  }
267 
269  const T& operator[](XnUInt32 nIndex) const
270  {
271  XN_ASSERT(nIndex < m_nSize);
272  return m_pData[nIndex];
273  }
274 
276  Iterator begin()
277  {
278  return &m_pData[0];
279  }
280 
282  ConstIterator begin() const
283  {
284  return &m_pData[0];
285  }
286 
288  Iterator end()
289  {
290  return m_pData + m_nSize;
291  }
292 
294  ConstIterator end() const
295  {
296  return m_pData + m_nSize;
297  }
298 
299 private:
300  void Init(XnUInt32 nBaseSize = BASE_SIZE)
301  {
302  m_pData = XN_NEW_ARR(T, nBaseSize);
303  m_nAllocatedSize = nBaseSize;
304  m_nSize = 0;
305  }
306 
307  T* m_pData;
308  XnUInt32 m_nSize;
309  XnUInt32 m_nAllocatedSize;
310 };
311 
312 #endif // __XNARRAY_H__
XnArray(XnUInt32 nBaseSize=BASE_SIZE)
Definition: XnArray.h:45
XnStatus CopyFrom(const XnArray &other)
Definition: XnArray.h:70
#define XN_IS_STATUS_OK(x)
Definition: XnMacros.h:59
#define XN_VALIDATE_ALLOC_PTR(x)
Definition: XnOS.h:131
const T * ConstIterator
Definition: XnArray.h:42
XnArray & operator=(const XnArray &other)
Definition: XnArray.h:63
#define XN_STATUS_OK
Definition: XnStatus.h:36
Definition: XnArray.h:36
const T * GetData() const
Definition: XnArray.h:94
XnStatus SetMinSize(XnUInt32 nSize, const T &fillVal)
Definition: XnArray.h:191
XnArray(const XnArray &other)
Definition: XnArray.h:51
XnUInt32 XnStatus
Definition: XnStatus.h:33
XnStatus Set(XnUInt32 nIndex, const T &val)
Definition: XnArray.h:217
Definition: XnArray.h:33
ConstIterator begin() const
Definition: XnArray.h:282
XnStatus SetSize(XnUInt32 nSize)
Definition: XnArray.h:154
XnStatus SetSize(XnUInt32 nSize, const T &fillVal)
Definition: XnArray.h:165
Iterator begin()
Definition: XnArray.h:276
XnUInt32 GetAllocatedSize() const
Definition: XnArray.h:209
Iterator end()
Definition: XnArray.h:288
XnStatus SetData(const T *pData, XnUInt32 nSize)
Definition: XnArray.h:81
#define XN_NEW_ARR(type, count)
Definition: XnOS.h:338
#define XN_DELETE_ARR(p)
Definition: XnOS.h:340
XnStatus Set(XnUInt32 nIndex, const T &val, const T &fillVal)
Definition: XnArray.h:226
#define XN_VALIDATE_INPUT_PTR(x)
Definition: XnOS.h:126
T * Iterator
Definition: XnArray.h:39
ConstIterator end() const
Definition: XnArray.h:294
virtual ~XnArray()
Definition: XnArray.h:57
XnStatus AddLast(const T &val)
Definition: XnArray.h:235
XnUInt32 GetSize() const
Definition: XnArray.h:147
XnStatus SetMinSize(XnUInt32 nSize)
Definition: XnArray.h:177
void Clear()
Definition: XnArray.h:255
const T & operator[](XnUInt32 nIndex) const
Definition: XnArray.h:269
XnBool IsEmpty() const
Definition: XnArray.h:141
T * GetData()
Definition: XnArray.h:100
T & operator[](XnUInt32 nIndex)
Definition: XnArray.h:262
XnStatus Reserve(XnUInt32 nReservedSize)
Definition: XnArray.h:107
XnStatus AddLast(const T *aValues, XnUInt32 nCount)
Definition: XnArray.h:241