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