Panda3D
doubleBitMask.h
1 // Filename: doubleBitMask.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 DOUBLEBITMASK_H
16 #define DOUBLEBITMASK_H
17 
18 #include "pandabase.h"
19 
20 #include "bitMask.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Class : DoubleBitMask
24 // Description : This is a special BitMask type that is implemented as
25 // a pair of lesser BitMask types, to present a
26 // double-wide bit mask. For instance, on a 32-bit
27 // system, this can be used to make a single 64-bit bit
28 // mask. More of these can be ganged up together to
29 // make a 128-bit mask, and so on.
30 ////////////////////////////////////////////////////////////////////
31 template<class BMType>
33 public:
34  typedef TYPENAME BMType::WordType WordType;
35 
36 PUBLISHED:
37  typedef BMType BitMaskType;
38 
39  enum {
40  half_bits = BitMaskType::num_bits,
41  num_bits = BitMaskType::num_bits * 2,
42  };
43 
44  INLINE DoubleBitMask();
45  INLINE DoubleBitMask(const DoubleBitMask<BMType> &copy);
46  INLINE DoubleBitMask<BMType> &operator = (const DoubleBitMask<BMType> &copy);
47 
48  INLINE static DoubleBitMask<BMType> all_on();
49  INLINE static DoubleBitMask<BMType> all_off();
50  INLINE static DoubleBitMask<BMType> lower_on(int on_bits);
51  INLINE static DoubleBitMask<BMType> bit(int index);
52  INLINE static DoubleBitMask<BMType> range(int low_bit, int size);
53 
54  INLINE ~DoubleBitMask();
55 
56  CONSTEXPR static bool has_max_num_bits();
57  CONSTEXPR static int get_max_num_bits();
58 
59  CONSTEXPR static int get_num_bits();
60  INLINE bool get_bit(int index) const;
61  INLINE void set_bit(int index);
62  INLINE void clear_bit(int index);
63  INLINE void set_bit_to(int index, bool value);
64  INLINE bool is_zero() const;
65  INLINE bool is_all_on() const;
66 
67  INLINE WordType extract(int low_bit, int size) const;
68  INLINE void store(WordType value, int low_bit, int size);
69  INLINE bool has_any_of(int low_bit, int size) const;
70  INLINE bool has_all_of(int low_bit, int size) const;
71  INLINE void set_range(int low_bit, int size);
72  INLINE void clear_range(int low_bit, int size);
73  INLINE void set_range_to(bool value, int low_bit, int size);
74 
75  INLINE int get_num_on_bits() const;
76  INLINE int get_num_off_bits() const;
77  INLINE int get_lowest_on_bit() const;
78  INLINE int get_lowest_off_bit() const;
79  INLINE int get_highest_on_bit() const;
80  INLINE int get_highest_off_bit() const;
81  INLINE int get_next_higher_different_bit(int low_bit) const;
82 
83  INLINE void invert_in_place();
84  INLINE bool has_bits_in_common(const DoubleBitMask<BMType> &other) const;
85  INLINE void clear();
86 
87  void output(ostream &out) const;
88  void output_binary(ostream &out, int spaces_every = 4) const;
89  void output_hex(ostream &out, int spaces_every = 4) const;
90  void write(ostream &out, int indent_level = 0) const;
91 
92  INLINE bool operator == (const DoubleBitMask<BMType> &other) const;
93  INLINE bool operator != (const DoubleBitMask<BMType> &other) const;
94  INLINE bool operator < (const DoubleBitMask<BMType> &other) const;
95  INLINE int compare_to(const DoubleBitMask<BMType> &other) const;
96 
98  operator & (const DoubleBitMask<BMType> &other) const;
99 
100  INLINE DoubleBitMask<BMType>
101  operator | (const DoubleBitMask<BMType> &other) const;
102 
103  INLINE DoubleBitMask<BMType>
104  operator ^ (const DoubleBitMask<BMType> &other) const;
105 
106  INLINE DoubleBitMask<BMType>
107  operator ~ () const;
108 
109  INLINE DoubleBitMask<BMType>
110  operator << (int shift) const;
111 
112  INLINE DoubleBitMask<BMType>
113  operator >> (int shift) const;
114 
115  INLINE void operator &= (const DoubleBitMask<BMType> &other);
116  INLINE void operator |= (const DoubleBitMask<BMType> &other);
117  INLINE void operator ^= (const DoubleBitMask<BMType> &other);
118  INLINE void operator <<= (int shift);
119  INLINE void operator >>= (int shift);
120 
121 public:
122  INLINE void generate_hash(ChecksumHashGenerator &hashgen) const;
123 
124 private:
125  BitMaskType _lo, _hi;
126 
127 public:
128  static TypeHandle get_class_type() {
129  return _type_handle;
130  }
131  static void init_type();
132 
133 private:
134  static TypeHandle _type_handle;
135 };
136 
137 #include "doubleBitMask.I"
138 
139 template<class BMType>
140 INLINE ostream &operator << (ostream &out, const DoubleBitMask<BMType> &doubleBitMask) {
141  doubleBitMask.output(out);
142  return out;
143 }
144 
145 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, DoubleBitMask<BitMaskNative>);
147 
148 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, DoubleBitMask<DoubleBitMaskNative>);
150 
151 // Tell GCC that we'll take care of the instantiation explicitly here.
152 #ifdef __GNUC__
153 #pragma interface
154 #endif
155 
156 #endif
bool is_all_on() const
Returns true if the entire doubleBitMask is one, false otherwise.
void write(ostream &out, int indent_level=0) const
Writes the DoubleBitMask out as a binary or a hex number, according to the number of 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.
void output(ostream &out) const
Writes the DoubleBitMask out as a binary or a hex number, according to the number of bits...
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
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.
This is a special BitMask type that is implemented as a pair of lesser BitMask types, to present a double-wide bit mask.
Definition: doubleBitMask.h:32
void output_hex(ostream &out, int spaces_every=4) const
Writes the DoubleBitMask out as a hexadecimal number, with spaces every four digits.
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:89
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
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 output_binary(ostream &out, int spaces_every=4) const
Writes the DoubleBitMask out as a binary number, with spaces every four 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, shifted to the least-significant position.
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.
static CONSTEXPR int get_num_bits()
Returns the number of bits available to set in the doubleBitMask.
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:61
bool is_zero() const
Returns true if the entire doubleBitMask is zero, false otherwise.
static DoubleBitMask< BMType > bit(int index)
Returns a DoubleBitMask with only the indicated bit on.
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:85
int compare_to(const DoubleBitMask< BMType > &other) const
Returns a number less than zero if this DoubleBitMask sorts before the indicated other DoubleBitMask...
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.
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, false otherwise.
static DoubleBitMask< BMType > all_off()
Returns a DoubleBitMask whose bits are all off.
Definition: doubleBitMask.I:75
bool has_all_of(int low_bit, int size) const
Returns true if all bits in the indicated range are set, false otherwise.