BitSet.h 5.5 KB

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