Panda3D
sparseArray.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 sparseArray.h
10  * @author drose
11  * @date 2007-02-14
12  */
13 
14 #ifndef SPARSEARRAY_H
15 #define SPARSEARRAY_H
16 
17 #include "pandabase.h"
18 #include "ordered_vector.h"
19 
20 class BitArray;
21 class BamWriter;
22 class BamReader;
23 class Datagram;
24 class DatagramIterator;
25 
26 /**
27  * This class records a set of integers, where each integer is either present
28  * or not present in the set.
29  *
30  * It is similar in principle and in interface to a BitArray (which can be
31  * thought of as a set of integers, one integer corresponding to each
32  * different bit position), but the SparseArray is implemented as a list of
33  * min/max subrange lists, rather than as a bitmask.
34  *
35  * This makes it particularly efficient for storing sets which consist of
36  * large sections of consecutively included or consecutively excluded
37  * elements, with arbitrarily large integers, but particularly inefficient for
38  * doing boolean operations such as & or |.
39  *
40  * Also, unlike BitArray, the SparseArray can store negative integers.
41  */
42 class EXPCL_PANDA_PUTIL SparseArray {
43 PUBLISHED:
44  INLINE SparseArray();
45  SparseArray(const BitArray &from);
46 
47  INLINE static SparseArray all_on();
48  INLINE static SparseArray all_off();
49  INLINE static SparseArray lower_on(int on_bits);
50  INLINE static SparseArray bit(int index);
51  INLINE static SparseArray range(int low_bit, int size);
52 
53  INLINE static bool has_max_num_bits();
54  INLINE static int get_max_num_bits();
55 
56  INLINE int get_num_bits() const;
57  INLINE bool get_bit(int index) const;
58  INLINE void set_bit(int index);
59  INLINE void clear_bit(int index);
60  INLINE void set_bit_to(int index, bool value);
61  INLINE bool get_highest_bits() const;
62  INLINE bool is_zero() const;
63  INLINE bool is_all_on() const;
64 
65  INLINE bool has_any_of(int low_bit, int size) const;
66  INLINE bool has_all_of(int low_bit, int size) const;
67  INLINE void set_range(int low_bit, int size);
68  INLINE void clear_range(int low_bit, int size);
69  INLINE void set_range_to(bool value, int low_bit, int size);
70 
71  int get_num_on_bits() const;
72  int get_num_off_bits() const;
73  int get_lowest_on_bit() const;
74  int get_lowest_off_bit() const;
75  int get_highest_on_bit() const;
76  int get_highest_off_bit() const;
77  int get_next_higher_different_bit(int low_bit) const;
78 
79  INLINE void invert_in_place();
80  bool has_bits_in_common(const SparseArray &other) const;
81  INLINE void clear();
82 
83  void output(std::ostream &out) const;
84 
85  INLINE bool operator == (const SparseArray &other) const;
86  INLINE bool operator != (const SparseArray &other) const;
87  INLINE bool operator < (const SparseArray &other) const;
88  int compare_to(const SparseArray &other) const;
89 
90  INLINE SparseArray
91  operator & (const SparseArray &other) const;
92 
93  INLINE SparseArray
94  operator | (const SparseArray &other) const;
95 
96  INLINE SparseArray
97  operator ^ (const SparseArray &other) const;
98 
99  INLINE SparseArray
100  operator ~ () const;
101 
102  INLINE SparseArray
103  operator << (int shift) const;
104 
105  INLINE SparseArray
106  operator >> (int shift) const;
107 
108  void operator &= (const SparseArray &other);
109  void operator |= (const SparseArray &other);
110  void operator ^= (const SparseArray &other);
111  INLINE void operator <<= (int shift);
112  INLINE void operator >>= (int shift);
113 
114  INLINE bool is_inverse() const;
115  INLINE size_t get_num_subranges() const;
116  INLINE int get_subrange_begin(size_t n) const;
117  INLINE int get_subrange_end(size_t n) const;
118 
119 private:
120  void do_add_range(int begin, int end);
121  void do_remove_range(int begin, int end);
122  bool do_has_any(int begin, int end) const;
123  bool do_has_all(int begin, int end) const;
124 
125  void do_intersection(const SparseArray &other);
126  void do_union(const SparseArray &other);
127  void do_intersection_neg(const SparseArray &other);
128  void do_shift(int offset);
129 
130  // The SparseArray is implemented as a set of non-overlapping Subranges.
131  class Subrange {
132  public:
133  INLINE Subrange(int begin, int end);
134  INLINE bool operator < (const Subrange &other) const;
135 
136  int _begin, _end;
137  };
138 
139  typedef ov_set<Subrange> Subranges;
140  Subranges _subranges;
141  bool _inverse;
142 
143 public:
144  void write_datagram(BamWriter *manager, Datagram &dg) const;
145  void read_datagram(DatagramIterator &scan, BamReader *manager);
146 
147 public:
148  static TypeHandle get_class_type() {
149  return _type_handle;
150  }
151  static void init_type() {
152  register_type(_type_handle, "SparseArray");
153  }
154 
155 private:
156  static TypeHandle _type_handle;
157 };
158 
159 #include "sparseArray.I"
160 
161 INLINE std::ostream &
162 operator << (std::ostream &out, const SparseArray &array) {
163  array.output(out);
164  return out;
165 }
166 
167 #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 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.
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
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
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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