XnArray.h
Go to the documentation of this file.
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#ifndef __XNARRAY_H__
#define __XNARRAY_H__
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
template <typename T>
class XnArray
{
public:
enum {BASE_SIZE = 8};
typedef T* Iterator;
typedef const T* ConstIterator;
XnArray(XnUInt32 nBaseSize = BASE_SIZE)
{
Init(nBaseSize);
}
XnArray(const XnArray& other) : m_pData(NULL), m_nSize(0), m_nAllocatedSize(0)
{
*this = other;
}
virtual ~XnArray()
{
XN_DELETE_ARR(m_pData);
}
XnArray& operator=(const XnArray& other)
{
CopyFrom(other);
return *this;
}
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;
}
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;
}
const T* GetData() const
{
return m_pData;
}
T* GetData()
{
return m_pData;
}
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;
}
XnBool IsEmpty() const
{
return (m_nSize == 0);
}
XnUInt32 GetSize() const
{
return m_nSize;
}
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;
}
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;
}
XnStatus SetMinSize(XnUInt32 nSize)
{
if (nSize > m_nSize)
{
XnStatus nRetVal = Reserve(nSize);
XN_IS_STATUS_OK(nRetVal);
m_nSize = nSize;
}
return XN_STATUS_OK;
}
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;
}
XnUInt32 GetAllocatedSize() const
{
return m_nAllocatedSize;
}
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;
}
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;
}
XnStatus AddLast(const T& val)
{
return Set(m_nSize, val);
}
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;
}
void Clear()
{
XN_DELETE_ARR(m_pData);
Init();
}
T& operator[](XnUInt32 nIndex)
{
XN_ASSERT(nIndex < m_nSize);
return m_pData[nIndex];
}
const T& operator[](XnUInt32 nIndex) const
{
XN_ASSERT(nIndex < m_nSize);
return m_pData[nIndex];
}
Iterator begin()
{
return &m_pData[0];
}
ConstIterator begin() const
{
return &m_pData[0];
}
Iterator end()
{
return m_pData + m_nSize;
}
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__
XN_IS_STATUS_OK
#define XN_IS_STATUS_OK(x)
Definition XnMacros.h:60
XnOS.h
XN_VALIDATE_ALLOC_PTR
#define XN_VALIDATE_ALLOC_PTR(x)
Definition XnOS.h:128
XN_VALIDATE_INPUT_PTR
#define XN_VALIDATE_INPUT_PTR(x)
Definition XnOS.h:123
XN_DELETE_ARR
#define XN_DELETE_ARR(p)
Definition XnOS.h:337
XN_NEW_ARR
#define XN_NEW_ARR(type, count)
Definition XnOS.h:335
XnStatus
XnUInt32 XnStatus
Definition XnStatus.h:34
XN_STATUS_OK
#define XN_STATUS_OK
Definition XnStatus.h:37
XnArray::Iterator
T * Iterator
Definition XnArray.h:40
XnArray::GetSize
XnUInt32 GetSize() const
Definition XnArray.h:148
XnArray::Reserve
XnStatus Reserve(XnUInt32 nReservedSize)
Definition XnArray.h:108
XnArray::SetMinSize
XnStatus SetMinSize(XnUInt32 nSize, const T &fillVal)
Definition XnArray.h:192
XnArray::GetData
const T * GetData() const
Definition XnArray.h:95
XnArray::end
ConstIterator end() const
Definition XnArray.h:295
XnArray::GetAllocatedSize
XnUInt32 GetAllocatedSize() const
Definition XnArray.h:210
XnArray::end
Iterator end()
Definition XnArray.h:289
XnArray::AddLast
XnStatus AddLast(const T *aValues, XnUInt32 nCount)
Definition XnArray.h:242
XnArray::~XnArray
virtual ~XnArray()
Definition XnArray.h:58
XnArray::Clear
void Clear()
Definition XnArray.h:256
XnArray::XnArray
XnArray(XnUInt32 nBaseSize=BASE_SIZE)
Definition XnArray.h:46
XnArray::operator[]
const T & operator[](XnUInt32 nIndex) const
Definition XnArray.h:270
XnArray::SetSize
XnStatus SetSize(XnUInt32 nSize, const T &fillVal)
Definition XnArray.h:166
XnArray::operator=
XnArray & operator=(const XnArray &other)
Definition XnArray.h:64
XnArray::begin
ConstIterator begin() const
Definition XnArray.h:283
XnArray::SetData
XnStatus SetData(const T *pData, XnUInt32 nSize)
Definition XnArray.h:82
XnArray::AddLast
XnStatus AddLast(const T &val)
Definition XnArray.h:236
XnArray::operator[]
T & operator[](XnUInt32 nIndex)
Definition XnArray.h:263
XnArray::BASE_SIZE
@ BASE_SIZE
Definition XnArray.h:37
XnArray::Set
XnStatus Set(XnUInt32 nIndex, const T &val)
Definition XnArray.h:218
XnArray::XnArray
XnArray(const XnArray &other)
Definition XnArray.h:52
XnArray::ConstIterator
const T * ConstIterator
Definition XnArray.h:43
XnArray::begin
Iterator begin()
Definition XnArray.h:277
XnArray::IsEmpty
XnBool IsEmpty() const
Definition XnArray.h:142
XnArray::GetData
T * GetData()
Definition XnArray.h:101
XnArray::Set
XnStatus Set(XnUInt32 nIndex, const T &val, const T &fillVal)
Definition XnArray.h:227
XnArray::SetMinSize
XnStatus SetMinSize(XnUInt32 nSize)
Definition XnArray.h:178
XnArray::CopyFrom
XnStatus CopyFrom(const XnArray &other)
Definition XnArray.h:71
XnArray::SetSize
XnStatus SetSize(XnUInt32 nSize)
Definition XnArray.h:155