Panda3D

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
45 PUBLISHED:
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
165 INLINE std::ostream &
166 operator << (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::output
void output(std::ostream &out) const
Writes the BitArray out as a hex number.
Definition: bitArray.cxx:520
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:27
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
compare_to
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
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.