sparseArray.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Filename: sparseArray.h
  2. // Created by: drose (14Feb07)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef SPARSEARRAY_H
  19. #define SPARSEARRAY_H
  20. #include "pandabase.h"
  21. #include "ordered_vector.h"
  22. class BitArray;
  23. class BamWriter;
  24. class BamReader;
  25. class Datagram;
  26. class DatagramIterator;
  27. ////////////////////////////////////////////////////////////////////
  28. // Class : SparseArray
  29. // Description : This class records a set of integers, where each
  30. // integer is either present or not present in the set.
  31. //
  32. // It is similar in principle and in interface to a
  33. // BitArray (which can be thought of as a set of
  34. // integers, one integer corresponding to each different
  35. // bit position), but the SparseArray is implemented as
  36. // a list of min/max subrange lists, rather than as a
  37. // bitmask.
  38. //
  39. // This makes it particularly efficient for storing sets
  40. // which consist of large sections of consecutively
  41. // included or consecutively excluded elements, with
  42. // arbitrarily large integers, but particularly
  43. // inefficient for doing boolean operations such as & or
  44. // |.
  45. //
  46. // Also, unlike BitArray, the SparseArray can store
  47. // negative integers.
  48. ////////////////////////////////////////////////////////////////////
  49. class EXPCL_PANDA SparseArray {
  50. PUBLISHED:
  51. INLINE SparseArray();
  52. INLINE SparseArray(const SparseArray &copy);
  53. INLINE SparseArray &operator = (const SparseArray &copy);
  54. SparseArray(const BitArray &from);
  55. INLINE static SparseArray all_on();
  56. INLINE static SparseArray all_off();
  57. INLINE static SparseArray lower_on(int on_bits);
  58. INLINE static SparseArray bit(int index);
  59. INLINE static SparseArray range(int low_bit, int size);
  60. INLINE ~SparseArray();
  61. INLINE static bool has_max_num_bits();
  62. INLINE static int get_max_num_bits();
  63. INLINE int get_num_bits() const;
  64. INLINE bool get_bit(int index) const;
  65. INLINE void set_bit(int index);
  66. INLINE void clear_bit(int index);
  67. INLINE void set_bit_to(int index, bool value);
  68. INLINE bool get_highest_bits() const;
  69. INLINE bool is_zero() const;
  70. INLINE bool is_all_on() const;
  71. INLINE bool has_any_of(int low_bit, int size) const;
  72. INLINE bool has_all_of(int low_bit, int size) const;
  73. INLINE void set_range(int low_bit, int size);
  74. INLINE void clear_range(int low_bit, int size);
  75. INLINE void set_range_to(bool value, int low_bit, int size);
  76. INLINE void invert_in_place();
  77. bool has_bits_in_common(const SparseArray &other) const;
  78. INLINE void clear();
  79. void output(ostream &out) const;
  80. INLINE bool operator == (const SparseArray &other) const;
  81. INLINE bool operator != (const SparseArray &other) const;
  82. INLINE bool operator < (const SparseArray &other) const;
  83. int compare_to(const SparseArray &other) const;
  84. INLINE SparseArray
  85. operator & (const SparseArray &other) const;
  86. INLINE SparseArray
  87. operator | (const SparseArray &other) const;
  88. INLINE SparseArray
  89. operator ^ (const SparseArray &other) const;
  90. INLINE SparseArray
  91. operator ~ () const;
  92. INLINE SparseArray
  93. operator << (int shift) const;
  94. INLINE SparseArray
  95. operator >> (int shift) const;
  96. void operator &= (const SparseArray &other);
  97. void operator |= (const SparseArray &other);
  98. void operator ^= (const SparseArray &other);
  99. INLINE void operator <<= (int shift);
  100. INLINE void operator >>= (int shift);
  101. INLINE bool is_inverse() const;
  102. INLINE int get_num_subranges() const;
  103. INLINE int get_subrange_begin(int n) const;
  104. INLINE int get_subrange_end(int n) const;
  105. private:
  106. void do_add_range(int begin, int end);
  107. void do_remove_range(int begin, int end);
  108. bool do_has_any(int begin, int end) const;
  109. bool do_has_all(int begin, int end) const;
  110. void do_intersection(const SparseArray &other);
  111. void do_union(const SparseArray &other);
  112. void do_intersection_neg(const SparseArray &other);
  113. void do_shift(int offset);
  114. // The SparseArray is implemented as a set of non-overlapping
  115. // Subranges.
  116. class Subrange {
  117. public:
  118. INLINE Subrange(int begin, int end);
  119. INLINE bool operator < (const Subrange &other) const;
  120. int _begin, _end;
  121. };
  122. typedef ov_set<Subrange> Subranges;
  123. Subranges _subranges;
  124. bool _inverse;
  125. public:
  126. void write_datagram(BamWriter *manager, Datagram &dg) const;
  127. void read_datagram(DatagramIterator &scan, BamReader *manager);
  128. public:
  129. static TypeHandle get_class_type() {
  130. return _type_handle;
  131. }
  132. static void init_type() {
  133. register_type(_type_handle, "SparseArray");
  134. }
  135. private:
  136. static TypeHandle _type_handle;
  137. };
  138. #include "sparseArray.I"
  139. INLINE ostream &
  140. operator << (ostream &out, const SparseArray &array) {
  141. array.output(out);
  142. return out;
  143. }
  144. #endif