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 
25 #include "checksumHashGenerator.h"
26 
27 class SparseArray;
28 class BamWriter;
29 class BamReader;
30 class Datagram;
31 class DatagramIterator;
32 
33 /**
34  * A dynamic array with an unlimited number of bits.
35  *
36  * This is similar to a BitMask, except it appears to contain an infinite
37  * number of bits. You can use it very much as you would use a BitMask.
38  */
39 class EXPCL_PANDA_PUTIL BitArray {
40 public:
41  typedef BitMaskNative MaskType;
42  typedef MaskType::WordType WordType;
43 
44 PUBLISHED:
45  enum { num_bits_per_word = MaskType::num_bits };
46 
47  INLINE BitArray();
48  INLINE BitArray(WordType init_value);
49  BitArray(const SparseArray &from);
50 
51  INLINE static BitArray all_on();
52  INLINE static BitArray all_off();
53  INLINE static BitArray lower_on(int on_bits);
54  INLINE static BitArray bit(int index);
55  INLINE static BitArray range(int low_bit, int size);
56 
57  constexpr static bool has_max_num_bits() { return false; }
58  constexpr static int get_max_num_bits() { return INT_MAX; }
59 
60  constexpr static int get_num_bits_per_word() { return num_bits_per_word; }
61  INLINE size_t get_num_bits() const;
62  INLINE bool get_bit(int index) const;
63  INLINE void set_bit(int index);
64  INLINE void clear_bit(int index);
65  INLINE void set_bit_to(int index, bool value);
66  INLINE bool get_highest_bits() const;
67  bool is_zero() const;
68  bool is_all_on() const;
69 
70  INLINE WordType extract(int low_bit, int size) const;
71  INLINE void store(WordType value, int low_bit, int size);
72  bool has_any_of(int low_bit, int size) const;
73  bool has_all_of(int low_bit, int size) const;
74  void set_range(int low_bit, int size);
75  void clear_range(int low_bit, int size);
76  INLINE void set_range_to(bool value, int low_bit, int size);
77 
78  int get_num_on_bits() const;
79  int get_num_off_bits() const;
80  int get_lowest_on_bit() const;
81  int get_lowest_off_bit() const;
82  int get_highest_on_bit() const;
83  int get_highest_off_bit() const;
84  int get_next_higher_different_bit(int low_bit) const;
85 
86  INLINE size_t get_num_words() const;
87  INLINE MaskType get_word(size_t n) const;
88  INLINE void set_word(size_t n, WordType value);
89 
90  void invert_in_place();
91  bool has_bits_in_common(const BitArray &other) const;
92  INLINE void clear();
93 
94  void output(std::ostream &out) const;
95  void output_binary(std::ostream &out, int spaces_every = 4) const;
96  void output_hex(std::ostream &out, int spaces_every = 4) const;
97  void write(std::ostream &out, int indent_level = 0) const;
98 
99  INLINE bool operator == (const BitArray &other) const;
100  INLINE bool operator != (const BitArray &other) const;
101  INLINE bool operator < (const BitArray &other) const;
102  int compare_to(const BitArray &other) const;
103 
104  INLINE BitArray
105  operator & (const BitArray &other) const;
106 
107  INLINE BitArray
108  operator | (const BitArray &other) const;
109 
110  INLINE BitArray
111  operator ^ (const BitArray &other) const;
112 
113  INLINE BitArray
114  operator ~ () const;
115 
116  INLINE BitArray
117  operator << (int shift) const;
118 
119  INLINE BitArray
120  operator >> (int shift) const;
121 
122  void operator &= (const BitArray &other);
123  void operator |= (const BitArray &other);
124  void operator ^= (const BitArray &other);
125  void operator <<= (int shift);
126  void operator >>= (int shift);
127 
128 public:
129  void generate_hash(ChecksumHashGenerator &hashgen) const;
130 
131 private:
132  INLINE void copy_on_write();
133  void ensure_has_word(int n);
134  void normalize();
135 
136 private:
137  typedef PTA(MaskType) Array;
138  Array _array;
139  int _highest_bits; // Either 0 or 1.
140 
141 public:
142  void write_datagram(BamWriter *manager, Datagram &dg) const;
143  void read_datagram(DatagramIterator &scan, BamReader *manager);
144 
145 public:
146  static TypeHandle get_class_type() {
147  return _type_handle;
148  }
149  static void init_type() {
150  register_type(_type_handle, "BitArray");
151  }
152 
153 private:
154  static TypeHandle _type_handle;
155 };
156 
157 #include "bitArray.I"
158 
159 INLINE std::ostream &
160 operator << (std::ostream &out, const BitArray &array) {
161  array.output(out);
162  return out;
163 }
164 
165 #endif
This class records a set of integers, where each integer is either present or not present in the set.
Definition: sparseArray.h:42
This is a specific kind of HashGenerator that simply adds up all of the ints.
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
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
int get_lowest_on_bit(uint16_t x)
Returns the index of the lowest 1 bit in the word.
Definition: pbitops.I:129
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void output(std::ostream &out) const
Writes the BitArray out as a hex number.
Definition: bitArray.cxx:520
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A dynamic array with an unlimited number of bits.
Definition: bitArray.h:39
int get_highest_on_bit(uint16_t x)
Returns the index of the highest 1 bit in the word.
Definition: pbitops.I:192
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.