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
26#include "checksumHashGenerator.h"
27
28 class SparseArray;
29 class BamWriter;
30 class BamReader;
31 class Datagram;
32 class DatagramIterator;
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 */
40 class EXPCL_PANDA_PUTIL BitArray {
41 public:
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
132 public:
133 void generate_hash(ChecksumHashGenerator &hashgen) const;
134
135 private:
136 INLINE void copy_on_write();
137 void ensure_has_word(int n);
138 void normalize();
139
140 private:
141 typedef PTA(MaskType) Array;
142 Array _array;
143 int _highest_bits; // Either 0 or 1.
144
145 friend class Extension<BitArray>;
146
147 public:
148 void write_datagram(BamWriter *manager, Datagram &dg) const;
149 void read_datagram(DatagramIterator &scan, BamReader *manager);
150
151 public:
152 static TypeHandle get_class_type() {
153 return _type_handle;
154 }
155 static void init_type() {
156 register_type(_type_handle, "BitArray");
157 }
158
159 private:
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
bitArray.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bitMask.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
checksumHashGenerator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
BamWriter
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
BitArray
A dynamic array with an unlimited number of bits.
Definition bitArray.h:40
BitArray::get_highest_bits
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
BitArray::clear_bit
void clear_bit(int index)
Sets the nth bit off.
Definition bitArray.I:133
BitArray::output_hex
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
BitArray::get_num_on_bits
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the array.
Definition bitArray.cxx:296
BitArray::get_next_higher_different_bit
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
BitArray::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 bitArray.I:150
BitArray::set_word
void set_word(size_t n, WordType value)
Replaces the nth word in the array.
Definition bitArray.I:262
BitArray::get_word
MaskType get_word(size_t n) const
Returns the nth word in the array.
Definition bitArray.I:245
BitArray::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 bitArray.cxx:89
BitArray::compare_to
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
BitArray::all_off
static BitArray all_off()
Returns a BitArray whose bits are all off.
Definition bitArray.I:47
BitArray::get_highest_off_bit
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the array.
Definition bitArray.cxx:391
BitArray::generate_hash
void generate_hash(ChecksumHashGenerator &hashgen) const
Adds the bitmask to the indicated hash generator.
Definition bitArray.cxx:855
BitArray::invert_in_place
void invert_in_place()
Inverts all the bits in the BitArray.
Definition bitArray.cxx:451
BitArray::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 bitArray.cxx:142
BitArray::get_lowest_off_bit
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the array.
Definition bitArray.cxx:352
BitArray::read_datagram
void read_datagram(DatagramIterator &scan, BamReader *manager)
Reads the object that was previously written to a Bam file.
Definition bitArray.cxx:928
BitArray::get_num_bits
size_t get_num_bits() const
Returns the current number of possibly different bits in this array.
Definition bitArray.I:89
BitArray::lower_on
static BitArray lower_on(int on_bits)
Returns a BitArray whose lower on_bits bits are on.
Definition bitArray.I:55
BitArray::output
void output(std::ostream &out) const
Writes the BitArray out as a hex number.
Definition bitArray.cxx:520
BitArray::set_range
void set_range(int low_bit, int size)
Sets the indicated range of bits on.
Definition bitArray.cxx:195
BitArray::clear
void clear()
Sets all the bits in the BitArray off.
Definition bitArray.I:272
BitArray::has_bits_in_common
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
BitArray::range
static BitArray range(int low_bit, int size)
Returns a BitArray whose size bits, beginning at low_bit, are on.
Definition bitArray.I:75
BitArray::set_bit
void set_bit(int index)
Sets the nth bit on.
Definition bitArray.I:115
BitArray::all_on
static BitArray all_on()
Returns a BitArray with an infinite array of bits, all on.
Definition bitArray.I:37
BitArray::extract
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
BitArray::write_datagram
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
BitArray::get_num_off_bits
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the array.
Definition bitArray.cxx:314
BitArray::store
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
BitArray::output_binary
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
BitArray::is_zero
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
Definition bitArray.cxx:48
BitArray::get_bit
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
Definition bitArray.I:99
BitArray::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 bitArray.I:223
BitArray::bit
static BitArray bit(int index)
Returns a BitArray with only the indicated bit on.
Definition bitArray.I:65
BitArray::write
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
BitArray::clear_range
void clear_range(int low_bit, int size)
Sets the indicated range of bits off.
Definition bitArray.cxx:245
BitArray::get_num_words
size_t get_num_words() const
Returns the number of possibly-unique words stored in the array.
Definition bitArray.I:235
BitArray::is_all_on
bool is_all_on() const
Returns true if the entire bitmask is one, false otherwise.
Definition bitArray.cxx:69
ChecksumHashGenerator
This is a specific kind of HashGenerator that simply adds up all of the ints.
Definition checksumHashGenerator.h:25
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition datagramIterator.h:30
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
Extension
The default class template does not define any methods.
Definition extension.h:34
SparseArray
This class records a set of integers, where each integer is either present or not present in the set.
Definition sparseArray.h:43
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
array
STL class.
extension.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
get_highest_on_bit
int get_highest_on_bit(unsigned short x)
Returns the index of the highest 1 bit in the word.
Definition pbitops.I:259
get_lowest_on_bit
int get_lowest_on_bit(unsigned short x)
Returns the index of the lowest 1 bit in the word.
Definition pbitops.I:175
pointerToArray.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition register_type.I:22
typedObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.