Panda3D
bitMask.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file bitMask.h
10  * @author drose
11  * @date 2000-06-08
12  */
13 
14 #ifndef BITMASK_H
15 #define BITMASK_H
16 
17 #include "pandabase.h"
18 #include "pbitops.h"
19 #include "numeric_types.h"
20 #include "typedObject.h"
21 #include "indent.h"
22 #include "pnotify.h"
23 
24 #include "checksumHashGenerator.h"
25 
26 
27 /**
28  * A general bitmask class. This stores an array of bits of some length that
29  * must fit within a given word of the indicated type. See also BitArray.
30  */
31 template<class WType, int nbits>
32 class BitMask {
33 public:
34  typedef WType WordType;
35 
36 PUBLISHED:
37  enum { num_bits = nbits };
38 
39  constexpr BitMask() = default;
40  ALWAYS_INLINE constexpr BitMask(WordType init_value);
41 
42  INLINE static BitMask<WType, nbits> all_on();
43  INLINE static BitMask<WType, nbits> all_off();
44  INLINE static BitMask<WType, nbits> lower_on(int on_bits);
45  INLINE static BitMask<WType, nbits> bit(int index);
46  INLINE static BitMask<WType, nbits> range(int low_bit, int size);
47 
48  constexpr static bool has_max_num_bits() { return true; }
49  constexpr static int get_max_num_bits() { return num_bits; }
50 
51  constexpr int get_num_bits() const;
52  INLINE bool get_bit(int index) const;
53  INLINE void set_bit(int index);
54  INLINE void clear_bit(int index);
55  INLINE void set_bit_to(int index, bool value);
56  INLINE bool is_zero() const;
57  INLINE bool is_all_on() const;
58 
59  INLINE WordType extract(int low_bit, int size) const;
60  INLINE void store(WordType value, int low_bit, int size);
61  INLINE bool has_any_of(int low_bit, int size) const;
62  INLINE bool has_all_of(int low_bit, int size) const;
63  INLINE void set_range(int low_bit, int size);
64  INLINE void clear_range(int low_bit, int size);
65  INLINE void set_range_to(bool value, int low_bit, int size);
66  INLINE WordType get_word() const;
67  INLINE void set_word(WordType value);
68 
69  INLINE int get_num_on_bits() const;
70  INLINE int get_num_off_bits() const;
71  INLINE int get_lowest_on_bit() const;
72  INLINE int get_lowest_off_bit() const;
73  INLINE int get_highest_on_bit() const;
74  INLINE int get_highest_off_bit() const;
75  INLINE int get_next_higher_different_bit(int low_bit) const;
76 
77  INLINE void invert_in_place();
78  INLINE bool has_bits_in_common(const BitMask<WType, nbits> &other) const;
79  INLINE void clear();
80 
81  void output(std::ostream &out) const;
82  void output_binary(std::ostream &out, int spaces_every = 4) const;
83  void output_hex(std::ostream &out, int spaces_every = 4) const;
84  void write(std::ostream &out, int indent_level = 0) const;
85 
86  INLINE bool operator == (const BitMask<WType, nbits> &other) const;
87  INLINE bool operator != (const BitMask<WType, nbits> &other) const;
88  INLINE bool operator < (const BitMask<WType, nbits> &other) const;
89  INLINE int compare_to(const BitMask<WType, nbits> &other) const;
90 
92  operator & (const BitMask<WType, nbits> &other) const;
93 
95  operator | (const BitMask<WType, nbits> &other) const;
96 
98  operator ^ (const BitMask<WType, nbits> &other) const;
99 
100  INLINE BitMask<WType, nbits>
101  operator ~ () const;
102 
103  INLINE BitMask<WType, nbits>
104  operator << (int shift) const;
105 
106  INLINE BitMask<WType, nbits>
107  operator >> (int shift) const;
108 
109  INLINE void operator &= (const BitMask<WType, nbits> &other);
110  INLINE void operator |= (const BitMask<WType, nbits> &other);
111  INLINE void operator ^= (const BitMask<WType, nbits> &other);
112  INLINE void operator <<= (int shift);
113  INLINE void operator >>= (int shift);
114 
115  INLINE void flood_down_in_place();
116  INLINE void flood_up_in_place();
117  INLINE BitMask<WType, nbits> flood_bits_down() const;
118  INLINE BitMask<WType, nbits> flood_bits_up() const;
121  INLINE BitMask<WType, nbits> keep_next_highest_bit(int index) const;
122  INLINE BitMask<WType, nbits> keep_next_lowest_bit(int index) const;
125 
126  INLINE int get_key() const;
127 
128  INLINE bool __nonzero__() const;
129  EXTENSION(PyObject *__reduce__(PyObject *self) const);
130 
131 public:
132  INLINE void generate_hash(ChecksumHashGenerator &hashgen) const;
133 
134 private:
135  WordType _word = 0u;
136 
137 public:
138  static TypeHandle get_class_type() {
139  return _type_handle;
140  }
141  static void init_type(const std::string &name);
142 
143 private:
144  static TypeHandle _type_handle;
145 };
146 
147 #include "bitMask.I"
148 
149 template<class WType, int nbits>
150 INLINE std::ostream &operator << (std::ostream &out, const BitMask<WType, nbits> &bitmask) {
151  bitmask.output(out);
152  return out;
153 }
154 
155 // We need to define this temporary macro so we can pass a parameter
156 // containing a comma through the macro.
157 #define BITMASK16_DEF BitMask<uint16_t, 16>
158 #define BITMASK32_DEF BitMask<uint32_t, 32>
159 #define BITMASK64_DEF BitMask<uint64_t, 64>
160 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK16_DEF);
161 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK32_DEF);
162 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK64_DEF);
163 
167 
168 #if NATIVE_WORDSIZE == 32
169 typedef BitMask32 BitMaskNative;
170 #elif NATIVE_WORDSIZE == 64
171 typedef BitMask64 BitMaskNative;
172 #else
173 #error No definition for NATIVE_WORDSIZE--should be defined in dtoolbase.h.
174 #endif // NATIVE_WORDSIZE
175 
176 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A general bitmask class.
Definition: bitMask.h:32
void flood_up_in_place()
Floods this bitmask's bits upwards.
Definition: bitMask.I:658
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.
Definition: bitMask.I:698
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
Definition: bitMask.I:283
bool has_all_of(int low_bit, int size) const
Returns true if all bits in the indicated range are set, false otherwise.
Definition: bitMask.I:203
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
Definition: bitMask.I:213
static BitMask< WType, nbits > bit(int index)
Returns a BitMask with only the indicated bit on.
Definition: bitMask.I:70
void set_bit_to(int index, bool value)
Sets the nth bit either on or off, according to the indicated bool value.
Definition: bitMask.I:140
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the mask.
Definition: bitMask.I:313
void set_bit(int index)
Sets the nth bit on.
Definition: bitMask.I:119
BitMask< WType, nbits > flood_bits_up() const
Returns a BitMask with the bits flooded upwards.
Definition: bitMask.I:676
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the mask.
Definition: bitMask.I:303
static BitMask< WType, nbits > all_on()
Returns a BitMask whose bits are all on.
Definition: bitMask.I:32
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.
Definition: bitMask.I:386
void store(WordType value, int low_bit, int size)
Stores the indicated word into the indicated range of bits with this BitMask.
Definition: bitMask.I:183
bool is_all_on() const
Returns true if the entire bitmask is one, false otherwise.
Definition: bitMask.I:162
static BitMask< WType, nbits > all_off()
Returns a BitMask whose bits are all off.
Definition: bitMask.I:43
void output_binary(std::ostream &out, int spaces_every=4) const
Writes the BitMask out as a binary number, with spaces every four bits.
Definition: bitMask.I:418
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
Definition: bitMask.I:109
void set_range_to(bool value, int low_bit, int size)
Sets the indicated range of bits to either on or off.
Definition: bitMask.I:233
void clear_bit(int index)
Sets the nth bit off.
Definition: bitMask.I:129
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
Definition: bitMask.I:293
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the bitmask to the indicated hash generator.
Definition: bitMask.I:640
void output_hex(std::ostream &out, int spaces_every=4) const
Writes the BitMask out as a hexadecimal number, with spaces every four digits.
Definition: bitMask.I:433
void clear()
Sets all the bits in the BitMask off.
Definition: bitMask.I:395
static BitMask< WType, nbits > range(int low_bit, int size)
Returns a BitMask whose size bits, beginning at low_bit, are on.
Definition: bitMask.I:81
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the mask.
Definition: bitMask.I:264
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.
Definition: bitMask.I:713
int get_key() const
Returns a mostly unique integer key per unique bitmask, suitable for using in a hash table.
Definition: bitMask.I:622
void write(std::ostream &out, int indent_level=0) const
Writes the BitMask out as a binary or a hex number, according to the number of bits.
Definition: bitMask.I:455
void invert_in_place()
Inverts all the bits in the BitMask.
Definition: bitMask.I:372
bool __nonzero__() const
Returns true if the bitmask is not zero.
Definition: bitMask.I:631
static BitMask< WType, nbits > lower_on(int on_bits)
Returns a BitMask whose lower on_bits bits are on.
Definition: bitMask.I:54
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
Definition: bitMask.I:223
constexpr int get_num_bits() const
Returns the number of bits available to set in the bitmask.
Definition: bitMask.I:99
void set_word(WordType value)
Sets the entire BitMask to the value indicated by the given word.
Definition: bitMask.I:255
int compare_to(const BitMask< WType, nbits > &other) const
Returns a number less than zero if this BitMask sorts before the indicated other BitMask,...
Definition: bitMask.I:497
bool has_any_of(int low_bit, int size) const
Returns true if any bit in the indicated range is set, false otherwise.
Definition: bitMask.I:193
void output(std::ostream &out) const
Writes the BitMask out as a binary or a hex number, according to the number of bits.
Definition: bitMask.I:405
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the mask.
Definition: bitMask.I:273
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
Definition: bitMask.I:153
WordType get_word() const
Returns the entire BitMask as a single word.
Definition: bitMask.I:246
BitMask< WType, nbits > flood_bits_down() const
Returns a BitMask with the bits flooded down.
Definition: bitMask.I:686
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...
Definition: bitMask.I:326
bool operator<(const BitMask< WType, nbits > &other) const
The ordering operator is of limited usefulness with a BitMask, however, it has a definition which pla...
Definition: bitMask.I:486
WordType extract(int low_bit, int size) const
Returns a word that represents only the indicated range of bits within this BitMask,...
Definition: bitMask.I:172
void flood_down_in_place()
Floods this bitmask's bits downwards.
Definition: bitMask.I:667
This is a specific kind of HashGenerator that simply adds up all of the ints.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.