OpenNI 1.5.4
XnArray.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.x Alpha *
4 * Copyright (C) 2011 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * OpenNI is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published *
10 * by the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * OpenNI is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 ****************************************************************************/
22 #ifndef __XNARRAY_H__
23 #define __XNARRAY_H__
24 
25 //---------------------------------------------------------------------------
26 // Includes
27 //---------------------------------------------------------------------------
28 #include <XnOS.h>
29 
30 //---------------------------------------------------------------------------
31 // Types
32 //---------------------------------------------------------------------------
33 template <typename T>
34 class XnArray
35 {
36 public:
37  enum {BASE_SIZE = 8};
38 
40  typedef T* Iterator;
41 
43  typedef const T* ConstIterator;
44 
46  XnArray(XnUInt32 nBaseSize = BASE_SIZE)
47  {
48  Init(nBaseSize);
49  }
50 
52  XnArray(const XnArray& other) : m_pData(NULL), m_nSize(0), m_nAllocatedSize(0)
53  {
54  *this = other;
55  }
56 
58  virtual ~XnArray()
59  {
60  XN_DELETE_ARR(m_pData);
61  }
62 
64  XnArray& operator=(const XnArray& other)
65  {
66  CopyFrom(other);
67  return *this;
68  }
69 
71  XnStatus CopyFrom(const XnArray& other)
72  {
73  if (this != &other)
74  {
75  XnStatus nRetVal = SetData(other.m_pData, other.m_nSize);
76  XN_IS_STATUS_OK(nRetVal);
77  }
78  return XN_STATUS_OK;
79  }
80 
82  XnStatus SetData(const T* pData, XnUInt32 nSize)
83  {
84  Clear();
85  XnStatus nRetVal = SetSize(nSize);
86  XN_IS_STATUS_OK(nRetVal);
87  for (XnUInt32 i = 0; i < nSize; i++)
88  {
89  m_pData[i] = pData[i];
90  }
91  return XN_STATUS_OK;
92  }
93 
95  const T* GetData() const
96  {
97  return m_pData;
98  }
99 
101  T* GetData()
102  {
103  return m_pData;
104  }
105 
108  XnStatus Reserve(XnUInt32 nReservedSize)
109  {
110  if (nReservedSize > m_nAllocatedSize)
111  {
112  //Calculate next power of 2 after nReservedSize
113  nReservedSize--;
114  nReservedSize = (nReservedSize >> 1) | nReservedSize;
115  nReservedSize = (nReservedSize >> 2) | nReservedSize;
116  nReservedSize = (nReservedSize >> 4) | nReservedSize;
117  nReservedSize = (nReservedSize >> 8) | nReservedSize;
118  nReservedSize = (nReservedSize >> 16) | nReservedSize;
119  nReservedSize++; // nReservedSize is now the next power of 2.
120 
121  //Allocate new data
122  T* pNewData = XN_NEW_ARR(T, nReservedSize);
123  XN_VALIDATE_ALLOC_PTR(pNewData);
124 
125  //Copy old data into new data
126  for (XnUInt32 i = 0; i < m_nSize; i++)
127  {
128  pNewData[i] = m_pData[i];
129  }
130 
131  //Delete old data
132  XN_DELETE_ARR(m_pData);
133 
134  //Point to new data
135  m_pData = pNewData;
136  m_nAllocatedSize = nReservedSize;
137  }
138  return XN_STATUS_OK;
139  }
140 
142  XnBool IsEmpty() const
143  {
144  return (m_nSize == 0);
145  }
146 
148  XnUInt32 GetSize() const
149  {
150  return m_nSize;
151  }
152 
155  XnStatus SetSize(XnUInt32 nSize)
156  {
157  //TODO: Shrink allocated array if new size is smaller
158  XnStatus nRetVal = SetMinSize(nSize);
159  XN_IS_STATUS_OK(nRetVal);
160  m_nSize = nSize;
161  return XN_STATUS_OK;
162  }
163 
166  XnStatus SetSize(XnUInt32 nSize, const T& fillVal)
167  {
168  //TODO: Shrink allocated array if new size is smaller
169  XnStatus nRetVal = SetMinSize(nSize, fillVal);
170  XN_IS_STATUS_OK(nRetVal);
171  m_nSize = nSize;
172  return XN_STATUS_OK;
173  }
174 
178  XnStatus SetMinSize(XnUInt32 nSize)
179  {
180  if (nSize > m_nSize)
181  {
182  XnStatus nRetVal = Reserve(nSize);
183  XN_IS_STATUS_OK(nRetVal);
184  m_nSize = nSize;
185  }
186  return XN_STATUS_OK;
187  }
188 
192  XnStatus SetMinSize(XnUInt32 nSize, const T& fillVal)
193  {
194  if (nSize > m_nSize)
195  {
196  XnStatus nRetVal = Reserve(nSize);
197  XN_IS_STATUS_OK(nRetVal);
198  for (XnUInt32 i = m_nSize; i < nSize; i++)
199  {
200  m_pData[i] = fillVal;
201  }
202  m_nSize = nSize;
203  }
204 
205  return XN_STATUS_OK;
206  }
207 
210  XnUInt32 GetAllocatedSize() const
211  {
212  return m_nAllocatedSize;
213  }
214 
218  XnStatus Set(XnUInt32 nIndex, const T& val)
219  {
220  XnStatus nRetVal = SetMinSize(nIndex+1);
221  XN_IS_STATUS_OK(nRetVal);
222  m_pData[nIndex] = val;
223  return XN_STATUS_OK;
224  }
225 
227  XnStatus Set(XnUInt32 nIndex, const T& val, const T& fillVal)
228  {
229  XnStatus nRetVal = SetMinSize(nIndex+1, fillVal);
230  XN_IS_STATUS_OK(nRetVal);
231  m_pData[nIndex] = val;
232  return XN_STATUS_OK;
233  }
234 
236  XnStatus AddLast(const T& val)
237  {
238  return Set(m_nSize, val);
239  }
240 
242  XnStatus AddLast(const T* aValues, XnUInt32 nCount)
243  {
244  XN_VALIDATE_INPUT_PTR(aValues);
245  XnUInt32 nOffset = GetSize();
246  XnStatus nRetVal = SetMinSize(GetSize() + nCount);
247  XN_IS_STATUS_OK(nRetVal);
248  for (XnUInt32 i = 0; i < nCount; ++i)
249  {
250  m_pData[nOffset + i] = aValues[i];
251  }
252  return XN_STATUS_OK;
253  }
254 
256  void Clear()
257  {
258  XN_DELETE_ARR(m_pData);
259  Init();
260  }
261 
263  T& operator[](XnUInt32 nIndex)
264  {
265  XN_ASSERT(nIndex < m_nSize);
266  return m_pData[nIndex];
267  }
268 
270  const T& operator[](XnUInt32 nIndex) const
271  {
272  XN_ASSERT(nIndex < m_nSize);
273  return m_pData[nIndex];
274  }
275 
277  Iterator begin()
278  {
279  return &m_pData[0];
280  }
281 
283  ConstIterator begin() const
284  {
285  return &m_pData[0];
286  }
287 
289  Iterator end()
290  {
291  return m_pData + m_nSize;
292  }
293 
295  ConstIterator end() const
296  {
297  return m_pData + m_nSize;
298  }
299 
300 private:
301  void Init(XnUInt32 nBaseSize = BASE_SIZE)
302  {
303  m_pData = XN_NEW_ARR(T, nBaseSize);
304  m_nAllocatedSize = nBaseSize;
305  m_nSize = 0;
306  }
307 
308  T* m_pData;
309  XnUInt32 m_nSize;
310  XnUInt32 m_nAllocatedSize;
311 };
312 
313 #endif // __XNARRAY_H__
XnArray(XnUInt32 nBaseSize=BASE_SIZE)
Definition: XnArray.h:46
XnUInt32 GetAllocatedSize() const
Definition: XnArray.h:210
XnStatus CopyFrom(const XnArray &other)
Definition: XnArray.h:71
#define XN_IS_STATUS_OK(x)
Definition: XnMacros.h:60
ConstIterator begin() const
Definition: XnArray.h:283
#define XN_VALIDATE_ALLOC_PTR(x)
Definition: XnOS.h:128
const T * ConstIterator
Definition: XnArray.h:43
XnArray & operator=(const XnArray &other)
Definition: XnArray.h:64
#define XN_STATUS_OK
Definition: XnStatus.h:37
Definition: XnArray.h:37
XnBool IsEmpty() const
Definition: XnArray.h:142
XnStatus SetMinSize(XnUInt32 nSize, const T &fillVal)
Definition: XnArray.h:192
XnArray(const XnArray &other)
Definition: XnArray.h:52
XnUInt32 XnStatus
Definition: XnStatus.h:34
XnStatus Set(XnUInt32 nIndex, const T &val)
Definition: XnArray.h:218
Definition: XnArray.h:34
XnUInt32 GetSize() const
Definition: XnArray.h:148
XnStatus SetSize(XnUInt32 nSize)
Definition: XnArray.h:155
XnStatus SetSize(XnUInt32 nSize, const T &fillVal)
Definition: XnArray.h:166
Iterator begin()
Definition: XnArray.h:277
Iterator end()
Definition: XnArray.h:289
XnStatus SetData(const T *pData, XnUInt32 nSize)
Definition: XnArray.h:82
#define XN_NEW_ARR(type, count)
Definition: XnOS.h:335
#define XN_DELETE_ARR(p)
Definition: XnOS.h:337
const T * GetData() const
Definition: XnArray.h:95
XnStatus Set(XnUInt32 nIndex, const T &val, const T &fillVal)
Definition: XnArray.h:227
#define XN_VALIDATE_INPUT_PTR(x)
Definition: XnOS.h:123
T * Iterator
Definition: XnArray.h:40
virtual ~XnArray()
Definition: XnArray.h:58
XnStatus AddLast(const T &val)
Definition: XnArray.h:236
XnStatus SetMinSize(XnUInt32 nSize)
Definition: XnArray.h:178
void Clear()
Definition: XnArray.h:256
T * GetData()
Definition: XnArray.h:101
ConstIterator end() const
Definition: XnArray.h:295
T & operator[](XnUInt32 nIndex)
Definition: XnArray.h:263
const T & operator[](XnUInt32 nIndex) const
Definition: XnArray.h:270
XnStatus Reserve(XnUInt32 nReservedSize)
Definition: XnArray.h:108
XnStatus AddLast(const T *aValues, XnUInt32 nCount)
Definition: XnArray.h:242