pyprobables 0.7.0__tar.gz → 0.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyprobables-0.7.0 → pyprobables-0.7.1}/PKG-INFO +1 -1
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/__init__.py +1 -1
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/blooms/countingbloom.py +2 -2
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/cuckoo/countingcuckoo.py +37 -15
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/cuckoo/cuckoo.py +30 -10
- {pyprobables-0.7.0 → pyprobables-0.7.1}/pyprobables.egg-info/PKG-INFO +1 -1
- {pyprobables-0.7.0 → pyprobables-0.7.1}/LICENSE +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/README.rst +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/blooms/__init__.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/blooms/bloom.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/blooms/expandingbloom.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/blooms/py.typed +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/constants.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/countminsketch/__init__.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/countminsketch/countminsketch.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/countminsketch/py.typed +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/cuckoo/__init__.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/cuckoo/py.typed +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/exceptions.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/hashes.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/py.typed +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/quotientfilter/__init__.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/quotientfilter/py.typed +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/quotientfilter/quotientfilter.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/probables/utilities.py +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/pyprobables.egg-info/SOURCES.txt +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/pyprobables.egg-info/dependency_links.txt +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/pyprobables.egg-info/top_level.txt +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/pyproject.toml +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/setup.cfg +0 -0
- {pyprobables-0.7.0 → pyprobables-0.7.1}/tests/test_utilities.py +0 -0
|
@@ -24,7 +24,7 @@ __author__ = "Tyler Barrus"
|
|
|
24
24
|
__maintainer__ = "Tyler Barrus"
|
|
25
25
|
__email__ = "barrust@gmail.com"
|
|
26
26
|
__license__ = "MIT"
|
|
27
|
-
__version__ = "0.7.
|
|
27
|
+
__version__ = "0.7.1"
|
|
28
28
|
__credits__: list[str] = []
|
|
29
29
|
__url__ = "https://github.com/barrust/pyprobables"
|
|
30
30
|
__bugtrack_url__ = "https://github.com/barrust/pyprobables/issues"
|
|
@@ -235,7 +235,7 @@ class CountingBloomFilter(BloomFilter):
|
|
|
235
235
|
for i in range(self.bloom_length):
|
|
236
236
|
if self._bloom[i] > 0 and second._bloom[i] > 0:
|
|
237
237
|
tmp = self._bloom[i] + second._bloom[i]
|
|
238
|
-
res.bloom[i] = tmp
|
|
238
|
+
res.bloom[i] = min(tmp, UINT32_T_MAX)
|
|
239
239
|
res.elements_added = res.estimate_elements()
|
|
240
240
|
return res
|
|
241
241
|
|
|
@@ -295,7 +295,7 @@ class CountingBloomFilter(BloomFilter):
|
|
|
295
295
|
)
|
|
296
296
|
for i in range(self.bloom_length):
|
|
297
297
|
tmp = self._bloom[i] + second._bloom[i]
|
|
298
|
-
res._bloom[i] = tmp
|
|
298
|
+
res._bloom[i] = min(tmp, UINT32_T_MAX)
|
|
299
299
|
res.elements_added = res.estimate_elements()
|
|
300
300
|
return res
|
|
301
301
|
|
|
@@ -232,11 +232,11 @@ class CountingCuckooFilter(CuckooFilter):
|
|
|
232
232
|
) -> Union["CountingCuckooBin", None]:
|
|
233
233
|
"""insert a fingerprint, but with a count parameter!"""
|
|
234
234
|
if self.__insert_element(fingerprint, idx_1, count):
|
|
235
|
-
self._inserted_elements +=
|
|
235
|
+
self._inserted_elements += count
|
|
236
236
|
self.__unique_elements += 1
|
|
237
237
|
return None
|
|
238
238
|
if self.__insert_element(fingerprint, idx_2, count):
|
|
239
|
-
self._inserted_elements +=
|
|
239
|
+
self._inserted_elements += count
|
|
240
240
|
self.__unique_elements += 1
|
|
241
241
|
return None
|
|
242
242
|
|
|
@@ -244,11 +244,14 @@ class CountingCuckooFilter(CuckooFilter):
|
|
|
244
244
|
# and move things around to the other index, if possible, until we
|
|
245
245
|
# either move everything around or hit the maximum number of swaps
|
|
246
246
|
idx = random.choice([idx_1, idx_2])
|
|
247
|
-
prv_bin = CountingCuckooBin(fingerprint,
|
|
247
|
+
prv_bin = CountingCuckooBin(fingerprint, count)
|
|
248
|
+
orig_bin = prv_bin
|
|
249
|
+
swaps = []
|
|
248
250
|
for _ in range(self.max_swaps):
|
|
249
251
|
# select one element to be swapped out...
|
|
250
252
|
swap_elm = random.randint(0, self.bucket_size - 1)
|
|
251
253
|
swap_finger = self.buckets[idx][swap_elm]
|
|
254
|
+
swaps.append((idx, swap_elm, swap_finger))
|
|
252
255
|
prv_bin, self.buckets[idx][swap_elm] = swap_finger, prv_bin
|
|
253
256
|
|
|
254
257
|
# now find another place to put this fingerprint
|
|
@@ -257,12 +260,17 @@ class CountingCuckooFilter(CuckooFilter):
|
|
|
257
260
|
idx = index_2 if idx == index_1 else index_1
|
|
258
261
|
|
|
259
262
|
if self.__insert_element(prv_bin.finger, idx, prv_bin.count):
|
|
260
|
-
self._inserted_elements +=
|
|
263
|
+
self._inserted_elements += count
|
|
261
264
|
self.__unique_elements += 1
|
|
262
265
|
return None
|
|
263
266
|
|
|
267
|
+
# unable to place the new fingerprint; undo the swaps so that the
|
|
268
|
+
# failed insertion leaves the filter unchanged
|
|
269
|
+
for idx, swap_elm, elm in reversed(swaps):
|
|
270
|
+
self.buckets[idx][swap_elm] = elm
|
|
271
|
+
|
|
264
272
|
# if we got here we have an error... we might need to know what is left
|
|
265
|
-
return
|
|
273
|
+
return orig_bin
|
|
266
274
|
|
|
267
275
|
def _check_if_present(self, idx_1: int, idx_2: int, fingerprint: int) -> int | None:
|
|
268
276
|
"""wrapper for checking if fingerprint is already inserted"""
|
|
@@ -304,16 +312,30 @@ class CountingCuckooFilter(CuckooFilter):
|
|
|
304
312
|
|
|
305
313
|
def _expand_logic(self, extra_fingerprint: Union["CountingCuckooBin", None]) -> None:
|
|
306
314
|
"""the logic to acutally expand the cuckoo filter"""
|
|
307
|
-
#
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
315
|
+
# snapshot the current state so that a failed expansion leaves the
|
|
316
|
+
# filter unchanged
|
|
317
|
+
old_capacity = self._cuckoo_capacity
|
|
318
|
+
old_buckets = self._buckets
|
|
319
|
+
old_inserted = self._inserted_elements
|
|
320
|
+
old_unique = self.__unique_elements
|
|
321
|
+
|
|
322
|
+
try:
|
|
323
|
+
# get all the fingerprints
|
|
324
|
+
fingerprints = self._setup_expand(extra_fingerprint)
|
|
325
|
+
self.__unique_elements = 0 # this needs to be reset!
|
|
326
|
+
|
|
327
|
+
for elm in fingerprints:
|
|
328
|
+
idx_1, idx_2 = self._indicies_from_fingerprint(elm.finger)
|
|
329
|
+
res = self._insert_fingerprint_alt(elm.finger, idx_1, idx_2, elm.count)
|
|
330
|
+
if res is not None: # again, this *shouldn't* happen
|
|
331
|
+
msg = "The CountingCuckooFilter failed to expand"
|
|
332
|
+
raise CuckooFilterFullError(msg)
|
|
333
|
+
except Exception:
|
|
334
|
+
self._cuckoo_capacity = old_capacity
|
|
335
|
+
self._buckets = old_buckets
|
|
336
|
+
self._inserted_elements = old_inserted
|
|
337
|
+
self.__unique_elements = old_unique
|
|
338
|
+
raise
|
|
317
339
|
|
|
318
340
|
def __insert_element(self, fingerprint, idx, count=1) -> bool:
|
|
319
341
|
"""insert an element"""
|
|
@@ -371,12 +371,15 @@ class CuckooFilter:
|
|
|
371
371
|
# and move things around to the other index, if possible, until we
|
|
372
372
|
# either move everything around or hit the maximum number of swaps
|
|
373
373
|
idx = random.choice([idx_1, idx_2])
|
|
374
|
+
orig_fingerprint = fingerprint
|
|
375
|
+
swaps = []
|
|
374
376
|
|
|
375
377
|
for _ in range(self.max_swaps):
|
|
376
378
|
# select one element to be swapped out...
|
|
377
379
|
swap_elm = random.randint(0, self.bucket_size - 1)
|
|
378
380
|
|
|
379
381
|
swb = self.buckets[idx][swap_elm]
|
|
382
|
+
swaps.append((idx, swap_elm, swb))
|
|
380
383
|
fingerprint, self.buckets[idx][swap_elm] = swb, fingerprint
|
|
381
384
|
|
|
382
385
|
# now find another place to put this fingerprint
|
|
@@ -388,8 +391,13 @@ class CuckooFilter:
|
|
|
388
391
|
self._inserted_elements += 1
|
|
389
392
|
return None
|
|
390
393
|
|
|
394
|
+
# unable to place the new fingerprint; undo the swaps so that the
|
|
395
|
+
# failed insertion leaves the filter unchanged
|
|
396
|
+
for idx, swap_elm, elm in reversed(swaps):
|
|
397
|
+
self.buckets[idx][swap_elm] = elm
|
|
398
|
+
|
|
391
399
|
# if we got here we have an error... we might need to know what is left
|
|
392
|
-
return
|
|
400
|
+
return orig_fingerprint
|
|
393
401
|
|
|
394
402
|
def _load(self, file: Path | str | IOBase | mmap | bytes) -> None:
|
|
395
403
|
"""load a cuckoo filter from file"""
|
|
@@ -454,15 +462,27 @@ class CuckooFilter:
|
|
|
454
462
|
|
|
455
463
|
def _expand_logic(self, extra_fingerprint):
|
|
456
464
|
"""the logic to acutally expand the cuckoo filter"""
|
|
457
|
-
#
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
465
|
+
# snapshot the current state so that a failed expansion leaves the
|
|
466
|
+
# filter unchanged
|
|
467
|
+
old_capacity = self._cuckoo_capacity
|
|
468
|
+
old_buckets = self._buckets
|
|
469
|
+
old_inserted = self._inserted_elements
|
|
470
|
+
|
|
471
|
+
try:
|
|
472
|
+
# get all the fingerprints
|
|
473
|
+
fingerprints = self._setup_expand(extra_fingerprint)
|
|
474
|
+
|
|
475
|
+
for finger in fingerprints:
|
|
476
|
+
idx_1, idx_2 = self._indicies_from_fingerprint(finger)
|
|
477
|
+
res = self._insert_fingerprint(finger, idx_1, idx_2)
|
|
478
|
+
if res is not None: # again, this *shouldn't* happen
|
|
479
|
+
msg = "The CuckooFilter failed to expand"
|
|
480
|
+
raise CuckooFilterFullError(msg)
|
|
481
|
+
except Exception:
|
|
482
|
+
self._cuckoo_capacity = old_capacity
|
|
483
|
+
self._buckets = old_buckets
|
|
484
|
+
self._inserted_elements = old_inserted
|
|
485
|
+
raise
|
|
466
486
|
|
|
467
487
|
def _setup_expand(self, extra_fingerprint):
|
|
468
488
|
"""setup this thing"""
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|