Panda3D
Loading...
Searching...
No Matches
bitArray.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 bitArray.h
10 * @author drose
11 * @date 2006-01-20
12 */
13
14#ifndef BITARRAY_H
15#define BITARRAY_H
16
17#include "pandabase.h"
18
19#include "bitMask.h"
20#include "numeric_types.h"
21#include "typedObject.h"
22#include "indent.h"
23#include "pointerToArray.h"
24#include "extension.h"
25
27
28class SparseArray;
29class BamWriter;
30class BamReader;
31class Datagram;
33
34/**
35 * A dynamic array with an unlimited number of bits.
36 *
37 * This is similar to a BitMask, except it appears to contain an infinite
38 * number of bits. You can use it very much as you would use a BitMask.
39 */
40class EXPCL_PANDA_PUTIL BitArray {
41public:
42 typedef BitMaskNative MaskType;
43 typedef MaskType::WordType WordType;
44
45PUBLISHED:
46 enum { num_bits_per_word = MaskType::num_bits };
47
48 INLINE BitArray();
49 INLINE BitArray(WordType init_value);
50 BitArray(const SparseArray &from);
51
52 INLINE static BitArray all_on();
53 INLINE static BitArray all_off();
54 INLINE static BitArray lower_on(int on_bits);
55 INLINE static BitArray bit(int index);
56 INLINE static BitArray range(int low_bit, int size);
57
58 constexpr static bool has_max_num_bits() { return false; }
59 constexpr static int get_max_num_bits() { return INT_MAX; }
60
61 constexpr static int get_num_bits_per_word() { return num_bits_per_word; }
62 INLINE size_t get_num_bits() const;
63 INLINE bool get_bit(int index) const;
64 INLINE void set_bit(int index);
65 INLINE void clear_bit(int index);
66 INLINE void set_bit_to(int index, bool value);
67 INLINE bool get_highest_bits() const;
68 bool is_zero() const;
69 bool is_all_on() const;
70
71 INLINE WordType extract(int low_bit, int size) const;
72 INLINE void store(WordType value, int low_bit, int size);
73 bool has_any_of(int low_bit, int size) const;
74 bool has_all_of(int low_bit, int size) const;
75 void set_range(int low_bit, int size);
76 void clear_range(int low_bit, int size);
77 INLINE void set_range_to(bool value, int low_bit, int size);
78
79 int get_num_on_bits() const;
80 int get_num_off_bits() const;
81 int get_lowest_on_bit() const;
82 int get_lowest_off_bit() const;
83 int get_highest_on_bit() const;
84 int get_highest_off_bit() const;
85 int get_next_higher_different_bit(int low_bit) const;
86
87 INLINE size_t get_num_words() const;
88 INLINE MaskType get_word(size_t n) const;
89 INLINE void set_word(size_t n, WordType value);
90
91 void invert_in_place();
92 bool has_bits_in_common(const BitArray &other) const;
93 INLINE void clear();
94
95 void output(std::ostream &out) const;
96 void output_binary(std::ostream &out, int spaces_every = 4) const;
97 void output_hex(std::ostream &out, int spaces_every = 4) const;
98 void write(std::ostream &out, int indent_level = 0) const;
99
100 INLINE bool operator == (const BitArray &other) const;
101 INLINE bool operator != (const BitArray &other) const;
102 INLINE bool operator < (const BitArray &other) const;
103 int compare_to(const BitArray &other) const;
104
105 INLINE BitArray
106 operator & (const BitArray &other) const;
107
108 INLINE BitArray
109 operator | (const BitArray &other) const;
110
111 INLINE BitArray
112 operator ^ (const BitArray &other) const;
113
114 INLINE BitArray
115 operator ~ () const;
116
117 INLINE BitArray
118 operator << (int shift) const;
119
120 INLINE BitArray
121 operator >> (int shift) const;
122
123 void operator &= (const BitArray &other);
124 void operator |= (const BitArray &other);
125 void operator ^= (const BitArray &other);
126 void operator <<= (int shift);
127 void operator >>= (int shift);
128
129 EXTENSION(PyObject *__getstate__() const);
130 EXTENSION(void __setstate__(PyObject *state));
131
132public:
133 void generate_hash(ChecksumHashGenerator &hashgen) const;
134
135private:
136 INLINE void copy_on_write();
137 void ensure_has_word(int n);
138 void normalize();
139
140private:
141 typedef PTA(MaskType) Array;
142 Array _array;
143 int _highest_bits; // Either 0 or 1.
144
145 friend class Extension<BitArray>;
146
147public:
148 void write_datagram(BamWriter *manager, Datagram &dg) const;
149 void read_datagram(DatagramIterator &scan, BamReader *manager);
150
151public:
152 static TypeHandle get_class_type() {
153 return _type_handle;
154 }
155 static void init_type() {
156 register_type(_type_handle, "BitArray");
157 }
158
159private:
160 static TypeHandle _type_handle;
161};
162
163#include "bitArray.I"
164
165INLINE std::ostream &
166operator << (std::ostream &out, const BitArray &array) {
167 array.output(out);
168 return out;
169}
170
171#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
A dynamic array with an unlimited number of bits.
Definition bitArray.h:40
bool get_highest_bits() const
Returns true if the infinite set of bits beyond get_num_bits() are all on, or false of they are all o...
Definition bitArray.I:163
void clear_bit(int index)
Sets the nth bit off.
Definition bitArray.I:133
void output_hex(std::ostream &out, int spaces_every=4) const
Writes the BitArray out as a hexadecimal number, with spaces every four digits.
Definition bitArray.cxx:546
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the array.
Definition bitArray.cxx:296
int get_next_higher_different_bit(int low_bit) const
Returns the index of the next bit in the array, above low_bit, whose value is different that the valu...
Definition bitArray.cxx:413
void set_bit_to(int index, bool value)
Sets the nth bit either on or off, according to the indicated bool value.
Definition bitArray.I:150
void set_word(size_t n, WordType value)
Replaces the nth word in the array.
Definition bitArray.I:262
MaskType get_word(size_t n) const
Returns the nth word in the array.
Definition bitArray.I:245
bool has_any_of(int low_bit, int size) const
Returns true if any bit in the indicated range is set, false otherwise.
Definition bitArray.cxx:89
int compare_to(const BitArray &other) const
Returns a number less than zero if this BitArray sorts before the indicated other BitArray,...
Definition bitArray.cxx:582
static BitArray all_off()
Returns a BitArray whose bits are all off.
Definition bitArray.I:47
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the array.
Definition bitArray.cxx:391
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the bitmask to the indicated hash generator.
Definition bitArray.cxx:855
void invert_in_place()
Inverts all the bits in the BitArray.
Definition bitArray.cxx:451
bool has_all_of(int low_bit, int size) const
Returns true if all bits in the indicated range are set, false otherwise.
Definition bitArray.cxx:142
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the array.
Definition bitArray.cxx:352
void read_datagram(DatagramIterator &scan, BamReader *manager)
Reads the object that was previously written to a Bam file.
Definition bitArray.cxx:928
size_t get_num_bits() const
Returns the current number of possibly different bits in this array.
Definition bitArray.I:89
static BitArray lower_on(int on_bits)
Returns a BitArray whose lower on_bits bits are on.
Definition bitArray.I:55
void output(std::ostream &out) const
Writes the BitArray out as a hex number.
Definition bitArray.cxx:520
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
Definition bitArray.cxx:195
void clear()
Sets all the bits in the BitArray off.
Definition bitArray.I:272
bool has_bits_in_common(const BitArray &other) const
Returns true if this BitArray has any "one" bits in common with the other one, false otherwise.
Definition bitArray.cxx:467
static BitArray range(int low_bit, int size)
Returns a BitArray whose size bits, beginning at low_bit, are on.
Definition bitArray.I:75
void set_bit(int index)
Sets the nth bit on.
Definition bitArray.I:115
static BitArray all_on()
Returns a BitArray with an infinite array of bits, all on.
Definition bitArray.I:37
WordType extract(int low_bit, int size) const
Returns a word that represents only the indicated range of bits within this BitArray,...
Definition bitArray.I:173
void write_datagram(BamWriter *manager, Datagram &dg) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition bitArray.cxx:911
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the array.
Definition bitArray.cxx:314
void store(WordType value, int low_bit, int size)
Stores the indicated word into the indicated range of bits with this BitArray.
Definition bitArray.I:197
void output_binary(std::ostream &out, int spaces_every=4) const
Writes the BitArray out as a binary number, with spaces every four bits.
Definition bitArray.cxx:528
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
Definition bitArray.cxx:48
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
Definition bitArray.I:99
void set_range_to(bool value, int low_bit, int size)
Sets the indicated range of bits to either on or off.
Definition bitArray.I:223
static BitArray bit(int index)
Returns a BitArray with only the indicated bit on.
Definition bitArray.I:65
void write(std::ostream &out, int indent_level=0) const
Writes the BitArray out as a binary or a hex number, according to the number of bits.
Definition bitArray.cxx:572
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
Definition bitArray.cxx:245
size_t get_num_words() const
Returns the number of possibly-unique words stored in the array.
Definition bitArray.I:235
bool is_all_on() const
Returns true if the entire bitmask is one, false otherwise.
Definition bitArray.cxx:69
This is a specific kind of HashGenerator that simply adds up all of the ints.
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
The default class template does not define any methods.
Definition extension.h:34
This class records a set of integers, where each integer is either present or not present in the set.
Definition sparseArray.h:43
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
STL class.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_highest_on_bit(unsigned short x)
Returns the index of the highest 1 bit in the word.
Definition pbitops.I:259
int get_lowest_on_bit(unsigned short x)
Returns the index of the lowest 1 bit in the word.
Definition pbitops.I:175
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.