diff --git a/panda/src/putil/bitMask.I b/panda/src/putil/bitMask.I index 4b737eec5d..993c35128a 100644 --- a/panda/src/putil/bitMask.I +++ b/panda/src/putil/bitMask.I @@ -400,46 +400,6 @@ set_word(WordType value) { _word = value; } -//////////////////////////////////////////////////////////////////// -// Function: BitMask::invert_in_place -// Access: Published -// Description: Inverts all the bits in the BitMask. This is -// equivalent to mask = ~mask. -//////////////////////////////////////////////////////////////////// -template -INLINE void BitMask:: -invert_in_place() { - _word = ~_word; -} - -//////////////////////////////////////////////////////////////////// -// Function: BitMask::has_bits_in_common -// Access: Published -// Description: Returns true if this BitMask has any "one" bits in -// common with the other one, false otherwise. -// -// This is equivalent to (mask & other) != 0, but may be -// faster. (Actually, it should only be faster in the -// BitArray case, but this method is provided for the -// benefit of generic programming algorithms). -//////////////////////////////////////////////////////////////////// -template -INLINE bool BitMask:: -has_bits_in_common(const BitMask &other) const { - return (_word & other._word) != 0; -} - -//////////////////////////////////////////////////////////////////// -// Function: BitMask::clear -// Access: Published -// Description: Sets all the bits in the BitMask off. -//////////////////////////////////////////////////////////////////// -template -INLINE void BitMask:: -clear() { - _word = 0; -} - //////////////////////////////////////////////////////////////////// // Function: BitMask::get_num_on_bits // Access: Published @@ -565,6 +525,46 @@ get_next_higher_different_bit(int low_bit) const { } } +//////////////////////////////////////////////////////////////////// +// Function: BitMask::invert_in_place +// Access: Published +// Description: Inverts all the bits in the BitMask. This is +// equivalent to mask = ~mask. +//////////////////////////////////////////////////////////////////// +template +INLINE void BitMask:: +invert_in_place() { + _word = ~_word; +} + +//////////////////////////////////////////////////////////////////// +// Function: BitMask::has_bits_in_common +// Access: Published +// Description: Returns true if this BitMask has any "one" bits in +// common with the other one, false otherwise. +// +// This is equivalent to (mask & other) != 0, but may be +// faster. (Actually, it should only be faster in the +// BitArray case, but this method is provided for the +// benefit of generic programming algorithms). +//////////////////////////////////////////////////////////////////// +template +INLINE bool BitMask:: +has_bits_in_common(const BitMask &other) const { + return (_word & other._word) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BitMask::clear +// Access: Published +// Description: Sets all the bits in the BitMask off. +//////////////////////////////////////////////////////////////////// +template +INLINE void BitMask:: +clear() { + _word = 0; +} + //////////////////////////////////////////////////////////////////// // Function: BitMask::output // Access: Published diff --git a/panda/src/putil/doubleBitMask.I b/panda/src/putil/doubleBitMask.I index f465ebb531..ab48a5cb58 100644 --- a/panda/src/putil/doubleBitMask.I +++ b/panda/src/putil/doubleBitMask.I @@ -425,6 +425,136 @@ set_range_to(bool value, int low_bit, int size) { } } +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_num_on_bits +// Access: Published +// Description: Returns the number of bits that are set to 1 in the +// mask. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_num_on_bits() const { + return _lo.get_num_on_bits() + _hi.get_num_on_bits(); +} + +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_num_off_bits +// Access: Published +// Description: Returns the number of bits that are set to 0 in the +// mask. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_num_off_bits() const { + return _lo.get_num_off_bits() + _hi.get_num_off_bits(); +} + +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_lowest_on_bit +// Access: Published +// Description: Returns the index of the lowest 1 bit in the mask. +// Returns -1 if there are no 1 bits. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_lowest_on_bit() const { + int result = _lo.get_lowest_on_bit(); + if (result == -1) { + result = _hi.get_lowest_on_bit(); + if (result != -1) { + result += half_bits; + } + } + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_lowest_off_bit +// Access: Published +// Description: Returns the index of the lowest 0 bit in the mask. +// Returns -1 if there are no 0 bits. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_lowest_off_bit() const { + int result = _lo.get_lowest_off_bit(); + if (result == -1) { + result = _hi.get_lowest_off_bit(); + if (result != -1) { + result += half_bits; + } + } + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_highest_on_bit +// Access: Published +// Description: Returns the index of the highest 1 bit in the mask. +// Returns -1 if there are no 1 bits. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_highest_on_bit() const { + int result = _hi.get_highest_on_bit(); + if (result == -1) { + result = _lo.get_highest_on_bit(); + } else { + result += half_bits; + } + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_highest_off_bit +// Access: Published +// Description: Returns the index of the highest 0 bit in the mask. +// Returns -1 if there are no 0 bits. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_highest_off_bit() const { + int result = _hi.get_highest_off_bit(); + if (result == -1) { + result = _lo.get_highest_off_bit(); + } else { + result += half_bits; + } + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: DoubleBitMask::get_next_higher_different_bit +// Access: Published +// Description: Returns the index of the next bit in the mask, 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 mask. +//////////////////////////////////////////////////////////////////// +template +INLINE int DoubleBitMask:: +get_next_higher_different_bit(int low_bit) const { + if (low_bit > half_bits) { + return _hi.get_next_higher_different_bit(low_bit - half_bits) + half_bits; + } + int result = _lo.get_next_higher_different_bit(low_bit); + if (result != low_bit) { + return result; + } + if (_lo.get_bit(low_bit)) { + result = _hi.get_lowest_off_bit(); + } else { + result = _hi.get_lowest_on_bit(); + } + if (result == -1) { + return low_bit; + } + return result + half_bits; +} + //////////////////////////////////////////////////////////////////// // Function: DoubleBitMask::invert_in_place // Access: Published diff --git a/panda/src/putil/doubleBitMask.h b/panda/src/putil/doubleBitMask.h index 56e805d0c4..0bf97dc5ca 100644 --- a/panda/src/putil/doubleBitMask.h +++ b/panda/src/putil/doubleBitMask.h @@ -76,6 +76,14 @@ PUBLISHED: INLINE void clear_range(int low_bit, int size); INLINE void set_range_to(bool value, int low_bit, int size); + INLINE int get_num_on_bits() const; + INLINE int get_num_off_bits() const; + INLINE int get_lowest_on_bit() const; + INLINE int get_lowest_off_bit() const; + INLINE int get_highest_on_bit() const; + INLINE int get_highest_off_bit() const; + INLINE int get_next_higher_different_bit(int low_bit) const; + INLINE void invert_in_place(); INLINE bool has_bits_in_common(const DoubleBitMask &other) const; INLINE void clear();