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