18 #include "pandabase.h" 20 #include "numeric_types.h" 21 #include "typedObject.h" 25 #include "checksumHashGenerator.h" 34 template<
class WType,
int nbits>
37 typedef WType WordType;
40 enum { num_bits = nbits };
43 INLINE BitMask(WordType init_value);
59 INLINE
bool get_bit(
int index)
const;
66 INLINE WordType
extract(
int low_bit,
int size)
const;
67 INLINE
void store(WordType value,
int low_bit,
int size);
68 INLINE
bool has_any_of(
int low_bit,
int size)
const;
69 INLINE
bool has_all_of(
int low_bit,
int size)
const;
70 INLINE
void set_range(
int low_bit,
int size);
72 INLINE
void set_range_to(
bool value,
int low_bit,
int size);
74 INLINE
void set_word(WordType value);
88 void output(ostream &out)
const;
90 void output_hex(ostream &out,
int spaces_every = 4)
const;
91 void write(ostream &out,
int indent_level = 0)
const;
95 INLINE
bool operator < (const BitMask<WType, nbits> &other)
const;
111 operator << (
int shift)
const;
114 operator >> (
int shift)
const;
119 INLINE
void operator <<= (
int shift);
120 INLINE
void operator >>= (
int shift);
147 static void init_type();
155 template<
class WType,
int nbits>
156 INLINE ostream &operator << (ostream &out, const BitMask<WType, nbits> &bitmask) {
163 #define BITMASK16_DEF BitMask<PN_uint16, 16> 164 #define BITMASK32_DEF BitMask<PN_uint32, 32> 165 #define BITMASK64_DEF BitMask<PN_uint64, 64> 166 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK16_DEF);
167 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK32_DEF);
168 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK64_DEF);
174 #if NATIVE_WORDSIZE == 32 176 #elif NATIVE_WORDSIZE == 64 179 #error No definition for NATIVE_WORDSIZE--should be defined in dtoolbase.h. 180 #endif // NATIVE_WORDSIZE void clear_bit(int index)
Sets the nth bit off.
static BitMask< WType, nbits > bit(int index)
Returns a BitMask with only the indicated bit on.
void flood_down_in_place()
Floods this bitmask's bits downwards.
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the mask.
bool has_any_of(int low_bit, int size) const
Returns true if any bit in the indicated range is set, false otherwise.
This is a specific kind of HashGenerator that simply adds up all of the ints.
BitMask< WType, nbits > flood_bits_down() const
Returns a BitMask with the bits flooded down.
void output_binary(ostream &out, int spaces_every=4) const
Writes the BitMask out as a binary number, with spaces every four bits.
void invert_in_place()
Inverts all the bits in the BitMask.
static BitMask< WType, nbits > all_on()
Returns a BitMask whose bits are all on.
void output(ostream &out) const
Writes the BitMask out as a binary or a hex number, according to the number of bits.
static CONSTEXPR int get_max_num_bits()
If get_max_num_bits() returned true, this method may be called to return the maximum number of bits t...
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
void clear()
Sets all the bits in the BitMask off.
void store(WordType value, int low_bit, int size)
Stores the indicated word into the indicated range of bits with this BitMask.
int get_next_higher_different_bit(int low_bit) const
Returns the index of the next bit in the mask, above low_bit, whose value is different that the value...
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
bool __nonzero__() const
Returns true if the bitmask is not zero.
int compare_to(const BitMask< WType, nbits > &other) const
Returns a number less than zero if this BitMask sorts before the indicated other BitMask, greater than zero if it sorts after, or 0 if they are equivalent.
BitMask< WType, nbits > keep_next_highest_bit() const
Returns a BitMask with only the next highest bit above the indicated bit on, or all_off.
static BitMask< WType, nbits > all_off()
Returns a BitMask whose bits are all off.
void output_hex(ostream &out, int spaces_every=4) const
Writes the BitMask out as a hexadecimal number, with spaces every four digits.
void write(ostream &out, int indent_level=0) const
Writes the BitMask out as a binary or a hex number, according to the number of bits.
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the mask.
void set_word(WordType value)
Sets the entire BitMask to the value indicated by the given word.
static BitMask< WType, nbits > range(int low_bit, int size)
Returns a BitMask whose size bits, beginning at low_bit, are on.
void flood_up_in_place()
Floods this bitmask's bits upwards.
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
bool is_all_on() const
Returns true if the entire bitmask is one, false otherwise.
WordType get_word() const
Returns the entire BitMask as a single word.
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
static BitMask< WType, nbits > lower_on(int on_bits)
Returns a BitMask whose lower on_bits bits are on.
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the bitmask to the indicated hash generator.
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
void set_bit_to(int index, bool value)
Sets the nth bit either on or off, according to the indicated bool value.
WordType extract(int low_bit, int size) const
Returns a word that represents only the indicated range of bits within this BitMask, shifted to the least-significant position.
bool has_bits_in_common(const BitMask< WType, nbits > &other) const
Returns true if this BitMask has any "one" bits in common with the other one, false otherwise...
BitMask< WType, nbits > flood_bits_up() const
Returns a BitMask with the bits flooded upwards.
void set_range_to(bool value, int low_bit, int size)
Sets the indicated range of bits to either on or off.
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the mask.
void set_bit(int index)
Sets the nth bit on.
bool has_all_of(int low_bit, int size) const
Returns true if all bits in the indicated range are set, false otherwise.
static CONSTEXPR int get_num_bits()
Returns the number of bits available to set in the bitmask.
int get_key() const
Returns a mostly unique integer key per unique bitmask, suitable for using in a hash table...
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the mask.
TypeHandle is the identifier used to differentiate C++ class types.
static CONSTEXPR bool has_max_num_bits()
Returns true if there is a maximum number of bits that may be stored in this structure, false otherwise.
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
BitMask< WType, nbits > keep_next_lowest_bit() const
Returns a BitMask with only the next lower bit below the indicated bit on, or all_off.