Panda3D
Public Types | Public Member Functions | Static Public Member Functions | List of all members
BitArray Class Reference

A dynamic array with an unlimited number of bits. More...

#include "bitArray.h"

Public Types

enum  { num_bits_per_word = MaskType::num_bits }
 
typedef BitMaskNative MaskType
 
typedef MaskType::WordType WordType
 

Public Member Functions

 BitArray (WordType init_value)
 
 BitArray (const BitArray &copy)
 
 BitArray (const SparseArray &from)
 
void clear ()
 Sets all the bits in the BitArray off. More...
 
void clear_bit (int index)
 Sets the nth bit off. More...
 
void clear_range (int low_bit, int size)
 Sets the indicated range of bits off. More...
 
int compare_to (const BitArray &other) const
 Returns a number less than zero if this BitArray sorts before the indicated other BitArray, greater than zero if it sorts after, or 0 if they are equivalent. More...
 
WordType extract (int low_bit, int size) const
 Returns a word that represents only the indicated range of bits within this BitArray, shifted to the least-significant position. More...
 
void generate_hash (ChecksumHashGenerator &hashgen) const
 Adds the bitmask to the indicated hash generator. More...
 
bool get_bit (int index) const
 Returns true if the nth bit is set, false if it is cleared. More...
 
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 off. More...
 
int get_highest_off_bit () const
 Returns the index of the highest 0 bit in the array. More...
 
int get_highest_on_bit () const
 Returns the index of the highest 1 bit in the array. More...
 
int get_lowest_off_bit () const
 Returns the index of the lowest 0 bit in the array. More...
 
int get_lowest_on_bit () const
 Returns the index of the lowest 1 bit in the array. More...
 
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 value of low_bit. More...
 
int get_num_bits () const
 Returns the current number of possibly different bits in this array. More...
 
int get_num_off_bits () const
 Returns the number of bits that are set to 0 in the array. More...
 
int get_num_on_bits () const
 Returns the number of bits that are set to 1 in the array. More...
 
int get_num_words () const
 Returns the number of possibly-unique words stored in the array. More...
 
MaskType get_word (int n) const
 Returns the nth word in the array. More...
 
bool has_all_of (int low_bit, int size) const
 Returns true if all bits in the indicated range are set, false otherwise. More...
 
bool has_any_of (int low_bit, int size) const
 Returns true if any bit in the indicated range is set, false otherwise. More...
 
bool has_bits_in_common (const BitArray &other) const
 Returns true if this BitArray has any "one" bits in common with the other one, false otherwise. More...
 
void invert_in_place ()
 Inverts all the bits in the BitArray. More...
 
bool is_all_on () const
 Returns true if the entire bitmask is one, false otherwise. More...
 
bool is_zero () const
 Returns true if the entire bitmask is zero, false otherwise. More...
 
bool operator!= (const BitArray &other) const
 
BitArray operator& (const BitArray &other) const
 
void operator&= (const BitArray &other)
 
bool operator< (const BitArray &other) const
 Returns true if the unsigned integer which is represented by this BitArray is less than that of the other one, false otherwise. More...
 
BitArray operator<< (int shift) const
 
void operator<<= (int shift)
 Logical left shift. More...
 
BitArrayoperator= (const BitArray &copy)
 
bool operator== (const BitArray &other) const
 
BitArray operator>> (int shift) const
 
void operator>>= (int shift)
 Logical right shift. More...
 
BitArray operator^ (const BitArray &other) const
 
void operator^= (const BitArray &other)
 
BitArray operator| (const BitArray &other) const
 
void operator|= (const BitArray &other)
 
BitArray operator~ () const
 
void output (ostream &out) const
 Writes the BitArray out as a hex number. More...
 
void output_binary (ostream &out, int spaces_every=4) const
 Writes the BitArray out as a binary number, with spaces every four bits. More...
 
void output_hex (ostream &out, int spaces_every=4) const
 Writes the BitArray out as a hexadecimal number, with spaces every four digits. More...
 
void read_datagram (DatagramIterator &scan, BamReader *manager)
 Reads the object that was previously written to a Bam file. More...
 
void set_bit (int index)
 Sets the nth bit on. More...
 
void set_bit_to (int index, bool value)
 Sets the nth bit either on or off, according to the indicated bool value. More...
 
void set_range (int low_bit, int size)
 Sets the indicated range of bits on. More...
 
void set_range_to (bool value, int low_bit, int size)
 Sets the indicated range of bits to either on or off. More...
 
void set_word (int n, WordType value)
 Replaces the nth word in the array. More...
 
void store (WordType value, int low_bit, int size)
 Stores the indicated word into the indicated range of bits with this BitArray. More...
 
void write (ostream &out, int indent_level=0) const
 Writes the BitArray out as a binary or a hex number, according to the number of bits. More...
 
void write_datagram (BamWriter *manager, Datagram &dg) const
 Writes the contents of this object to the datagram for shipping out to a Bam file. More...
 

Static Public Member Functions

static BitArray all_off ()
 Returns a BitArray whose bits are all off. More...
 
static BitArray all_on ()
 Returns a BitArray with an infinite array of bits, all on. More...
 
static BitArray bit (int index)
 Returns a BitArray with only the indicated bit on. More...
 
static TypeHandle get_class_type ()
 
static CONSTEXPR int get_max_num_bits ()
 If get_max_num_bits() returned true, this method may be called to return the maximum number of bits that may be stored in this structure. More...
 
static CONSTEXPR int get_num_bits_per_word ()
 Returns the number of bits stored per word internally. More...
 
static CONSTEXPR bool has_max_num_bits ()
 Returns true if there is a maximum number of bits that may be stored in this structure, false otherwise. More...
 
static void init_type ()
 
static BitArray lower_on (int on_bits)
 Returns a BitArray whose lower on_bits bits are on. More...
 
static BitArray range (int low_bit, int size)
 Returns a BitArray whose size bits, beginning at low_bit, are on. More...
 

Detailed Description

A dynamic array with an unlimited number of bits.

This is similar to a BitMask, except it appears to contain an infinite number of bits. You can use it very much as you would use a BitMask.

Definition at line 42 of file bitArray.h.

Member Function Documentation

◆ all_off()

BitArray BitArray::all_off ( )
inlinestatic

Returns a BitArray whose bits are all off.

Definition at line 82 of file bitArray.I.

References lower_on().

Referenced by all_on().

◆ all_on()

BitArray BitArray::all_on ( )
inlinestatic

Returns a BitArray with an infinite array of bits, all on.

Definition at line 70 of file bitArray.I.

References all_off().

Referenced by PartBundle::do_bind_anim().

◆ bit()

BitArray BitArray::bit ( int  index)
inlinestatic

Returns a BitArray with only the indicated bit on.

Definition at line 104 of file bitArray.I.

References range(), and set_bit().

Referenced by lower_on().

◆ clear()

void BitArray::clear ( )
inline

Sets all the bits in the BitArray off.

Definition at line 408 of file bitArray.I.

References compare_to(), and operator<().

Referenced by set_word().

◆ clear_bit()

void BitArray::clear_bit ( int  index)
inline

Sets the nth bit off.

If n >= get_num_bits(), this automatically extends the array.

Definition at line 245 of file bitArray.I.

References get_num_words(), and set_bit_to().

Referenced by set_bit(), set_bit_to(), and MovingPartBase::update_internals().

◆ clear_range()

void BitArray::clear_range ( int  low_bit,
int  size 
)

Sets the indicated range of bits off.

Definition at line 260 of file bitArray.cxx.

References get_num_on_bits(), and get_num_words().

Referenced by set_range(), and set_range_to().

◆ compare_to()

int BitArray::compare_to ( const BitArray other) const

Returns a number less than zero if this BitArray sorts before the indicated other BitArray, greater than zero if it sorts after, or 0 if they are equivalent.

This is based on the same ordering defined by operator <.

Definition at line 635 of file bitArray.cxx.

References get_num_words(), get_word(), and operator<<=().

Referenced by clear(), operator<(), and write().

◆ extract()

BitArray::WordType BitArray::extract ( int  low_bit,
int  size 
) const
inline

Returns a word that represents only the indicated range of bits within this BitArray, shifted to the least-significant position.

size must be <= get_num_bits_per_word().

Definition at line 294 of file bitArray.I.

References get_word(), and store().

Referenced by get_highest_bits(), and output_hex().

◆ generate_hash()

void BitArray::generate_hash ( ChecksumHashGenerator hashgen) const

Adds the bitmask to the indicated hash generator.

Definition at line 928 of file bitArray.cxx.

References ChecksumHashGenerator::add_int(), and write_datagram().

Referenced by operator>>=().

◆ get_bit()

bool BitArray::get_bit ( int  index) const
inline

Returns true if the nth bit is set, false if it is cleared.

It is valid for n to increase beyond get_num_bits(), but the return value get_num_bits() will always be the same.

Definition at line 207 of file bitArray.I.

References get_highest_bits(), get_num_words(), and set_bit().

Referenced by PStatClientData::add_collector(), GeomTransformer::finish_collect(), get_next_higher_different_bit(), get_num_bits(), GeomPrimitive::get_primitive_max_vertex(), MouseWatcher::is_button_down(), and output_binary().

◆ get_highest_bits()

bool BitArray::get_highest_bits ( ) const
inline

Returns true if the infinite set of bits beyond get_num_bits() are all on, or false of they are all off.

Definition at line 281 of file bitArray.I.

References extract().

Referenced by get_bit(), and set_bit_to().

◆ get_highest_off_bit()

int BitArray::get_highest_off_bit ( ) const

Returns the index of the highest 0 bit in the array.

Returns -1 if there are no 0 bits or if there an infinite number of 1 bits.

Definition at line 422 of file bitArray.cxx.

References get_next_higher_different_bit(), and get_num_words().

Referenced by get_highest_on_bit().

◆ get_highest_on_bit()

int BitArray::get_highest_on_bit ( ) const

Returns the index of the highest 1 bit in the array.

Returns -1 if there are no 1 bits or if there an infinite number of 1 bits.

Definition at line 400 of file bitArray.cxx.

References get_highest_off_bit(), and get_num_words().

Referenced by get_lowest_off_bit().

◆ get_lowest_off_bit()

int BitArray::get_lowest_off_bit ( ) const

Returns the index of the lowest 0 bit in the array.

Returns -1 if there are no 0 bits.

Definition at line 377 of file bitArray.cxx.

References get_highest_on_bit(), and get_num_words().

Referenced by get_lowest_on_bit().

◆ get_lowest_on_bit()

int BitArray::get_lowest_on_bit ( ) const

Returns the index of the lowest 1 bit in the array.

Returns -1 if there are no 1 bits.

Definition at line 355 of file bitArray.cxx.

References get_lowest_off_bit(), and get_num_words().

Referenced by get_num_off_bits(), and GeomPrimitive::get_primitive_max_vertex().

◆ get_max_num_bits()

CONSTEXPR int BitArray::get_max_num_bits ( )
static

If get_max_num_bits() returned true, this method may be called to return the maximum number of bits that may be stored in this structure.

It is an error to call this if get_max_num_bits() return false.

It is always an error to call this method. The BitArray has no maximum number of bits. This method is defined so generic programming algorithms can use BitMask or BitArray interchangeably.

Definition at line 164 of file bitArray.I.

References get_num_bits_per_word().

Referenced by has_max_num_bits().

◆ get_next_higher_different_bit()

int BitArray::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 value of low_bit.

Returns low_bit again if all bits higher than low_bit have the same value.

This can be used to quickly iterate through all of the bits in the array.

Definition at line 448 of file bitArray.cxx.

References get_bit(), get_num_words(), and invert_in_place().

Referenced by get_highest_off_bit(), and GeomPrimitive::get_primitive_max_vertex().

◆ get_num_bits()

int BitArray::get_num_bits ( ) const
inline

Returns the current number of possibly different bits in this array.

There are actually an infinite number of bits, but every bit higher than this bit will have the same value, either 0 or 1 (see get_highest_bits()).

This number may grow and/or shrink automatically as needed.

Definition at line 194 of file bitArray.I.

References get_bit(), and get_num_words().

Referenced by PStatClientData::add_collector(), get_num_bits_per_word(), output_binary(), and output_hex().

◆ get_num_bits_per_word()

CONSTEXPR int BitArray::get_num_bits_per_word ( )
static

Returns the number of bits stored per word internally.

This is of interest only in that it limits the maximum number of bits that may be queried or set at once by extract() and store().

Definition at line 177 of file bitArray.I.

References get_num_bits().

Referenced by get_max_num_bits().

◆ get_num_off_bits()

int BitArray::get_num_off_bits ( ) const

Returns the number of bits that are set to 0 in the array.

Returns -1 if there are an infinite number of 0 bits.

Definition at line 335 of file bitArray.cxx.

References get_lowest_on_bit().

Referenced by get_num_on_bits().

◆ get_num_on_bits()

int BitArray::get_num_on_bits ( ) const

Returns the number of bits that are set to 1 in the array.

Returns -1 if there are an infinite number of 1 bits.

Definition at line 314 of file bitArray.cxx.

References get_num_off_bits().

Referenced by clear_range(), GeomTransformer::finish_collect(), and GeomPrimitive::get_primitive_max_vertex().

◆ get_num_words()

int BitArray::get_num_words ( ) const
inline

◆ get_word()

BitArray::MaskType BitArray::get_word ( int  n) const
inline

Returns the nth word in the array.

It is valid for n to be greater than get_num_words(), but the return value beyond get_num_words() will always be the same.

Definition at line 375 of file bitArray.I.

References get_num_words(), and set_word().

Referenced by compare_to(), extract(), get_num_words(), has_all_of(), and has_any_of().

◆ has_all_of()

bool BitArray::has_all_of ( int  low_bit,
int  size 
) const

Returns true if all bits in the indicated range are set, false otherwise.

Definition at line 153 of file bitArray.cxx.

References get_num_words(), get_word(), is_all_on(), and set_range().

Referenced by has_any_of().

◆ has_any_of()

bool BitArray::has_any_of ( int  low_bit,
int  size 
) const

Returns true if any bit in the indicated range is set, false otherwise.

Definition at line 97 of file bitArray.cxx.

References get_num_words(), get_word(), has_all_of(), and is_zero().

Referenced by is_all_on().

◆ has_bits_in_common()

bool BitArray::has_bits_in_common ( const BitArray other) const

Returns true if this BitArray has any "one" bits in common with the other one, false otherwise.

This is equivalent to (array & other) != 0, but may be faster.

Definition at line 507 of file bitArray.cxx.

References output().

Referenced by PartBundle::do_bind_anim(), and invert_in_place().

◆ has_max_num_bits()

CONSTEXPR bool BitArray::has_max_num_bits ( )
static

Returns true if there is a maximum number of bits that may be stored in this structure, false otherwise.

If this returns true, the number may be queried in get_max_num_bits().

This method always returns false. The BitArray has no maximum number of bits. This method is defined so generic programming algorithms can use BitMask or BitArray interchangeably.

Definition at line 146 of file bitArray.I.

References get_max_num_bits().

Referenced by range().

◆ invert_in_place()

void BitArray::invert_in_place ( )

Inverts all the bits in the BitArray.

This is equivalent to array = ~array.

Definition at line 488 of file bitArray.cxx.

References has_bits_in_common().

Referenced by get_next_higher_different_bit(), and operator<().

◆ is_all_on()

bool BitArray::is_all_on ( ) const

Returns true if the entire bitmask is one, false otherwise.

Definition at line 74 of file bitArray.cxx.

References has_any_of().

Referenced by has_all_of(), and is_zero().

◆ is_zero()

bool BitArray::is_zero ( ) const

Returns true if the entire bitmask is zero, false otherwise.

Definition at line 50 of file bitArray.cxx.

References is_all_on().

Referenced by has_any_of().

◆ lower_on()

BitArray BitArray::lower_on ( int  on_bits)
inlinestatic

Returns a BitArray whose lower on_bits bits are on.

Definition at line 92 of file bitArray.I.

References bit(), and set_range().

Referenced by all_off(), operator<<=(), and operator>>=().

◆ operator<()

bool BitArray::operator< ( const BitArray other) const
inline

Returns true if the unsigned integer which is represented by this BitArray is less than that of the other one, false otherwise.

Definition at line 441 of file bitArray.I.

References compare_to(), and invert_in_place().

Referenced by clear().

◆ operator<<=()

void BitArray::operator<<= ( int  shift)

Logical left shift.

The rightmost bits are filled in with zeroes. Since this is an infinite bit array, none of the bits on the left are lost.

Definition at line 795 of file bitArray.cxx.

References lower_on(), and operator>>=().

Referenced by compare_to(), and operator>>=().

◆ operator>>=()

void BitArray::operator>>= ( int  shift)

Logical right shift.

The rightmost bits are lost. Since this is an infinite bit array, there is no question of sign extension; there is no need to synthesize bits on the left.

Definition at line 863 of file bitArray.cxx.

References generate_hash(), lower_on(), and operator<<=().

Referenced by operator<<=().

◆ output()

void BitArray::output ( ostream &  out) const

Writes the BitArray out as a hex number.

For a BitArray, this is always the same as output_hex(); it's too confusing for the output format to change back and forth at runtime.

Definition at line 563 of file bitArray.cxx.

References output_binary(), and output_hex().

Referenced by has_bits_in_common().

◆ output_binary()

void BitArray::output_binary ( ostream &  out,
int  spaces_every = 4 
) const

Writes the BitArray out as a binary number, with spaces every four bits.

Definition at line 574 of file bitArray.cxx.

References get_bit(), get_num_bits(), and output_hex().

Referenced by output().

◆ output_hex()

void BitArray::output_hex ( ostream &  out,
int  spaces_every = 4 
) const

Writes the BitArray out as a hexadecimal number, with spaces every four digits.

Definition at line 594 of file bitArray.cxx.

References extract(), get_num_bits(), and write().

Referenced by output(), and output_binary().

◆ range()

BitArray BitArray::range ( int  low_bit,
int  size 
)
inlinestatic

Returns a BitArray whose size bits, beginning at low_bit, are on.

Definition at line 117 of file bitArray.I.

References has_max_num_bits(), and set_range().

Referenced by bit().

◆ read_datagram()

void BitArray::read_datagram ( DatagramIterator scan,
BamReader manager 
)

Reads the object that was previously written to a Bam file.

Definition at line 1008 of file bitArray.cxx.

References DatagramIterator::get_uint32(), and DatagramIterator::get_uint8().

Referenced by write_datagram().

◆ set_bit()

void BitArray::set_bit ( int  index)
inline

Sets the nth bit on.

If n >= get_num_bits(), this automatically extends the array.

Definition at line 225 of file bitArray.I.

References clear_bit(), and get_num_words().

Referenced by bit(), GeomTransformer::finish_collect(), get_bit(), GeomPrimitive::get_primitive_max_vertex(), set_bit_to(), and MovingPartBase::update_internals().

◆ set_bit_to()

void BitArray::set_bit_to ( int  index,
bool  value 
)
inline

Sets the nth bit either on or off, according to the indicated bool value.

Definition at line 265 of file bitArray.I.

References clear_bit(), get_highest_bits(), and set_bit().

Referenced by clear_bit(), and MovingPartBase::update_internals().

◆ set_range()

void BitArray::set_range ( int  low_bit,
int  size 
)

Sets the indicated range of bits on.

Definition at line 208 of file bitArray.cxx.

References clear_range(), and get_num_words().

Referenced by GeomPrimitive::get_primitive_max_vertex(), has_all_of(), lower_on(), range(), and set_range_to().

◆ set_range_to()

void BitArray::set_range_to ( bool  value,
int  low_bit,
int  size 
)
inline

Sets the indicated range of bits to either on or off.

Definition at line 348 of file bitArray.I.

References clear_range(), get_num_words(), and set_range().

Referenced by store().

◆ set_word()

void BitArray::set_word ( int  n,
WordType  value 
)
inline

Replaces the nth word in the array.

If n >= get_num_words(), this automatically extends the array.

Definition at line 395 of file bitArray.I.

References clear().

Referenced by get_word().

◆ store()

void BitArray::store ( WordType  value,
int  low_bit,
int  size 
)
inline

Stores the indicated word into the indicated range of bits with this BitArray.

Definition at line 320 of file bitArray.I.

References set_range_to().

Referenced by extract().

◆ write()

void BitArray::write ( ostream &  out,
int  indent_level = 0 
) const

Writes the BitArray out as a binary or a hex number, according to the number of bits.

Definition at line 622 of file bitArray.cxx.

References compare_to().

Referenced by output_hex().

◆ write_datagram()

void BitArray::write_datagram ( BamWriter manager,
Datagram dg 
) const

Writes the contents of this object to the datagram for shipping out to a Bam file.

Definition at line 992 of file bitArray.cxx.

References Datagram::add_uint32(), Datagram::add_uint8(), and read_datagram().

Referenced by generate_hash().


The documentation for this class was generated from the following files: