OpenNI 1.5.4
XnBitSet.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 __XNBITSET_H__
23 #define __XNBITSET_H__
24 
25 #include <XnArray.h>
26 
27 class XnBitSet
28 {
29 public:
30  XnBitSet() : m_nSize(0) {}
31 
34  XnStatus Reserve(XnUInt32 nBits)
35  {
36  return m_array.Reserve((nBits >> 5) + 1);
37  }
38 
40  XnStatus SetSize(XnUInt32 nBits)
41  {
42  return m_array.SetSize((nBits >> 5) + 1, 0);
43  }
44 
46  XnStatus Set(XnUInt32 nIndex, XnBool bValue)
47  {
48  XnUInt32 nArrayIndex = (nIndex >> 5);
49  XnUInt32 nMask = (1 << ((~nIndex) & 0x1F));
50  XnUInt32 nOldVal = nArrayIndex < m_array.GetSize() ? m_array[nArrayIndex] : 0;
51  XnUInt32 nNewVal = bValue ? (nOldVal | nMask) : (nOldVal & (~nMask));
52  XnStatus nRetVal = m_array.Set(nArrayIndex, nNewVal, 0);
53  XN_IS_STATUS_OK(nRetVal);
54  m_nSize = XN_MAX(m_nSize, nIndex + 1);
55  return XN_STATUS_OK;
56  }
57 
59  XnBool IsSet(XnUInt32 nIndex) const
60  {
61  XnUInt32 nArrayIndex = (nIndex >> 5);
62  if (nArrayIndex >= m_array.GetSize())
63  {
64  return FALSE;
65  }
66  return (m_array[nArrayIndex] & (1 << ((~nIndex) & 0x1F))) ? TRUE : FALSE;
67  }
68 
70  XnStatus SetData(const XnUInt32* pData, XnUInt32 nSizeInDwords)
71  {
72  XnStatus nRetVal = m_array.SetData(pData, nSizeInDwords);
73  XN_IS_STATUS_OK(nRetVal);
74  m_nSize = (nSizeInDwords << 5);
75  return XN_STATUS_OK;
76  }
77 
79  XnStatus SetDataBytes(const XnUInt8* pData, XnUInt32 nSizeInBytes)
80  {
81  //XnStatus nRetVal = m_array.SetData(reinterpret_cast<const XnUInt32*>(pData), XN_MAX(1, nSizeInBytes >> 2));
82  XnUInt32 nSizeInDwords = XN_MAX(1, nSizeInBytes >> 2);
83  XnStatus nRetVal = m_array.SetSize(nSizeInDwords);
84  XN_IS_STATUS_OK(nRetVal);
85  for (XnUInt32 nDwordIdx = 0, nByteIdx = 0; nDwordIdx < nSizeInDwords; nDwordIdx++, nByteIdx += 4)
86  {
87  m_array[nDwordIdx] = ((pData[nByteIdx] << 24) | (pData[nByteIdx + 1] << 16) | (pData[nByteIdx + 2] << 8) | pData[nByteIdx + 3] );
88  }
89  m_nSize = (nSizeInBytes << 3);
90  return XN_STATUS_OK;
91  }
92 
94  const XnUInt32* GetData() const
95  {
96  return m_array.GetData();
97  }
98 
100  XnUInt32* GetData()
101  {
102  return m_array.GetData();
103  }
104 
106  XnUInt32 GetDataSize() const
107  {
108  return m_array.GetSize();
109  }
110 
112  XnUInt32 GetSize() const
113  {
114  return m_nSize;
115  }
116 
118  void Clear()
119  {
120  m_array.Clear();
121  m_nSize = 0;
122  }
123 
125  XnBool IsEmpty() const
126  {
127  return m_array.IsEmpty();
128  }
129 
130 private:
131  XnArray<XnUInt32> m_array;
132  XnUInt32 m_nSize;
133 };
134 
135 #endif // __XNBITSET_H__
#define XN_IS_STATUS_OK(x)
Definition: XnMacros.h:60
XnBool IsEmpty() const
Definition: XnBitSet.h:125
#define FALSE
Definition: XnPlatform.h:85
Definition: XnBitSet.h:27
#define XN_STATUS_OK
Definition: XnStatus.h:37
XnStatus SetSize(XnUInt32 nBits)
Definition: XnBitSet.h:40
const T * GetData() const
Definition: XnArray.h:95
XnUInt32 GetDataSize() const
Definition: XnBitSet.h:106
XnUInt32 XnStatus
Definition: XnStatus.h:34
XnBitSet()
Definition: XnBitSet.h:30
XnStatus Set(XnUInt32 nIndex, const T &val)
Definition: XnArray.h:218
#define XN_MAX(a, b)
Definition: XnPlatform.h:90
XnStatus Reserve(XnUInt32 nBits)
Definition: XnBitSet.h:34
XnStatus SetSize(XnUInt32 nSize)
Definition: XnArray.h:155
#define TRUE
Definition: XnPlatform.h:81
XnStatus SetDataBytes(const XnUInt8 *pData, XnUInt32 nSizeInBytes)
Definition: XnBitSet.h:79
XnStatus SetData(const T *pData, XnUInt32 nSize)
Definition: XnArray.h:82
const XnUInt32 * GetData() const
Definition: XnBitSet.h:94
XnStatus SetData(const XnUInt32 *pData, XnUInt32 nSizeInDwords)
Definition: XnBitSet.h:70
XnUInt32 GetSize() const
Definition: XnArray.h:148
XnBool IsSet(XnUInt32 nIndex) const
Definition: XnBitSet.h:59
void Clear()
Definition: XnArray.h:256
XnBool IsEmpty() const
Definition: XnArray.h:142
XnUInt32 GetSize() const
Definition: XnBitSet.h:112
XnUInt32 * GetData()
Definition: XnBitSet.h:100
void Clear()
Definition: XnBitSet.h:118
XnStatus Set(XnUInt32 nIndex, XnBool bValue)
Definition: XnBitSet.h:46
XnStatus Reserve(XnUInt32 nReservedSize)
Definition: XnArray.h:108