diff --git a/panda/src/putil/sparseArray.cxx b/panda/src/putil/sparseArray.cxx index 5404728aa1..54aa8b03ea 100644 --- a/panda/src/putil/sparseArray.cxx +++ b/panda/src/putil/sparseArray.cxx @@ -89,12 +89,16 @@ get_num_off_bits() const { /** * Returns the index of the lowest 1 bit in the array. Returns -1 if there - * are no 1 bits or if there are an infinite number of 1 bits. + * are no 1 bits and 0 if there are an infinite number of 1 bits. */ int SparseArray:: get_lowest_on_bit() const { if (_inverse) { - return -1; + if (_subranges.empty() || _subranges[0]._begin > 0) { + return 0; + } else { + return _subranges[0]._end; + } } if (_subranges.empty()) { @@ -111,7 +115,11 @@ get_lowest_on_bit() const { int SparseArray:: get_lowest_off_bit() const { if (!_inverse) { - return -1; + if (_subranges.empty() || _subranges[0]._begin > 0) { + return 0; + } else { + return _subranges[0]._end; + } } if (_subranges.empty()) { diff --git a/tests/putil/test_sparsearray.py b/tests/putil/test_sparsearray.py index 5138914cdf..4c627a29be 100644 --- a/tests/putil/test_sparsearray.py +++ b/tests/putil/test_sparsearray.py @@ -235,6 +235,35 @@ def test_sparse_array_augm_assignment(): assert s is t +def test_sparse_array_lowest_bit(): + sa = core.SparseArray() + assert sa.get_lowest_off_bit() == 0 + assert sa.get_lowest_on_bit() == -1 + + sa.invert_in_place() + assert sa.get_lowest_off_bit() == -1 + assert sa.get_lowest_on_bit() == 0 + + sa = core.SparseArray() + sa.set_bit(0) + sa.set_bit(1) + assert sa.get_lowest_off_bit() == 2 + assert sa.get_lowest_on_bit() == 0 + + sa.invert_in_place() + assert sa.get_lowest_off_bit() == 0 + assert sa.get_lowest_on_bit() == 2 + + sa = core.SparseArray() + sa.set_bit(2) + assert sa.get_lowest_off_bit() == 0 + assert sa.get_lowest_on_bit() == 2 + + sa.invert_in_place() + assert sa.get_lowest_off_bit() == 2 + assert sa.get_lowest_on_bit() == 0 + + def test_sparse_array_getstate(): sa = core.SparseArray() assert sa.__getstate__() == ()