BitSet.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Array.h>
  7. #include <initializer_list>
  8. #include <cstring>
  9. namespace anki {
  10. /// @addtogroup util_containers
  11. /// @{
  12. /// Easy bit manipulation.
  13. /// @tparam kBitCount The number of bits.
  14. /// @tparam TChunkType The type of the chunks that the bitset consists. By default it's U8.
  15. template<U32 kBitCount, typename TChunkType = U8>
  16. class BitSet
  17. {
  18. private:
  19. using ChunkType = TChunkType;
  20. /// Number of bits a chunk holds.
  21. static constexpr U32 kChunkBitCount = sizeof(ChunkType) * 8;
  22. /// Number of chunks.
  23. static constexpr U32 kChunkCount = (kBitCount + (kChunkBitCount - 1)) / kChunkBitCount;
  24. public:
  25. /// Constructor. It will set all the bits or unset them.
  26. BitSet(Bool set)
  27. {
  28. ANKI_ASSERT(set == 0 || set == 1);
  29. if(!set)
  30. {
  31. unsetAll();
  32. }
  33. else
  34. {
  35. setAll();
  36. }
  37. }
  38. /// Copy.
  39. BitSet(const BitSet& b)
  40. : m_chunks(b.m_chunks)
  41. {
  42. }
  43. /// Copy.
  44. BitSet& operator=(const BitSet& b)
  45. {
  46. m_chunks = b.m_chunks;
  47. return *this;
  48. }
  49. /// Bitwise or between this and @a b sets.
  50. BitSet operator|(const BitSet& b) const
  51. {
  52. BitSet out;
  53. for(U32 i = 0; i < kChunkCount; ++i)
  54. {
  55. out.m_chunks[i] = m_chunks[i] | b.m_chunks[i];
  56. }
  57. return out;
  58. }
  59. /// Bitwise or between this and @a b sets.
  60. BitSet& operator|=(const BitSet& b)
  61. {
  62. for(U32 i = 0; i < kChunkCount; ++i)
  63. {
  64. m_chunks[i] = m_chunks[i] | b.m_chunks[i];
  65. }
  66. return *this;
  67. }
  68. /// Bitwise and between this and @a b sets.
  69. BitSet operator&(const BitSet& b) const
  70. {
  71. BitSet out;
  72. for(U32 i = 0; i < kChunkCount; ++i)
  73. {
  74. out.m_chunks[i] = m_chunks[i] & b.m_chunks[i];
  75. }
  76. return out;
  77. }
  78. /// Bitwise and between this and @a b sets.
  79. BitSet& operator&=(const BitSet& b)
  80. {
  81. for(U32 i = 0; i < kChunkCount; ++i)
  82. {
  83. m_chunks[i] = m_chunks[i] & b.m_chunks[i];
  84. }
  85. return *this;
  86. }
  87. /// Bitwise xor between this and @a b sets.
  88. BitSet operator^(const BitSet& b) const
  89. {
  90. BitSet out;
  91. for(U i = 0; i < kChunkCount; ++i)
  92. {
  93. out.m_chunks[i] = m_chunks[i] ^ b.m_chunks[i];
  94. }
  95. return out;
  96. }
  97. /// Bitwise xor between this and @a b sets.
  98. BitSet& operator^=(const BitSet& b)
  99. {
  100. for(U32 i = 0; i < kChunkCount; ++i)
  101. {
  102. m_chunks[i] = m_chunks[i] ^ b.m_chunks[i];
  103. }
  104. return *this;
  105. }
  106. /// Bitwise not of self.
  107. BitSet operator~() const
  108. {
  109. BitSet out;
  110. for(U32 i = 0; i < kChunkCount; ++i)
  111. {
  112. out.m_chunks[i] = TChunkType(~m_chunks[i]);
  113. }
  114. out.zeroUnusedBits();
  115. return out;
  116. }
  117. Bool operator==(const BitSet& b) const
  118. {
  119. Bool same = m_chunks[0] == b.m_chunks[0];
  120. for(U32 i = 1; i < kChunkCount; ++i)
  121. {
  122. same = same && (m_chunks[i] == b.m_chunks[i]);
  123. }
  124. return same;
  125. }
  126. Bool operator!=(const BitSet& b) const
  127. {
  128. return !(*this == b);
  129. }
  130. Bool operator!() const
  131. {
  132. return !getAnySet();
  133. }
  134. explicit operator Bool() const
  135. {
  136. return getAnySet();
  137. }
  138. /// Set or unset a bit at the given position.
  139. template<typename TInt>
  140. BitSet& set(TInt pos, Bool setBit = true)
  141. {
  142. U32 high, low;
  143. position(U32(pos), high, low);
  144. const ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low));
  145. m_chunks[high] = (setBit) ? ChunkType(m_chunks[high] | mask) : ChunkType(m_chunks[high] & ~mask);
  146. return *this;
  147. }
  148. /// Set multiple bits.
  149. template<typename TInt>
  150. BitSet& set(std::initializer_list<TInt> list, Bool setBits = true)
  151. {
  152. for(auto it : list)
  153. {
  154. set(it, setBits);
  155. }
  156. return *this;
  157. }
  158. /// Set all bits.
  159. BitSet& setAll()
  160. {
  161. memset(&m_chunks[0], 0xFF, sizeof(m_chunks));
  162. zeroUnusedBits();
  163. return *this;
  164. }
  165. /// Unset a bit (set to zero) at the given position.
  166. template<typename TInt>
  167. BitSet& unset(TInt pos)
  168. {
  169. return set(pos, false);
  170. }
  171. /// Unset multiple bits.
  172. template<typename TInt>
  173. BitSet& unset(std::initializer_list<TInt> list)
  174. {
  175. return set(list, false);
  176. }
  177. /// Unset all bits.
  178. BitSet& unsetAll()
  179. {
  180. memset(&m_chunks[0], 0, sizeof(m_chunks));
  181. return *this;
  182. }
  183. /// Flip the bits at the given position. It will go from 1 to 0 or from 0 to 1.
  184. template<typename TInt>
  185. BitSet& flip(TInt pos)
  186. {
  187. U32 high, low;
  188. position(U32(pos), high, low);
  189. const ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low));
  190. m_chunks[high] ^= mask;
  191. return *this;
  192. }
  193. /// Return true if the bit is set or false if it's not.
  194. template<typename TInt>
  195. Bool get(TInt pos) const
  196. {
  197. U32 high, low;
  198. position(U32(pos), high, low);
  199. const ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low));
  200. return (m_chunks[high] & mask) != 0;
  201. }
  202. /// Any are enabled.
  203. Bool getAnySet() const
  204. {
  205. const BitSet kZero(false);
  206. return *this != kZero;
  207. }
  208. /// Count bits.
  209. U32 getSetBitCount() const
  210. {
  211. U32 count = 0;
  212. for(U i = 0; i < kChunkCount; ++i)
  213. {
  214. count += std::popcount(m_chunks[i]);
  215. }
  216. return count;
  217. }
  218. /// Get the most significant bit that is enabled. Or kMaxU32 if all is zero.
  219. U32 getMostSignificantBit() const
  220. {
  221. U32 i = kChunkCount;
  222. while(i--)
  223. {
  224. const U64 bits = m_chunks[i];
  225. if(bits != 0)
  226. {
  227. const U32 msb = U32(std::countl_zero(bits));
  228. return (63 - msb) + (i * kChunkBitCount);
  229. }
  230. }
  231. return kMaxU32;
  232. }
  233. /// Get the least significant bit that is enabled. Or kMaxU32 if all is zero.
  234. U32 getLeastSignificantBit() const
  235. {
  236. for(U32 i = 0; i < kChunkCount; ++i)
  237. {
  238. const U64 bits = m_chunks[i];
  239. if(bits != 0)
  240. {
  241. const U32 lsb = U32(std::countr_zero(bits));
  242. return lsb + (i * kChunkBitCount);
  243. }
  244. }
  245. return kMaxU32;
  246. }
  247. Array<TChunkType, kChunkCount> getData() const
  248. {
  249. return m_chunks;
  250. }
  251. /// Unset the N least significant bits of the bitset.
  252. BitSet& unsetNLeastSignificantBits(U32 n)
  253. {
  254. ANKI_ASSERT(n);
  255. U32 high, low;
  256. position(n - 1, high, low);
  257. for(U32 i = 0; i < high; ++i)
  258. {
  259. m_chunks[i] = 0;
  260. }
  261. // This could be a simple 1<<(low+1) but that may overflow so...
  262. ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low)) - ChunkType(1);
  263. mask <<= 1;
  264. mask |= 1;
  265. mask = ~mask;
  266. m_chunks[high] &= mask;
  267. return *this;
  268. }
  269. private:
  270. Array<ChunkType, kChunkCount> m_chunks;
  271. BitSet()
  272. {
  273. }
  274. static void position(U32 bit, U32& high, U32& low)
  275. {
  276. ANKI_ASSERT(bit < kBitCount);
  277. high = bit / kChunkBitCount;
  278. low = bit % kChunkBitCount;
  279. ANKI_ASSERT(high < kChunkCount);
  280. ANKI_ASSERT(low < kChunkBitCount);
  281. }
  282. /// Zero the unused bits.
  283. void zeroUnusedBits()
  284. {
  285. constexpr ChunkType kUnusedBits = kChunkCount * kChunkBitCount - kBitCount;
  286. constexpr ChunkType kUsedBitmask = std::numeric_limits<ChunkType>::max() >> kUnusedBits;
  287. if(kUsedBitmask > 0)
  288. {
  289. m_chunks[kChunkCount - 1] &= kUsedBitmask;
  290. }
  291. }
  292. };
  293. /// @}
  294. } // end namespace anki