Panda3D
doubleBitMask.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 doubleBitMask.h
10  * @author drose
11  * @date 2000-06-08
12  */
13 
14 #ifndef DOUBLEBITMASK_H
15 #define DOUBLEBITMASK_H
16 
17 #include "pandabase.h"
18 
19 #include "bitMask.h"
20 
21 /**
22  * This is a special BitMask type that is implemented as a pair of lesser
23  * BitMask types, to present a double-wide bit mask. For instance, on a
24  * 32-bit system, this can be used to make a single 64-bit bit mask. More of
25  * these can be ganged up together to make a 128-bit mask, and so on.
26  */
27 template<class BMType>
29 public:
30  typedef typename BMType::WordType WordType;
31 
32 PUBLISHED:
33  typedef BMType BitMaskType;
34 
35  enum {
36  half_bits = BMType::num_bits,
37  num_bits = BMType::num_bits * 2,
38  };
39 
40  constexpr DoubleBitMask() = default;
41 
42  INLINE static DoubleBitMask<BMType> all_on();
43  INLINE static DoubleBitMask<BMType> all_off();
44  INLINE static DoubleBitMask<BMType> lower_on(int on_bits);
45  INLINE static DoubleBitMask<BMType> bit(int index);
46  INLINE static DoubleBitMask<BMType> 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 
67  INLINE int get_num_on_bits() const;
68  INLINE int get_num_off_bits() const;
69  INLINE int get_lowest_on_bit() const;
70  INLINE int get_lowest_off_bit() const;
71  INLINE int get_highest_on_bit() const;
72  INLINE int get_highest_off_bit() const;
73  INLINE int get_next_higher_different_bit(int low_bit) const;
74 
75  INLINE void invert_in_place();
76  INLINE bool has_bits_in_common(const DoubleBitMask<BMType> &other) const;
77  INLINE void clear();
78 
79  void output(std::ostream &out) const;
80  void output_binary(std::ostream &out, int spaces_every = 4) const;
81  void output_hex(std::ostream &out, int spaces_every = 4) const;
82  void write(std::ostream &out, int indent_level = 0) const;
83 
84  INLINE bool operator == (const DoubleBitMask<BMType> &other) const;
85  INLINE bool operator != (const DoubleBitMask<BMType> &other) const;
86  INLINE bool operator < (const DoubleBitMask<BMType> &other) const;
87  INLINE int compare_to(const DoubleBitMask<BMType> &other) const;
88 
90  operator & (const DoubleBitMask<BMType> &other) const;
91 
93  operator | (const DoubleBitMask<BMType> &other) const;
94 
96  operator ^ (const DoubleBitMask<BMType> &other) const;
97 
99  operator ~ () const;
100 
101  INLINE DoubleBitMask<BMType>
102  operator << (int shift) const;
103 
104  INLINE DoubleBitMask<BMType>
105  operator >> (int shift) const;
106 
107  INLINE void operator &= (const DoubleBitMask<BMType> &other);
108  INLINE void operator |= (const DoubleBitMask<BMType> &other);
109  INLINE void operator ^= (const DoubleBitMask<BMType> &other);
110  INLINE void operator <<= (int shift);
111  INLINE void operator >>= (int shift);
112 
113 public:
114  INLINE void generate_hash(ChecksumHashGenerator &hashgen) const;
115 
116 private:
117  BitMaskType _lo, _hi;
118 
119 public:
120  static TypeHandle get_class_type() {
121  return _type_handle;
122  }
123  static void init_type();
124 
125 private:
126  static TypeHandle _type_handle;
127 };
128 
129 #include "doubleBitMask.I"
130 
131 template<class BMType>
132 INLINE std::ostream &operator << (std::ostream &out, const DoubleBitMask<BMType> &doubleBitMask) {
133  doubleBitMask.output(out);
134  return out;
135 }
136 
137 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, DoubleBitMask<BitMaskNative>);
139 
140 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, DoubleBitMask<DoubleBitMaskNative>);
142 
143 #endif
bool is_all_on() const
Returns true if the entire doubleBitMask is one, false otherwise.
void output_binary(std::ostream &out, int spaces_every=4) const
Writes the DoubleBitMask out as a binary number, with spaces every four bits.
This is a specific kind of HashGenerator that simply adds up all of the ints.
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
static DoubleBitMask< BMType > range(int low_bit, int size)
Returns a DoubleBitMask whose size bits, beginning at low_bit, are on.
Definition: doubleBitMask.I:78
constexpr int get_num_bits() const
Returns the number of bits available to set in the doubleBitMask.
Definition: doubleBitMask.I:89
bool operator<(const DoubleBitMask< BMType > &other) const
The ordering operator is of limited usefulness with a DoubleBitMask, however, it has a definition whi...
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_range_to(bool value, int low_bit, int size)
Sets the indicated range of bits to either on or off.
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the doubleBitMask to the indicated hash generator.
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the mask.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a special BitMask type that is implemented as a pair of lesser BitMask types,...
Definition: doubleBitMask.h:28
void write(std::ostream &out, int indent_level=0) const
Writes the DoubleBitMask out as a binary or a hex number, according to the number of bits.
void store(WordType value, int low_bit, int size)
Stores the indicated word into the indicated range of bits with this DoubleBitMask.
bool has_any_of(int low_bit, int size) const
Returns true if any bit in the indicated range is set, false otherwise.
void clear_bit(int index)
Sets the nth bit off.
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...
static DoubleBitMask< BMType > lower_on(int on_bits)
Returns a DoubleBitMask whose lower on_bits bits are on.
Definition: doubleBitMask.I:46
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
Definition: doubleBitMask.I:99
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
void output(std::ostream &out) const
Writes the DoubleBitMask out as a binary or a hex number, according to the number of bits.
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
WordType extract(int low_bit, int size) const
Returns a word that represents only the indicated range of bits within this DoubleBitMask,...
void clear()
Sets all the bits in the DoubleBitMask off.
void set_bit_to(int index, bool value)
Sets the nth bit either on or off, according to the indicated bool value.
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the mask.
void set_bit(int index)
Sets the nth bit on.
static DoubleBitMask< BMType > all_on()
Returns a DoubleBitMask whose bits are all on.
Definition: doubleBitMask.I:22
bool is_zero() const
Returns true if the entire doubleBitMask is zero, false otherwise.
void output_hex(std::ostream &out, int spaces_every=4) const
Writes the DoubleBitMask out as a hexadecimal number, with spaces every four digits.
static DoubleBitMask< BMType > bit(int index)
Returns a DoubleBitMask with only the indicated bit on.
Definition: doubleBitMask.I:67
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.
Definition: typeHandle.h:81
int compare_to(const DoubleBitMask< BMType > &other) const
Returns a number less than zero if this DoubleBitMask sorts before the indicated other DoubleBitMask,...
void invert_in_place()
Inverts all the bits in the DoubleBitMask.
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the mask.
bool has_bits_in_common(const DoubleBitMask< BMType > &other) const
Returns true if this DoubleBitMask has any "one" bits in common with the other one,...
static DoubleBitMask< BMType > all_off()
Returns a DoubleBitMask whose bits are all off.
Definition: doubleBitMask.I:34
bool has_all_of(int low_bit, int size) const
Returns true if all bits in the indicated range are set, false otherwise.