Panda3D
Loading...
Searching...
No Matches
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#include "extension.h"
21
22/**
23 * This is a special BitMask type that is implemented as a pair of lesser
24 * BitMask types, to present a double-wide bit mask. For instance, on a
25 * 32-bit system, this can be used to make a single 64-bit bit mask. More of
26 * these can be ganged up together to make a 128-bit mask, and so on.
27 */
28template<class BMType>
30public:
31 typedef typename BMType::WordType WordType;
32
33PUBLISHED:
34 typedef BMType BitMaskType;
35
36 enum {
37 half_bits = BMType::num_bits,
38 num_bits = BMType::num_bits * 2,
39 };
40
41 constexpr DoubleBitMask() = default;
42 EXTENSION(DoubleBitMask(PyObject *init_value));
43
44 INLINE static DoubleBitMask<BMType> all_on();
45 INLINE static DoubleBitMask<BMType> all_off();
46 INLINE static DoubleBitMask<BMType> lower_on(int on_bits);
47 INLINE static DoubleBitMask<BMType> bit(int index);
48 INLINE static DoubleBitMask<BMType> range(int low_bit, int size);
49
50 constexpr static bool has_max_num_bits() {return true;}
51 constexpr static int get_max_num_bits() {return num_bits;}
52
53 constexpr int get_num_bits() const;
54 INLINE bool get_bit(int index) const;
55 INLINE void set_bit(int index);
56 INLINE void clear_bit(int index);
57 INLINE void set_bit_to(int index, bool value);
58 INLINE bool is_zero() const;
59 INLINE bool is_all_on() const;
60
61 INLINE WordType extract(int low_bit, int size) const;
62 INLINE void store(WordType value, int low_bit, int size);
63 INLINE bool has_any_of(int low_bit, int size) const;
64 INLINE bool has_all_of(int low_bit, int size) const;
65 INLINE void set_range(int low_bit, int size);
66 INLINE void clear_range(int low_bit, int size);
67 INLINE void set_range_to(bool value, int low_bit, int size);
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 DoubleBitMask<BMType> &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 DoubleBitMask<BMType> &other) const;
87 INLINE bool operator != (const DoubleBitMask<BMType> &other) const;
88 INLINE bool operator < (const DoubleBitMask<BMType> &other) const;
89 INLINE int compare_to(const DoubleBitMask<BMType> &other) const;
90
92 operator & (const DoubleBitMask<BMType> &other) const;
93
95 operator | (const DoubleBitMask<BMType> &other) const;
96
98 operator ^ (const DoubleBitMask<BMType> &other) const;
99
101 operator ~ () const;
102
104 operator << (int shift) const;
105
107 operator >> (int shift) const;
108
109 INLINE void operator &= (const DoubleBitMask<BMType> &other);
110 INLINE void operator |= (const DoubleBitMask<BMType> &other);
111 INLINE void operator ^= (const DoubleBitMask<BMType> &other);
112 INLINE void operator <<= (int shift);
113 INLINE void operator >>= (int shift);
114
115 EXTENSION(PyObject *__reduce__(PyObject *self) const);
116
117public:
118 INLINE void generate_hash(ChecksumHashGenerator &hashgen) const;
119
120private:
121 BitMaskType _lo, _hi;
122
123 friend class Extension<DoubleBitMask>;
124
125public:
126 static TypeHandle get_class_type() {
127 return _type_handle;
128 }
129 static void init_type();
130
131private:
132 static TypeHandle _type_handle;
133};
134
135#include "doubleBitMask.I"
136
137template<class BMType>
138INLINE std::ostream &operator << (std::ostream &out, const DoubleBitMask<BMType> &doubleBitMask) {
139 doubleBitMask.output(out);
140 return out;
141}
142
143EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, DoubleBitMask<BitMaskNative>);
145
146EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, DoubleBitMask<DoubleBitMaskNative>);
148
149#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a specific kind of HashGenerator that simply adds up all of the ints.
This is a special BitMask type that is implemented as a pair of lesser BitMask types,...
void clear_bit(int index)
Sets the nth bit off.
void output(std::ostream &out) const
Writes the DoubleBitMask out as a binary or a hex number, according to the number of bits.
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...
bool is_zero() const
Returns true if the entire doubleBitMask is zero, false otherwise.
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the doubleBitMask to the indicated hash generator.
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the mask.
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
int compare_to(const DoubleBitMask< BMType > &other) const
Returns a number less than zero if this DoubleBitMask sorts before the indicated other DoubleBitMask,...
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
void set_bit(int index)
Sets the nth bit on.
constexpr int get_num_bits() const
Returns the number of bits available to set in the doubleBitMask.
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.
void set_bit_to(int index, bool value)
Sets the nth bit either on or off, according to the indicated bool value.
void invert_in_place()
Inverts all the bits in the DoubleBitMask.
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.
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the mask.
static DoubleBitMask< BMType > lower_on(int on_bits)
Returns a DoubleBitMask whose lower on_bits bits are on.
void output_hex(std::ostream &out, int spaces_every=4) const
Writes the DoubleBitMask out as a hexadecimal number, with spaces every four digits.
bool operator<(const DoubleBitMask< BMType > &other) const
The ordering operator is of limited usefulness with a DoubleBitMask, however, it has a definition whi...
void clear()
Sets all the bits in the DoubleBitMask off.
bool has_all_of(int low_bit, int size) const
Returns true if all bits in the indicated range are set, false otherwise.
static DoubleBitMask< BMType > range(int low_bit, int size)
Returns a DoubleBitMask whose size bits, beginning at low_bit, are on.
static DoubleBitMask< BMType > all_on()
Returns a DoubleBitMask whose bits are all on.
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
void store(WordType value, int low_bit, int size)
Stores the indicated word into the indicated range of bits with this DoubleBitMask.
static DoubleBitMask< BMType > bit(int index)
Returns a DoubleBitMask with only the indicated bit on.
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
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_off_bit() const
Returns the index of the highest 0 bit in the mask.
WordType extract(int low_bit, int size) const
Returns a word that represents only the indicated range of bits within this DoubleBitMask,...
bool is_all_on() const
Returns true if the entire doubleBitMask is one, false otherwise.
bool has_any_of(int low_bit, int size) const
Returns true if any bit in the indicated range is set, 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.
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the mask.
The default class template does not define any methods.
Definition extension.h:34
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.