15 #include "sparseArray.h"
18 #include "datagramIterator.h"
33 bool current_state = from.
get_bit(0);
40 if (from.
get_bit(i) != current_state) {
42 if (current_state != empty_bit) {
43 Subrange
range(begin, i);
47 current_state = !current_state;
52 nassertv(current_state == empty_bit);
69 Subranges::const_iterator si;
70 for (si = _subranges.
begin(); si != _subranges.
end(); ++si) {
71 result += (*si)._end - (*si)._begin;
91 Subranges::const_iterator si;
92 for (si = _subranges.
begin(); si != _subranges.
end(); ++si) {
93 result += (*si)._end - (*si)._begin;
112 if (_subranges.
empty()) {
116 return _subranges[0]._begin;
132 if (_subranges.
empty()) {
136 return _subranges[0]._begin;
152 if (_subranges.
empty()) {
156 return _subranges[_subranges.
size() - 1]._end - 1;
172 if (_subranges.
empty()) {
176 return _subranges[_subranges.
size() - 1]._end - 1;
192 Subrange
range(low_bit, low_bit + 1);
193 Subranges::const_iterator si = _subranges.lower_bound(range);
194 if (si == _subranges.
end()) {
199 if (low_bit >= (*si)._begin) {
203 int next = (*si)._begin;
205 if (si != _subranges.
begin()) {
207 if (low_bit < (*si)._end) {
226 if (_inverse && other._inverse) {
231 if (_inverse != other._inverse) {
233 return !(*
this & other).
is_zero();
238 return !(*
this & other).
is_zero();
247 output(ostream &out)
const {
250 out <<
"all except: ";
252 Subranges::const_iterator si;
253 for (si = _subranges.
begin(); si != _subranges.
end(); ++si) {
254 if ((*si)._end == (*si)._begin + 1) {
256 out << (*si)._begin <<
", ";
259 out << (*si)._begin <<
"-" << ((*si)._end - 1) <<
", ";
276 if (_inverse != other._inverse) {
277 return _inverse ? 1 : -1;
280 Subranges::const_reverse_iterator ai = _subranges.
rbegin();
281 Subranges::const_reverse_iterator bi = other._subranges.
rbegin();
283 while (ai != _subranges.
rend() && bi != other._subranges.
rend()) {
284 if ((*ai)._end < (*bi)._end) {
287 }
else if ((*bi)._end < (*ai)._end) {
290 }
else if ((*ai)._begin < (*bi)._begin) {
293 }
else if ((*bi)._begin < (*ai)._begin) {
302 if (ai != _subranges.
rend()) {
306 if (bi != other._subranges.
rend()) {
326 if (_inverse && other._inverse) {
329 }
else if (!_inverse && !other._inverse) {
330 do_intersection(other);
332 }
else if (_inverse && !other._inverse) {
334 (*this) = other & (*this);
336 }
else if (!_inverse && other._inverse) {
337 do_intersection_neg(other);
357 if (_inverse && other._inverse) {
358 do_intersection(other);
360 }
else if (!_inverse && !other._inverse) {
363 }
else if (_inverse && !other._inverse) {
364 do_intersection_neg(other);
366 }
else if (!_inverse && other._inverse) {
368 (*this) = other | (*this);
387 (*this) = ((*this) | other) & ~((*
this) & other);
399 do_add_range(
int begin,
int end) {
405 Subrange
range(begin, end);
406 Subranges::iterator si = _subranges.lower_bound(
range);
407 if (si == _subranges.
end()) {
408 if (!_subranges.
empty()) {
409 si = _subranges.
begin() + _subranges.
size() - 1;
410 if ((*si)._end >= begin) {
427 nassertv((*si)._end >= end);
429 if ((*si)._begin > end) {
430 if (si != _subranges.
begin()) {
431 Subranges::iterator si2 = si;
433 if ((*si2)._end >= begin) {
452 while (si != _subranges.
begin()) {
453 Subranges::iterator si2 = si;
455 if ((*si2)._end >= begin) {
457 (*si2)._end = (*si)._end;
458 _subranges.erase(si);
466 if ((*si)._begin > begin) {
468 (*si)._begin = begin;
479 do_remove_range(
int begin,
int end) {
485 Subrange
range(begin, end);
486 Subranges::iterator si = _subranges.lower_bound(
range);
487 if (si == _subranges.
end()) {
488 if (!_subranges.
empty()) {
489 si = _subranges.
begin() + _subranges.
size() - 1;
490 if ((*si)._end >= begin) {
492 end = min(end, (*si)._begin);
506 nassertv((*si)._end >= end);
508 if ((*si)._begin > end) {
509 if (si != _subranges.
begin()) {
510 Subranges::iterator si2 = si;
512 if ((*si2)._end >= begin) {
515 end = min(end, (*si2)._begin);
530 if (end < (*si)._end) {
532 Subrange left_range((*si)._begin, begin);
538 while (begin <= (*si)._begin) {
539 if (si == _subranges.
begin()) {
540 _subranges.erase(si);
543 Subranges::iterator si2 = si;
545 _subranges.erase(si);
549 (*si)._end = min((*si)._end, begin);
561 do_has_any(
int begin,
int end)
const {
567 Subrange
range(begin, end);
568 Subranges::const_iterator si = _subranges.lower_bound(
range);
569 if (si != _subranges.
end() && end > (*si)._begin) {
572 if (si != _subranges.
begin()) {
574 if (begin < (*si)._end) {
591 do_has_all(
int begin,
int end)
const {
597 Subrange
range(begin, end);
598 Subranges::const_iterator si = _subranges.lower_bound(
range);
599 if (si != _subranges.
end() && begin >= (*si)._begin) {
614 if (_subranges.
empty()) {
617 if (other._subranges.
empty()) {
622 int my_begin = (*_subranges.
begin())._begin;
623 int other_begin = (*other._subranges.
begin())._begin;
624 do_remove_range(my_begin, other_begin);
626 for (
size_t i = 0; i < other._subranges.
size() - 1; ++i) {
627 do_remove_range(other._subranges[i]._end, other._subranges[i + 1]._begin);
630 int my_end = (*(_subranges.
begin() + _subranges.
size() - 1))._end;
631 int other_end = (*(other._subranges.
begin() + other._subranges.
size() - 1))._end;
632 do_remove_range(other_end, my_end);
643 Subranges::const_iterator si;
644 for (si = other._subranges.
begin(); si != other._subranges.
end(); ++si) {
645 do_add_range((*si)._begin, (*si)._end);
657 Subranges::const_iterator si;
658 for (si = other._subranges.
begin(); si != other._subranges.
end(); ++si) {
659 do_remove_range((*si)._begin, (*si)._end);
670 do_shift(
int offset) {
672 Subranges::iterator si;
673 for (si = _subranges.
begin(); si != _subranges.
end(); ++si) {
674 (*si)._begin += offset;
675 (*si)._end += offset;
689 Subranges::const_iterator si;
690 for (si = _subranges.
begin(); si != _subranges.
end(); ++si) {
706 _subranges.
reserve(num_subranges);
707 for (
size_t i = 0; i < num_subranges; ++i) {
710 _subranges.
push_back(Subrange(begin, end));
bool get_bit(int index) const
Returns true if the nth bit is set, false if it is cleared.
This class records a set of integers, where each integer is either present or not present in the set...
bool get_bool()
Extracts a boolean value.
bool empty() const
Returns true if the ordered vector is empty, false otherwise.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
void clear()
Removes all elements from the ordered vector.
int get_next_higher_different_bit(int low_bit) const
Returns the index of the next bit in the array, above low_bit, whose value is different that the valu...
int get_num_on_bits() const
Returns the number of bits that are set to 1 in the array.
iterator_0 begin()
Returns the iterator that marks the first element in the ordered vector.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
iterator_0 end()
Returns the iterator that marks the end of the ordered vector.
PN_int32 get_int32()
Extracts a signed 32-bit integer.
PN_uint32 get_uint32()
Extracts an unsigned 32-bit integer.
void reserve(size_type_0 n)
Informs the vector of a planned change in size; ensures that the capacity of the vector is greater th...
int get_num_off_bits() const
Returns the number of bits that are set to 0 in the array.
int get_lowest_on_bit() const
Returns the index of the lowest 1 bit in the array.
int get_highest_off_bit() const
Returns the index of the highest 0 bit in the array.
A dynamic array with an unlimited number of bits.
iterator_0 insert_unverified(iterator_0 position, const value_type_0 &key)
Inserts the indicated key into the ordered vector at the indicated place.
int compare_to(const SparseArray &other) const
Returns a number less than zero if this SparseArray sorts before the indicated other SparseArray...
void add_bool(bool value)
Adds a boolean value to the datagram.
int get_highest_on_bit() const
Returns the index of the highest 1 bit in the array.
bool has_bits_in_common(const SparseArray &other) const
Returns true if this SparseArray has any "one" bits in common with the other one, false otherwise...
reverse_iterator_0 rbegin()
Returns the iterator that marks the first element in the ordered vector, when viewed in reverse order...
bool is_zero() const
Returns true if the entire bitmask is zero, false otherwise.
static SparseArray range(int low_bit, int size)
Returns a SparseArray whose size bits, beginning at low_bit, are on.
int get_lowest_off_bit() const
Returns the index of the lowest 0 bit in the array.
void push_back(const value_type_0 &key)
Adds the new element to the end of the vector without regard for proper sorting.
reverse_iterator_0 rend()
Returns the iterator that marks the end of the ordered vector, when viewed in reverse order...
bool get_highest_bits() const
Returns true if the infinite set of bits beyond get_num_bits() are all on, or false of they are all o...
int get_num_bits() const
Returns the current number of possibly different bits in this array.
void add_uint32(PN_uint32 value)
Adds an unsigned 32-bit integer to the datagram.
size_type_0 size() const
Returns the number of elements in the ordered vector.
void write_datagram(BamWriter *manager, Datagram &dg) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the datagram.
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
void read_datagram(DatagramIterator &scan, BamReader *manager)
Reads the object that was previously written to a Bam file.