Panda3D
 All Classes Functions Variables Enumerations
bitArray.h
1 // Filename: bitArray.h
2 // Created by: drose (20Jan06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef BITARRAY_H
16 #define BITARRAY_H
17 
18 #include "pandabase.h"
19 
20 #include "bitMask.h"
21 #include "numeric_types.h"
22 #include "typedObject.h"
23 #include "indent.h"
24 #include "pointerToArray.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 // Class : BitArray
36 // Description : A dynamic array with an unlimited number of bits.
37 //
38 // This is similar to a BitMask, except it appears to
39 // contain an infinite number of bits. You can use it
40 // very much as you would use a BitMask.
41 ////////////////////////////////////////////////////////////////////
42 class EXPCL_PANDA_PUTIL BitArray {
43 public:
44  typedef BitMaskNative MaskType;
45  typedef MaskType::WordType WordType;
46 
47 PUBLISHED:
48  enum { num_bits_per_word = MaskType::num_bits };
49 
50  INLINE BitArray();
51  INLINE BitArray(WordType init_value);
52  INLINE BitArray(const BitArray &copy);
53  INLINE BitArray &operator = (const BitArray &copy);
54  BitArray(const SparseArray &from);
55 
56  INLINE static BitArray all_on();
57  INLINE static BitArray all_off();
58  INLINE static BitArray lower_on(int on_bits);
59  INLINE static BitArray bit(int index);
60  INLINE static BitArray range(int low_bit, int size);
61 
62  INLINE ~BitArray();
63 
64  CONSTEXPR static bool has_max_num_bits();
65  CONSTEXPR static int get_max_num_bits();
66 
67  CONSTEXPR static int get_num_bits_per_word();
68  INLINE int get_num_bits() const;
69  INLINE bool get_bit(int index) const;
70  INLINE void set_bit(int index);
71  INLINE void clear_bit(int index);
72  INLINE void set_bit_to(int index, bool value);
73  INLINE bool get_highest_bits() const;
74  bool is_zero() const;
75  bool is_all_on() const;
76 
77  INLINE WordType extract(int low_bit, int size) const;
78  INLINE void store(WordType value, int low_bit, int size);
79  bool has_any_of(int low_bit, int size) const;
80  bool has_all_of(int low_bit, int size) const;
81  void set_range(int low_bit, int size);
82  void clear_range(int low_bit, int size);
83  INLINE void set_range_to(bool value, int low_bit, int size);
84 
85  int get_num_on_bits() const;
86  int get_num_off_bits() const;
87  int get_lowest_on_bit() const;
88  int get_lowest_off_bit() const;
89  int get_highest_on_bit() const;
90  int get_highest_off_bit() const;
91  int get_next_higher_different_bit(int low_bit) const;
92 
93  INLINE int get_num_words() const;
94  INLINE MaskType get_word(int n) const;
95  INLINE void set_word(int n, WordType value);
96 
97  void invert_in_place();
98  bool has_bits_in_common(const BitArray &other) const;
99  INLINE void clear();
100 
101  void output(ostream &out) const;
102  void output_binary(ostream &out, int spaces_every = 4) const;
103  void output_hex(ostream &out, int spaces_every = 4) const;
104  void write(ostream &out, int indent_level = 0) const;
105 
106  INLINE bool operator == (const BitArray &other) const;
107  INLINE bool operator != (const BitArray &other) const;
108  INLINE bool operator < (const BitArray &other) const;
109  int compare_to(const BitArray &other) const;
110 
111  INLINE BitArray
112  operator & (const BitArray &other) const;
113 
114  INLINE BitArray
115  operator | (const BitArray &other) const;
116 
117  INLINE BitArray
118  operator ^ (const BitArray &other) const;
119 
120  INLINE BitArray
121  operator ~ () const;
122 
123  INLINE BitArray
124  operator << (int shift) const;
125 
126  INLINE BitArray
127  operator >> (int shift) const;
128 
129  void operator &= (const BitArray &other);
130  void operator |= (const BitArray &other);
131  void operator ^= (const BitArray &other);
132  void operator <<= (int shift);
133  void operator >>= (int shift);
134 
135 public:
136  void generate_hash(ChecksumHashGenerator &hashgen) const;
137 
138 private:
139  INLINE void copy_on_write();
140  void ensure_has_word(int n);
141  void normalize();
142 
143 private:
144  typedef PTA(MaskType) Array;
145  Array _array;
146  int _highest_bits; // Either 0 or 1.
147 
148 public:
149  void write_datagram(BamWriter *manager, Datagram &dg) const;
150  void read_datagram(DatagramIterator &scan, BamReader *manager);
151 
152 public:
153  static TypeHandle get_class_type() {
154  return _type_handle;
155  }
156  static void init_type() {
157  register_type(_type_handle, "BitArray");
158  }
159 
160 private:
161  static TypeHandle _type_handle;
162 };
163 
164 #include "bitArray.I"
165 
166 INLINE ostream &
167 operator << (ostream &out, const BitArray &array) {
168  array.output(out);
169  return out;
170 }
171 
172 #endif
This class records a set of integers, where each integer is either present or not present in the set...
Definition: sparseArray.h:49
This is a specific kind of HashGenerator that simply adds up all of the ints.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
A dynamic array with an unlimited number of bits.
Definition: bitArray.h:42
void output(ostream &out) const
Writes the BitArray out as a hex number.
Definition: bitArray.cxx:563
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
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:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43