![]() |
OpenNI 1.5.7
|
00001 /***************************************************************************** 00002 * * 00003 * OpenNI 1.x Alpha * 00004 * Copyright (C) 2012 PrimeSense Ltd. * 00005 * * 00006 * This file is part of OpenNI. * 00007 * * 00008 * Licensed under the Apache License, Version 2.0 (the "License"); * 00009 * you may not use this file except in compliance with the License. * 00010 * You may obtain a copy of the License at * 00011 * * 00012 * http://www.apache.org/licenses/LICENSE-2.0 * 00013 * * 00014 * Unless required by applicable law or agreed to in writing, software * 00015 * distributed under the License is distributed on an "AS IS" BASIS, * 00016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 00017 * See the License for the specific language governing permissions and * 00018 * limitations under the License. * 00019 * * 00020 *****************************************************************************/ 00021 #ifndef __XNARRAY_H__ 00022 #define __XNARRAY_H__ 00023 00024 //--------------------------------------------------------------------------- 00025 // Includes 00026 //--------------------------------------------------------------------------- 00027 #include <XnOS.h> 00028 00029 //--------------------------------------------------------------------------- 00030 // Types 00031 //--------------------------------------------------------------------------- 00032 template <typename T> 00033 class XnArray 00034 { 00035 public: 00036 enum {BASE_SIZE = 8}; 00037 00039 typedef T* Iterator; 00040 00042 typedef const T* ConstIterator; 00043 00045 XnArray(XnUInt32 nBaseSize = BASE_SIZE) 00046 { 00047 Init(nBaseSize); 00048 } 00049 00051 XnArray(const XnArray& other) : m_pData(NULL), m_nSize(0), m_nAllocatedSize(0) 00052 { 00053 *this = other; 00054 } 00055 00057 virtual ~XnArray() 00058 { 00059 XN_DELETE_ARR(m_pData); 00060 } 00061 00063 XnArray& operator=(const XnArray& other) 00064 { 00065 CopyFrom(other); 00066 return *this; 00067 } 00068 00070 XnStatus CopyFrom(const XnArray& other) 00071 { 00072 if (this != &other) 00073 { 00074 XnStatus nRetVal = SetData(other.m_pData, other.m_nSize); 00075 XN_IS_STATUS_OK(nRetVal); 00076 } 00077 return XN_STATUS_OK; 00078 } 00079 00081 XnStatus SetData(const T* pData, XnUInt32 nSize) 00082 { 00083 Clear(); 00084 XnStatus nRetVal = SetSize(nSize); 00085 XN_IS_STATUS_OK(nRetVal); 00086 for (XnUInt32 i = 0; i < nSize; i++) 00087 { 00088 m_pData[i] = pData[i]; 00089 } 00090 return XN_STATUS_OK; 00091 } 00092 00094 const T* GetData() const 00095 { 00096 return m_pData; 00097 } 00098 00100 T* GetData() 00101 { 00102 return m_pData; 00103 } 00104 00107 XnStatus Reserve(XnUInt32 nReservedSize) 00108 { 00109 if (nReservedSize > m_nAllocatedSize) 00110 { 00111 //Calculate next power of 2 after nReservedSize 00112 nReservedSize--; 00113 nReservedSize = (nReservedSize >> 1) | nReservedSize; 00114 nReservedSize = (nReservedSize >> 2) | nReservedSize; 00115 nReservedSize = (nReservedSize >> 4) | nReservedSize; 00116 nReservedSize = (nReservedSize >> 8) | nReservedSize; 00117 nReservedSize = (nReservedSize >> 16) | nReservedSize; 00118 nReservedSize++; // nReservedSize is now the next power of 2. 00119 00120 //Allocate new data 00121 T* pNewData = XN_NEW_ARR(T, nReservedSize); 00122 XN_VALIDATE_ALLOC_PTR(pNewData); 00123 00124 //Copy old data into new data 00125 for (XnUInt32 i = 0; i < m_nSize; i++) 00126 { 00127 pNewData[i] = m_pData[i]; 00128 } 00129 00130 //Delete old data 00131 XN_DELETE_ARR(m_pData); 00132 00133 //Point to new data 00134 m_pData = pNewData; 00135 m_nAllocatedSize = nReservedSize; 00136 } 00137 return XN_STATUS_OK; 00138 } 00139 00141 XnBool IsEmpty() const 00142 { 00143 return (m_nSize == 0); 00144 } 00145 00147 XnUInt32 GetSize() const 00148 { 00149 return m_nSize; 00150 } 00151 00154 XnStatus SetSize(XnUInt32 nSize) 00155 { 00156 //TODO: Shrink allocated array if new size is smaller 00157 XnStatus nRetVal = SetMinSize(nSize); 00158 XN_IS_STATUS_OK(nRetVal); 00159 m_nSize = nSize; 00160 return XN_STATUS_OK; 00161 } 00162 00165 XnStatus SetSize(XnUInt32 nSize, const T& fillVal) 00166 { 00167 //TODO: Shrink allocated array if new size is smaller 00168 XnStatus nRetVal = SetMinSize(nSize, fillVal); 00169 XN_IS_STATUS_OK(nRetVal); 00170 m_nSize = nSize; 00171 return XN_STATUS_OK; 00172 } 00173 00177 XnStatus SetMinSize(XnUInt32 nSize) 00178 { 00179 if (nSize > m_nSize) 00180 { 00181 XnStatus nRetVal = Reserve(nSize); 00182 XN_IS_STATUS_OK(nRetVal); 00183 m_nSize = nSize; 00184 } 00185 return XN_STATUS_OK; 00186 } 00187 00191 XnStatus SetMinSize(XnUInt32 nSize, const T& fillVal) 00192 { 00193 if (nSize > m_nSize) 00194 { 00195 XnStatus nRetVal = Reserve(nSize); 00196 XN_IS_STATUS_OK(nRetVal); 00197 for (XnUInt32 i = m_nSize; i < nSize; i++) 00198 { 00199 m_pData[i] = fillVal; 00200 } 00201 m_nSize = nSize; 00202 } 00203 00204 return XN_STATUS_OK; 00205 } 00206 00209 XnUInt32 GetAllocatedSize() const 00210 { 00211 return m_nAllocatedSize; 00212 } 00213 00217 XnStatus Set(XnUInt32 nIndex, const T& val) 00218 { 00219 XnStatus nRetVal = SetMinSize(nIndex+1); 00220 XN_IS_STATUS_OK(nRetVal); 00221 m_pData[nIndex] = val; 00222 return XN_STATUS_OK; 00223 } 00224 00226 XnStatus Set(XnUInt32 nIndex, const T& val, const T& fillVal) 00227 { 00228 XnStatus nRetVal = SetMinSize(nIndex+1, fillVal); 00229 XN_IS_STATUS_OK(nRetVal); 00230 m_pData[nIndex] = val; 00231 return XN_STATUS_OK; 00232 } 00233 00235 XnStatus AddLast(const T& val) 00236 { 00237 return Set(m_nSize, val); 00238 } 00239 00241 XnStatus AddLast(const T* aValues, XnUInt32 nCount) 00242 { 00243 XN_VALIDATE_INPUT_PTR(aValues); 00244 XnUInt32 nOffset = GetSize(); 00245 XnStatus nRetVal = SetMinSize(GetSize() + nCount); 00246 XN_IS_STATUS_OK(nRetVal); 00247 for (XnUInt32 i = 0; i < nCount; ++i) 00248 { 00249 m_pData[nOffset + i] = aValues[i]; 00250 } 00251 return XN_STATUS_OK; 00252 } 00253 00255 void Clear() 00256 { 00257 XN_DELETE_ARR(m_pData); 00258 Init(); 00259 } 00260 00262 T& operator[](XnUInt32 nIndex) 00263 { 00264 XN_ASSERT(nIndex < m_nSize); 00265 return m_pData[nIndex]; 00266 } 00267 00269 const T& operator[](XnUInt32 nIndex) const 00270 { 00271 XN_ASSERT(nIndex < m_nSize); 00272 return m_pData[nIndex]; 00273 } 00274 00276 Iterator begin() 00277 { 00278 return &m_pData[0]; 00279 } 00280 00282 ConstIterator begin() const 00283 { 00284 return &m_pData[0]; 00285 } 00286 00288 Iterator end() 00289 { 00290 return m_pData + m_nSize; 00291 } 00292 00294 ConstIterator end() const 00295 { 00296 return m_pData + m_nSize; 00297 } 00298 00299 private: 00300 void Init(XnUInt32 nBaseSize = BASE_SIZE) 00301 { 00302 m_pData = XN_NEW_ARR(T, nBaseSize); 00303 m_nAllocatedSize = nBaseSize; 00304 m_nSize = 0; 00305 } 00306 00307 T* m_pData; 00308 XnUInt32 m_nSize; 00309 XnUInt32 m_nAllocatedSize; 00310 }; 00311 00312 #endif // __XNARRAY_H__