BitSet.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. {
  11. /// @addtogroup util_containers
  12. /// @{
  13. /// Easy bit manipulation.
  14. /// @tparam N The number of bits.
  15. /// @tparam TChunkType The type of the chunks that the bitset consists. By default it's U8.
  16. template<U32 N, typename TChunkType = U8>
  17. class BitSet
  18. {
  19. private:
  20. using ChunkType = TChunkType;
  21. /// Number of bits a chunk holds.
  22. static constexpr U32 CHUNK_BIT_COUNT = sizeof(ChunkType) * 8;
  23. /// Number of chunks.
  24. static constexpr U32 CHUNK_COUNT = (N + (CHUNK_BIT_COUNT - 1)) / CHUNK_BIT_COUNT;
  25. public:
  26. /// Constructor. It will set all the bits or unset them.
  27. BitSet(Bool set)
  28. {
  29. ANKI_ASSERT(set == 0 || set == 1);
  30. if(!set)
  31. {
  32. unsetAll();
  33. }
  34. else
  35. {
  36. setAll();
  37. }
  38. }
  39. /// Copy.
  40. BitSet(const BitSet& b)
  41. : m_chunks(b.m_chunks)
  42. {
  43. }
  44. /// Copy.
  45. BitSet& operator=(const BitSet& b)
  46. {
  47. m_chunks = b.m_chunks;
  48. return *this;
  49. }
  50. /// Bitwise or between this and @a b sets.
  51. BitSet operator|(const BitSet& b) const
  52. {
  53. BitSet out;
  54. for(U32 i = 0; i < CHUNK_COUNT; ++i)
  55. {
  56. out.m_chunks[i] = m_chunks[i] | b.m_chunks[i];
  57. }
  58. return out;
  59. }
  60. /// Bitwise or between this and @a b sets.
  61. BitSet& operator|=(const BitSet& b)
  62. {
  63. for(U32 i = 0; i < CHUNK_COUNT; ++i)
  64. {
  65. m_chunks[i] = m_chunks[i] | b.m_chunks[i];
  66. }
  67. return *this;
  68. }
  69. /// Bitwise and between this and @a b sets.
  70. BitSet operator&(const BitSet& b) const
  71. {
  72. BitSet out;
  73. for(U32 i = 0; i < CHUNK_COUNT; ++i)
  74. {
  75. out.m_chunks[i] = m_chunks[i] & b.m_chunks[i];
  76. }
  77. return out;
  78. }
  79. /// Bitwise and between this and @a b sets.
  80. BitSet& operator&=(const BitSet& b)
  81. {
  82. for(U32 i = 0; i < CHUNK_COUNT; ++i)
  83. {
  84. m_chunks[i] = m_chunks[i] & b.m_chunks[i];
  85. }
  86. return *this;
  87. }
  88. /// Bitwise xor between this and @a b sets.
  89. BitSet operator^(const BitSet& b) const
  90. {
  91. BitSet out;
  92. for(U i = 0; i < CHUNK_COUNT; ++i)
  93. {
  94. out.m_chunks[i] = m_chunks[i] ^ b.m_chunks[i];
  95. }
  96. return out;
  97. }
  98. /// Bitwise xor between this and @a b sets.
  99. BitSet& operator^=(const BitSet& b)
  100. {
  101. for(U32 i = 0; i < CHUNK_COUNT; ++i)
  102. {
  103. m_chunks[i] = m_chunks[i] ^ b.m_chunks[i];
  104. }
  105. return *this;
  106. }
  107. /// Bitwise not of self.
  108. BitSet operator~() const
  109. {
  110. BitSet out;
  111. for(U32 i = 0; i < CHUNK_COUNT; ++i)
  112. {
  113. out.m_chunks[i] = TChunkType(~m_chunks[i]);
  114. }
  115. out.zeroUnusedBits();
  116. return out;
  117. }
  118. Bool operator==(const BitSet& b) const
  119. {
  120. Bool same = m_chunks[0] == b.m_chunks[0];
  121. for(U32 i = 1; i < CHUNK_COUNT; ++i)
  122. {
  123. same = same && (m_chunks[i] == b.m_chunks[i]);
  124. }
  125. return same;
  126. }
  127. Bool operator!=(const BitSet& b) const
  128. {
  129. return !(*this == b);
  130. }
  131. Bool operator!() const
  132. {
  133. return !getAny();
  134. }
  135. explicit operator Bool() const
  136. {
  137. return getAny();
  138. }
  139. /// Set or unset a bit at the given position.
  140. template<typename TInt>
  141. BitSet& set(TInt pos, Bool setBits = true)
  142. {
  143. U32 high, low;
  144. position(U32(pos), high, low);
  145. const ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low));
  146. m_chunks[high] = (setBits) ? ChunkType(m_chunks[high] | mask) : ChunkType(m_chunks[high] & ~mask);
  147. return *this;
  148. }
  149. /// Set multiple bits.
  150. template<typename TInt>
  151. BitSet& set(std::initializer_list<TInt> list, Bool setBits = true)
  152. {
  153. for(auto it : list)
  154. {
  155. set(it, setBits);
  156. }
  157. return *this;
  158. }
  159. /// Set all bits.
  160. BitSet& setAll()
  161. {
  162. memset(&m_chunks[0], 0xFF, sizeof(m_chunks));
  163. zeroUnusedBits();
  164. return *this;
  165. }
  166. /// Unset a bit (set to zero) at the given position.
  167. template<typename TInt>
  168. BitSet& unset(TInt pos)
  169. {
  170. return set(pos, false);
  171. }
  172. /// Unset multiple bits.
  173. template<typename TInt>
  174. BitSet& unset(std::initializer_list<TInt> list)
  175. {
  176. return set(list, false);
  177. }
  178. /// Unset all bits.
  179. BitSet& unsetAll()
  180. {
  181. memset(&m_chunks[0], 0, sizeof(m_chunks));
  182. return *this;
  183. }
  184. /// Flip the bits at the given position. It will go from 1 to 0 or from 0 to 1.
  185. template<typename TInt>
  186. BitSet& flip(TInt pos)
  187. {
  188. U32 high, low;
  189. position(U32(pos), high, low);
  190. const ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low));
  191. m_chunks[high] ^= mask;
  192. return *this;
  193. }
  194. /// Return true if the bit is set or false if it's not.
  195. template<typename TInt>
  196. Bool get(TInt pos) const
  197. {
  198. U32 high, low;
  199. position(U32(pos), high, low);
  200. const ChunkType mask = ChunkType(ChunkType(1) << ChunkType(low));
  201. return (m_chunks[high] & mask) != 0;
  202. }
  203. /// Any are enabled.
  204. Bool getAny() const
  205. {
  206. static const BitSet ZERO(false);
  207. return *this != ZERO;
  208. }
  209. /// Count bits.
  210. U32 getEnabledBitCount() const
  211. {
  212. U32 count = 0;
  213. for(U i = 0; i < CHUNK_COUNT; ++i)
  214. {
  215. count += __builtin_popcountl(m_chunks[i]);
  216. }
  217. return count;
  218. }
  219. /// Get the most significant bit that is enabled. Or MAX_U32 if all is zero.
  220. U32 getMostSignificantBit() const
  221. {
  222. U32 i = CHUNK_COUNT;
  223. while(i--)
  224. {
  225. const U64 bits = m_chunks[i];
  226. if(bits != 0)
  227. {
  228. const U32 msb = U32(__builtin_clzll(bits));
  229. return (63 - msb) + (i * CHUNK_BIT_COUNT);
  230. }
  231. }
  232. return MAX_U32;
  233. }
  234. Array<TChunkType, CHUNK_COUNT> getData() const
  235. {
  236. return m_chunks;
  237. }
  238. private:
  239. Array<ChunkType, CHUNK_COUNT> m_chunks;
  240. BitSet()
  241. {
  242. }
  243. static void position(U32 bit, U32& high, U32& low)
  244. {
  245. ANKI_ASSERT(bit < N);
  246. high = bit / CHUNK_BIT_COUNT;
  247. low = bit % CHUNK_BIT_COUNT;
  248. ANKI_ASSERT(high < CHUNK_COUNT);
  249. ANKI_ASSERT(low < CHUNK_BIT_COUNT);
  250. }
  251. /// Zero the unused bits.
  252. void zeroUnusedBits()
  253. {
  254. const ChunkType UNUSED_BITS = CHUNK_COUNT * CHUNK_BIT_COUNT - N;
  255. const ChunkType USED_BITMASK = std::numeric_limits<ChunkType>::max() >> UNUSED_BITS;
  256. if(USED_BITMASK > 0)
  257. {
  258. m_chunks[CHUNK_COUNT - 1] &= USED_BITMASK;
  259. }
  260. }
  261. };
  262. /// @}
  263. } // end namespace anki