Skip to content

Commit

Permalink
Fix in-place operations with self (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezibenroc authored Feb 10, 2024
1 parent 0f68dde commit f3cc686
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pyroaring/bitmap.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,28 @@ cdef class BitMap(AbstractBitMap):

def __ior__(self, other):
self._check_compatibility(other)
if self._c_bitmap == (<AbstractBitMap?>other)._c_bitmap:
return self
return (<BitMap>self).binary_iop(<AbstractBitMap?>other, croaring.roaring_bitmap_or_inplace)

def __iand__(self, other):
self._check_compatibility(other)
if self._c_bitmap == (<AbstractBitMap?>other)._c_bitmap:
return self
return (<BitMap>self).binary_iop(<AbstractBitMap?>other, croaring.roaring_bitmap_and_inplace)

def __ixor__(self, other):
self._check_compatibility(other)
if self._c_bitmap == (<AbstractBitMap?>other)._c_bitmap:
self.clear()
return self
return (<BitMap>self).binary_iop(<AbstractBitMap?>other, croaring.roaring_bitmap_xor_inplace)

def __isub__(self, other):
self._check_compatibility(other)
if self._c_bitmap == (<AbstractBitMap?>other)._c_bitmap:
self.clear()
return self
return (<BitMap>self).binary_iop(<AbstractBitMap?>other, croaring.roaring_bitmap_andnot_inplace)

def intersection_update(self, *all_values): # FIXME could be more efficient
Expand Down
15 changes: 15 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,21 @@ def test_binary_op_inplace(
assert self.bitmap2 == old_bitmap2
self.compare_with_set(self.bitmap1, self.set1)

@given(hyp_collection, st.booleans())
def test_binary_op_inplace_self(
self,
values: HypCollection,
cow: bool,
) -> None:
for op in [operator.ior, operator.iand, operator.ixor, operator.isub]:
self.set = set(values)
self.bitmap = BitMap(values, cow)
original = self.bitmap
op(self.set, self.set)
op(self.bitmap, self.bitmap)
assert original is self.bitmap
self.compare_with_set(self.bitmap, self.set)

@given(bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_binary_op_inplace_frozen(
self,
Expand Down

0 comments on commit f3cc686

Please sign in to comment.