Skip to content

Commit

Permalink
change _contains to return location if present
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 13, 2024
1 parent deffc0b commit 71cc992
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions probables/quotientfilter/quotientfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def add_alt(self, _hash: int) -> None:
_hash (int): The element to add"""
key_quotient = _hash >> self._r
key_remainder = _hash & ((1 << self._r) - 1)
if not self._contains(key_quotient, key_remainder):
if self._contained_at_loc(key_quotient, key_remainder) == -1:
self._add(key_quotient, key_remainder)

def check(self, key: KeyT) -> bool:
Expand All @@ -143,7 +143,7 @@ def check_alt(self, _hash: int) -> bool:
bool: True if likely encountered, False if definately not"""
key_quotient = _hash >> self._r
key_remainder = _hash & ((1 << self._r) - 1)
return self._contains(key_quotient, key_remainder)
return self._contained_at_loc(key_quotient, key_remainder) != -1

def iter_hashes(self) -> Iterator[int]:
"""A generator over the hashes in the quotient filter
Expand Down Expand Up @@ -287,9 +287,10 @@ def _add(self, q: int, r: int):
self._shift_insert(q, r, orig_start_idx, start_idx, 1)
self._elements_added += 1

def _contains(self, q: int, r: int) -> bool:
def _contained_at_loc(self, q: int, r: int) -> int:
"""returns the index location of the element, or -1 if not present"""
if self._is_occupied[q] == 0:
return False
return -1

start_idx = self._get_start_index(q)

Expand All @@ -308,7 +309,7 @@ def _contains(self, q: int, r: int) -> bool:
break

if self._filter[start_idx] == r:
return True
return start_idx

start_idx = (start_idx + 1) & (self._size - 1)
meta_bits = (
Expand All @@ -317,4 +318,4 @@ def _contains(self, q: int, r: int) -> bool:
+ self._is_shifted.check_bit(start_idx)
)

return False
return -1

0 comments on commit 71cc992

Please sign in to comment.