Skip to content

Commit

Permalink
Fix for the fill behavior of compat-set
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Bormann <ChristianCarl.Bormann@de.bosch.com>
  • Loading branch information
c2bo committed Sep 13, 2022
1 parent cd0d349 commit 2ca82ec
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions indy_common/compat_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,29 @@ def insert_entry_clean(self, entry: _Entry):
table = self._table
i = entry.hash & mask
perturb = entry.hash
found = None

while True:
if table[i] is None:
table[i] = entry
return
found = i
break

if i + LINEAR_PROBES <= mask:
for j in range(i + 1, i + LINEAR_PROBES + 1):
if table[j] is None:
table[j] = entry
return
found = j
break

if found is not None:
break

perturb >>= PERTURB_SHIFT
i = (i * 5 + 1 + perturb) & mask

table[found] = entry
self._fill += 1
self._used += 1

def intersection(self, other: Iterable) -> "_CompatSetInner":
"""Return a new inner set representing the intersection with another."""
res = _CompatSetInner()
Expand Down Expand Up @@ -406,7 +414,6 @@ def merge(self, other: "_CompatSetInner"):
for entry in other._table:
if entry is not None and entry is not REMOVED:
self.insert_entry_clean(entry)
self._used = other._used
return

# We can't assure there are no duplicates, so do normal insertions
Expand Down Expand Up @@ -444,18 +451,17 @@ def pop(self):

def resize(self, min_used: int):
"""Adjust the allocated size of the set."""
old_fill = self._fill
old_table = self._table
assert min_used >= 0

new_size = MIN_SIZE
while new_size <= min_used:
new_size <<= 1
new_size *= 2

self._table = [None] * new_size
self._fill = old_fill
self._fill = 0
self._mask = new_size - 1
self._used = old_fill
self._used = 0

for entry in old_table:
if entry is not None and entry is not REMOVED:
Expand Down

0 comments on commit 2ca82ec

Please sign in to comment.