bitSet.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _BITSET_H_
  23. #define _BITSET_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. /// A convenience class to manipulate a set of bits.
  28. ///
  29. /// Notice that bits are accessed directly, ie, by passing
  30. /// a variable with the relevant bit set or not, instead of
  31. /// passing the index of the relevant bit.
  32. class BitSet32
  33. {
  34. private:
  35. /// Internal representation of bitset.
  36. U32 mbits;
  37. public:
  38. BitSet32() { mbits = 0; }
  39. BitSet32(const BitSet32& in_rCopy) { mbits = in_rCopy.mbits; }
  40. BitSet32(const U32 in_mask) { mbits = in_mask; }
  41. operator U32() const { return mbits; }
  42. U32 getMask() const { return mbits; }
  43. /// Set all bits to true.
  44. void set() { mbits = 0xFFFFFFFFUL; }
  45. /// Set the specified bit(s) to true.
  46. void set(const U32 m) { mbits |= m; }
  47. /// Masked-set the bitset; ie, using s as the mask and then setting the masked bits
  48. /// to b.
  49. void set(BitSet32 s, bool b) { mbits = (mbits&~(s.mbits))|(b?s.mbits:0); }
  50. /// Clear all bits.
  51. void clear() { mbits = 0; }
  52. /// Clear the specified bit(s).
  53. void clear(const U32 m) { mbits &= ~m; }
  54. /// Toggle the specified bit(s).
  55. void toggle(const U32 m) { mbits ^= m; }
  56. /// Are any of the specified bit(s) set?
  57. bool test(const U32 m) const { return (mbits & m) != 0; }
  58. /// Are ALL the specified bit(s) set?
  59. bool testStrict(const U32 m) const { return (mbits & m) == m; }
  60. /// @name Operator Overloads
  61. /// @{
  62. BitSet32& operator =(const U32 m) { mbits = m; return *this; }
  63. BitSet32& operator|=(const U32 m) { mbits |= m; return *this; }
  64. BitSet32& operator&=(const U32 m) { mbits &= m; return *this; }
  65. BitSet32& operator^=(const U32 m) { mbits ^= m; return *this; }
  66. BitSet32 operator|(const U32 m) const { return BitSet32(mbits | m); }
  67. BitSet32 operator&(const U32 m) const { return BitSet32(mbits & m); }
  68. BitSet32 operator^(const U32 m) const { return BitSet32(mbits ^ m); }
  69. /// @}
  70. };
  71. #endif //_NBITSET_H_