BitSet.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Copyright (C) 2009-2021, 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 N The number of bits.
  14. /// @tparam TChunkType The type of the chunks that the bitset consists. By default it's U8.
  15. template<U32 N, typename TChunkType = U8>
  16. class BitSet
  17. {
  18. private:
  19. using ChunkType = TChunkType;
  20. /// Number of bits a chunk holds.
  21. static constexpr U32 CHUNK_BIT_COUNT = sizeof(ChunkType) * 8;
  22. /// Number of chunks.
  23. static constexpr U32 CHUNK_COUNT = (N + (CHUNK_BIT_COUNT - 1)) / CHUNK_BIT_COUNT;
  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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 < CHUNK_COUNT; ++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 !getAny();
  133. }
  134. explicit operator Bool() const
  135. {
  136. return getAny();
  137. }
  138. /// Set or unset a bit at the given position.
  139. template<typename TInt>
  140. BitSet& set(TInt pos, Bool setBits = 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] = (setBits) ? 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 getAny() const
  204. {
  205. static const BitSet ZERO(false);
  206. return *this != ZERO;
  207. }
  208. /// Count bits.
  209. U32 getEnabledBitCount() const
  210. {
  211. U32 count = 0;
  212. for(U i = 0; i < CHUNK_COUNT; ++i)
  213. {
  214. count += __builtin_popcountl(m_chunks[i]);
  215. }
  216. return count;
  217. }
  218. /// Get the most significant bit that is enabled. Or MAX_U32 if all is zero.
  219. U32 getMostSignificantBit() const
  220. {
  221. U32 i = CHUNK_COUNT;
  222. while(i--)
  223. {
  224. const U64 bits = m_chunks[i];
  225. if(bits != 0)
  226. {
  227. const U32 msb = U32(__builtin_clzll(bits));
  228. return (63 - msb) + (i * CHUNK_BIT_COUNT);
  229. }
  230. }
  231. return MAX_U32;
  232. }
  233. Array<TChunkType, CHUNK_COUNT> getData() const
  234. {
  235. return m_chunks;
  236. }
  237. private:
  238. Array<ChunkType, CHUNK_COUNT> m_chunks;
  239. BitSet()
  240. {
  241. }
  242. static void position(U32 bit, U32& high, U32& low)
  243. {
  244. ANKI_ASSERT(bit < N);
  245. high = bit / CHUNK_BIT_COUNT;
  246. low = bit % CHUNK_BIT_COUNT;
  247. ANKI_ASSERT(high < CHUNK_COUNT);
  248. ANKI_ASSERT(low < CHUNK_BIT_COUNT);
  249. }
  250. /// Zero the unused bits.
  251. void zeroUnusedBits()
  252. {
  253. const ChunkType UNUSED_BITS = CHUNK_COUNT * CHUNK_BIT_COUNT - N;
  254. const ChunkType USED_BITMASK = std::numeric_limits<ChunkType>::max() >> UNUSED_BITS;
  255. if(USED_BITMASK > 0)
  256. {
  257. m_chunks[CHUNK_COUNT - 1] &= USED_BITMASK;
  258. }
  259. }
  260. };
  261. /// @}
  262. } // end namespace anki