Panda3D
bitMask.h
1 // Filename: bitMask.h
2 // Created by: drose (08Jun00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef BITMASK_H
16 #define BITMASK_H
17 
18 #include "pandabase.h"
19 #include "pbitops.h"
20 #include "numeric_types.h"
21 #include "typedObject.h"
22 #include "indent.h"
23 #include "pnotify.h"
24 
25 #include "checksumHashGenerator.h"
26 
27 
28 ////////////////////////////////////////////////////////////////////
29 // Class : BitMask
30 // Description : A general bitmask class. This stores an array of
31 // bits of some length that must fit within a given word
32 // of the indicated type. See also BitArray.
33 ////////////////////////////////////////////////////////////////////
34 template<class WType, int nbits>
35 class BitMask {
36 public:
37  typedef WType WordType;
38 
39 PUBLISHED:
40  enum { num_bits = nbits };
41 
42  INLINE BitMask();
43  INLINE BitMask(WordType init_value);
44  INLINE BitMask(const BitMask<WType, nbits> &copy);
45  INLINE BitMask<WType, nbits> &operator = (const BitMask<WType, nbits> &copy);
46 
47  INLINE static BitMask<WType, nbits> all_on();
48  INLINE static BitMask<WType, nbits> all_off();
49  INLINE static BitMask<WType, nbits> lower_on(int on_bits);
50  INLINE static BitMask<WType, nbits> bit(int index);
51  INLINE static BitMask<WType, nbits> range(int low_bit, int size);
52 
53  INLINE ~BitMask();
54 
55  CONSTEXPR static bool has_max_num_bits();
56  CONSTEXPR static int get_max_num_bits();
57 
58  CONSTEXPR static int get_num_bits();
59  INLINE bool get_bit(int index) const;
60  INLINE void set_bit(int index);
61  INLINE void clear_bit(int index);
62  INLINE void set_bit_to(int index, bool value);
63  INLINE bool is_zero() const;
64  INLINE bool is_all_on() const;
65 
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);
71  INLINE void clear_range(int low_bit, int size);
72  INLINE void set_range_to(bool value, int low_bit, int size);
73  INLINE WordType get_word() const;
74  INLINE void set_word(WordType value);
75 
76  INLINE int get_num_on_bits() const;
77  INLINE int get_num_off_bits() const;
78  INLINE int get_lowest_on_bit() const;
79  INLINE int get_lowest_off_bit() const;
80  INLINE int get_highest_on_bit() const;
81  INLINE int get_highest_off_bit() const;
82  INLINE int get_next_higher_different_bit(int low_bit) const;
83 
84  INLINE void invert_in_place();
85  INLINE bool has_bits_in_common(const BitMask<WType, nbits> &other) const;
86  INLINE void clear();
87 
88  void output(ostream &out) const;
89  void output_binary(ostream &out, int spaces_every = 4) const;
90  void output_hex(ostream &out, int spaces_every = 4) const;
91  void write(ostream &out, int indent_level = 0) const;
92 
93  INLINE bool operator == (const BitMask<WType, nbits> &other) const;
94  INLINE bool operator != (const BitMask<WType, nbits> &other) const;
95  INLINE bool operator < (const BitMask<WType, nbits> &other) const;
96  INLINE int compare_to(const BitMask<WType, nbits> &other) const;
97 
99  operator & (const BitMask<WType, nbits> &other) const;
100 
101  INLINE BitMask<WType, nbits>
102  operator | (const BitMask<WType, nbits> &other) const;
103 
104  INLINE BitMask<WType, nbits>
105  operator ^ (const BitMask<WType, nbits> &other) const;
106 
107  INLINE BitMask<WType, nbits>
108  operator ~ () const;
109 
110  INLINE BitMask<WType, nbits>
111  operator << (int shift) const;
112 
113  INLINE BitMask<WType, nbits>
114  operator >> (int shift) const;
115 
116  INLINE void operator &= (const BitMask<WType, nbits> &other);
117  INLINE void operator |= (const BitMask<WType, nbits> &other);
118  INLINE void operator ^= (const BitMask<WType, nbits> &other);
119  INLINE void operator <<= (int shift);
120  INLINE void operator >>= (int shift);
121 
122  INLINE void flood_down_in_place();
123  INLINE void flood_up_in_place();
124  INLINE BitMask<WType, nbits> flood_bits_down() const;
125  INLINE BitMask<WType, nbits> flood_bits_up() const;
128  INLINE BitMask<WType, nbits> keep_next_highest_bit(int index) const;
129  INLINE BitMask<WType, nbits> keep_next_lowest_bit(int index) const;
132 
133  INLINE int get_key() const;
134 
135  INLINE bool __nonzero__() const;
136 
137 public:
138  INLINE void generate_hash(ChecksumHashGenerator &hashgen) const;
139 
140 private:
141  WordType _word;
142 
143 public:
144  static TypeHandle get_class_type() {
145  return _type_handle;
146  }
147  static void init_type();
148 
149 private:
150  static TypeHandle _type_handle;
151 };
152 
153 #include "bitMask.I"
154 
155 template<class WType, int nbits>
156 INLINE ostream &operator << (ostream &out, const BitMask<WType, nbits> &bitmask) {
157  bitmask.output(out);
158  return out;
159 }
160 
161 // We need to define this temporary macro so we can pass a parameter
162 // containing a comma through the macro.
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);
169 
173 
174 #if NATIVE_WORDSIZE == 32
175 typedef BitMask32 BitMaskNative;
176 #elif NATIVE_WORDSIZE == 64
177 typedef BitMask64 BitMaskNative;
178 #else
179 #error No definition for NATIVE_WORDSIZE--should be defined in dtoolbase.h.
180 #endif // NATIVE_WORDSIZE
181 
182 // Tell GCC that we'll take care of the instantiation explicitly here.
183 #ifdef __GNUC__
184 #pragma interface
185 #endif
186 
187 #endif
void clear_bit(int index)
Sets the nth bit off.
Definition: bitMask.I:238
static BitMask< WType, nbits > bit(int index)
Returns a BitMask with only the indicated bit on.
Definition: bitMask.I:117
void flood_down_in_place()
Floods this bitmask&#39;s bits downwards.
Definition: bitMask.I:889
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the mask.
Definition: bitMask.I:467
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:319
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.
Definition: bitMask.I:912
void output_binary(ostream &out, int spaces_every=4) const
Writes the BitMask out as a binary number, with spaces every four bits.
Definition: bitMask.I:589
void invert_in_place()
Inverts all the bits in the BitMask.
Definition: bitMask.I:533
static BitMask< WType, nbits > all_on()
Returns a BitMask whose bits are all on.
Definition: bitMask.I:73
void output(ostream &out) const
Writes the BitMask out as a binary or a hex number, according to the number of bits.
Definition: bitMask.I:573
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...
Definition: bitMask.I:187
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
Definition: bitMask.I:344
void clear()
Sets all the bits in the BitMask off.
Definition: bitMask.I:561
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:306
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:484
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
Definition: bitMask.I:443
bool __nonzero__() const
Returns true if the bitmask is not zero.
Definition: bitMask.I:843
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.
Definition: bitMask.I:683
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:926
static BitMask< WType, nbits > all_off()
Returns a BitMask whose bits are all off.
Definition: bitMask.I:86
void output_hex(ostream &out, int spaces_every=4) const
Writes the BitMask out as a hexadecimal number, with spaces every four digits.
Definition: bitMask.I:606
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.
Definition: bitMask.I:630
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the mask.
Definition: bitMask.I:419
void set_word(WordType value)
Sets the entire BitMask to the value indicated by the given word.
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:131
void flood_up_in_place()
Floods this bitmask&#39;s bits upwards.
Definition: bitMask.I:878
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
Definition: bitMask.I:356
bool is_all_on() const
Returns true if the entire bitmask is one, false otherwise.
Definition: bitMask.I:280
WordType get_word() const
Returns the entire BitMask as a single word.
Definition: bitMask.I:383
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
Definition: bitMask.I:212
static BitMask< WType, nbits > lower_on(int on_bits)
Returns a BitMask whose lower on_bits bits are on.
Definition: bitMask.I:99
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the bitmask to the indicated hash generator.
Definition: bitMask.I:854
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
Definition: bitMask.I:431
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:252
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.
Definition: bitMask.I:293
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:550
BitMask< WType, nbits > flood_bits_up() const
Returns a BitMask with the bits flooded upwards.
Definition: bitMask.I:900
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:368
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the mask.
Definition: bitMask.I:455
void set_bit(int index)
Sets the nth bit on.
Definition: bitMask.I:225
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:332
static CONSTEXPR int get_num_bits()
Returns the number of bits available to set in the bitmask.
Definition: bitMask.I:199
int get_key() const
Returns a mostly unique integer key per unique bitmask, suitable for using in a hash table...
Definition: bitMask.I:832
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the mask.
Definition: bitMask.I:407
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A general bitmask class.
Definition: bitMask.h:35
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.
Definition: bitMask.I:168
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
Definition: bitMask.I:268
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:943