gwnavruntime/kernel/SF_BitSet.h Source File

SF_BitSet.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: None
10 Filename : KY_BitSet.h
11 Content : Template implementation for a simple BitSet
12 Created : Feb, 2010
13 Authors : Artem Bolgar, Sergey Sikorskiy
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_BitSet_H
18 #define INC_KY_Kernel_BitSet_H
19 
21 
22 #undef new
23 
24 namespace Kaim {
25 
26 // A simple non-expandable basic bitset class
27 template <class Allocator>
28 class FixedBitSetBase
29 {
30 public:
31  FixedBitSetBase(const void* pheapAddr, unsigned bitsCount)
32  : BitsCount(bitsCount)
33  {
34  unsigned dataLen = (BitsCount + 7)/8;
35  pData = (UByte*)Allocator::Alloc(pheapAddr, dataLen, __FILE__, __LINE__);
36  memset(pData, 0, dataLen);
37  }
38  FixedBitSetBase(const FixedBitSetBase& other)
39  : BitsCount(other.BitsCount)
40  {
41  unsigned dataLen = (BitsCount + 7)/8;
42  pData = (UByte*)Allocator::Alloc(Memory::GetHeapByAddress(other.pData), dataLen, __FILE__, __LINE__);
43  memcpy(pData, other.pData, dataLen);
44  }
45  ~FixedBitSetBase()
46  {
47  Allocator::Free(pData);
48  }
49 
50  FixedBitSetBase& operator =(const FixedBitSetBase& other)
51  {
52  if (this != &other)
53  {
54  Allocator::Free(pData);
55 
56  BitsCount = other.BitsCount;
57  unsigned dataLen = (BitsCount + 7)/8;
58  pData = (UByte*)Allocator::Alloc(Memory::GetHeapByAddress(other.pData), dataLen, __FILE__, __LINE__);
59  memcpy(pData, other.pData, dataLen);
60  }
61 
62  return *this;
63  }
64 
65  void Set(unsigned bitIndex)
66  {
67  KY_ASSERT(bitIndex < BitsCount);
68  pData[bitIndex >> 3] |= (1 << (bitIndex & 7));
69  }
70 
71  void Set(unsigned bitIndex, bool s)
72  {
73  (s) ? Set(bitIndex) : Clear(bitIndex);
74  }
75 
76  void Clear(unsigned bitIndex)
77  {
78  KY_ASSERT(bitIndex < BitsCount);
79  pData[bitIndex >> 3] &= ~(1 << (bitIndex & 7));
80  }
81 
82  bool IsSet(unsigned bitIndex) const
83  {
84  KY_ASSERT(bitIndex < BitsCount);
85  return (pData[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0;
86  }
87  bool operator[](unsigned bitIndex) const { return IsSet(bitIndex); }
88 
89 protected:
90  UByte* pData;
91  unsigned BitsCount;
92 };
93 
94 template<int SID=Stat_Default_Mem>
95 class FixedBitSetLH : public FixedBitSetBase<AllocatorLH<UByte, SID> >
96 {
97 public:
98  typedef FixedBitSetBase<AllocatorLH<UByte, SID> > BaseType;
99 
100 protected:
101  void* getThis() { return this; }
102 public:
103  FixedBitSetLH(unsigned bitsCount) : BaseType(getThis(), bitsCount) {}
104 };
105 
106 template<int SID=Stat_Default_Mem>
107 class FixedBitSetDH : public FixedBitSetBase<AllocatorDH<UByte, SID> >
108 {
109 public:
110  typedef FixedBitSetBase<AllocatorDH<UByte, SID> > BaseType;
111 
112  FixedBitSetDH(MemoryHeap* pheap, unsigned bitsCount) : BaseType(pheap, bitsCount) {}
113 };
114 
115 template<int SID=Stat_Default_Mem>
116 class FixedBitSetGH : public FixedBitSetBase<AllocatorGH<UByte, SID> >
117 {
118 public:
119  typedef FixedBitSetBase<AllocatorGH<UByte, SID> > BaseType;
120 
121  FixedBitSetGH(unsigned bitsCount) : BaseType(Memory::GetGlobalHeap(), bitsCount) {}
122 };
123 
124 } // Scaleform
125 
126 #endif // INC_KY_Kernel_BitSet_H
std::uint8_t UByte
uint8_t
Definition: SF_Types.h:134
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17