Panda3D

Loading...
Searching...
No Matches
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
36PUBLISHED:
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
91 INLINE BitMask<WType, nbits>
92 operator & (const BitMask<WType, nbits> &other) const;
93
94 INLINE BitMask<WType, nbits>
95 operator | (const BitMask<WType, nbits> &other) const;
96
97 INLINE BitMask<WType, nbits>
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;
119 INLINE BitMask<WType, nbits> keep_next_highest_bit() const;
120 INLINE BitMask<WType, nbits> keep_next_lowest_bit() 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;
123 INLINE BitMask<WType, nbits> keep_next_highest_bit(const BitMask<WType, nbits> &other) const;
124 INLINE BitMask<WType, nbits> keep_next_lowest_bit(const BitMask<WType, nbits> &other) 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>
150INLINE 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>
160EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK16_DEF);
161EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK32_DEF);
162EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK64_DEF);
163
164 typedef BitMask<uint16_t, 16> BitMask16;
165 typedef BitMask<uint32_t, 32> BitMask32;
166 typedef BitMask<uint64_t, 64> BitMask64;
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
bitMask.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
checksumHashGenerator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BitMask
A general bitmask class.
Definition bitMask.h:32
BitMask::flood_up_in_place
void flood_up_in_place()
Floods this bitmask's bits upwards.
Definition bitMask.I:658
BitMask::keep_next_highest_bit
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
BitMask::get_lowest_on_bit
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the mask.
Definition bitMask.I:283
BitMask::has_all_of
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
BitMask::set_range
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
Definition bitMask.I:213
BitMask::bit
static BitMask< WType, nbits > bit(int index)
Returns a BitMask with only the indicated bit on.
Definition bitMask.I:70
BitMask::set_bit_to
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
BitMask::get_highest_off_bit
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the mask.
Definition bitMask.I:313
BitMask::set_bit
void set_bit(int index)
Sets the nth bit on.
Definition bitMask.I:119
BitMask::flood_bits_up
BitMask< WType, nbits > flood_bits_up() const
Returns a BitMask with the bits flooded upwards.
Definition bitMask.I:676
BitMask::get_highest_on_bit
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the mask.
Definition bitMask.I:303
BitMask::all_on
static BitMask< WType, nbits > all_on()
Returns a BitMask whose bits are all on.
Definition bitMask.I:32
BitMask::has_bits_in_common
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
BitMask::store
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
BitMask::is_all_on
bool is_all_on() const
Returns true if the entire bitmask is one, false otherwise.
Definition bitMask.I:162
BitMask::all_off
static BitMask< WType, nbits > all_off()
Returns a BitMask whose bits are all off.
Definition bitMask.I:43
BitMask::output_binary
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
BitMask::get_bit
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
Definition bitMask.I:109
BitMask::set_range_to
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
BitMask::keep_next_lowest_bit
BitMask< WType, nbits > keep_next_lowest_bit(int index) const
Returns a BitMask with only the next lower bit below the indicated bit on, or all_off.
Definition bitMask.I:752
BitMask::clear_bit
void clear_bit(int index)
Sets the nth bit off.
Definition bitMask.I:129
BitMask::get_lowest_off_bit
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the mask.
Definition bitMask.I:293
BitMask::generate_hash
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the bitmask to the indicated hash generator.
Definition bitMask.I:640
BitMask::keep_next_highest_bit
BitMask< WType, nbits > keep_next_highest_bit(int index) const
Returns a BitMask with only the next highest bit above the indicated bit on, or all.
Definition bitMask.I:728
BitMask::output_hex
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
BitMask::clear
void clear()
Sets all the bits in the BitMask off.
Definition bitMask.I:395
BitMask::range
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
BitMask::keep_next_lowest_bit
BitMask< WType, nbits > keep_next_lowest_bit(const BitMask< WType, nbits > &other) const
Returns a BitMask with only the next lowest "on" bit below all "on" bits in the passed in bitmask,...
Definition bitMask.I:793
BitMask::get_num_on_bits
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the mask.
Definition bitMask.I:264
BitMask::keep_next_lowest_bit
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
BitMask::get_key
int get_key() const
Returns a mostly unique integer key per unique bitmask, suitable for using in a hash table.
Definition bitMask.I:622
BitMask::write
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
BitMask::invert_in_place
void invert_in_place()
Inverts all the bits in the BitMask.
Definition bitMask.I:372
BitMask::__nonzero__
bool __nonzero__() const
Returns true if the bitmask is not zero.
Definition bitMask.I:631
BitMask::lower_on
static BitMask< WType, nbits > lower_on(int on_bits)
Returns a BitMask whose lower on_bits bits are on.
Definition bitMask.I:54
BitMask::clear_range
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
Definition bitMask.I:223
BitMask::get_num_bits
constexpr int get_num_bits() const
Returns the number of bits available to set in the bitmask.
Definition bitMask.I:99
BitMask::set_word
void set_word(WordType value)
Sets the entire BitMask to the value indicated by the given word.
Definition bitMask.I:255
BitMask::compare_to
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
BitMask::has_any_of
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
BitMask::output
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
BitMask::get_num_off_bits
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the mask.
Definition bitMask.I:273
BitMask::keep_next_highest_bit
BitMask< WType, nbits > keep_next_highest_bit(const BitMask< WType, nbits > &other) const
Returns a BitMask with only the next highest "on" bit above all "on" bits in the passed in bitmask,...
Definition bitMask.I:777
BitMask::is_zero
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
Definition bitMask.I:153
BitMask::get_word
WordType get_word() const
Returns the entire BitMask as a single word.
Definition bitMask.I:246
BitMask::flood_bits_down
BitMask< WType, nbits > flood_bits_down() const
Returns a BitMask with the bits flooded down.
Definition bitMask.I:686
BitMask::get_next_higher_different_bit
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
BitMask::operator<
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
BitMask::extract
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
BitMask::flood_down_in_place
void flood_down_in_place()
Floods this bitmask's bits downwards.
Definition bitMask.I:667
ChecksumHashGenerator
This is a specific kind of HashGenerator that simply adds up all of the ints.
Definition checksumHashGenerator.h:25
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
indent.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
numeric_types.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pbitops.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pnotify.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
typedObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.