Skip to content

Commit

Permalink
Computer properties
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewdalpino committed Oct 12, 2024
1 parent 6f4ec7c commit ba9106f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "PyBloomer"
dynamic = ["version"]
version = "0.0.1"
requires-python = ">= 3.10"
dependencies = [
"numpy>=1.19.5",
Expand Down
12 changes: 8 additions & 4 deletions src/pybloomer/bloom_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ def __init__(self,
self.layers = [np.zeros(layer_size, dtype='bool')]
self.m = layer_size

@property
def num_layers(self) -> int:
return len(self.layers)

@property
def utilization(self) -> float:
"""Return the proportion of bits that are currently set"""
return self.n / self.m

@property
def capacity(self) -> float:
"""Return the proportion of bits that are currently not set"""
return 1.0 - self.utilization()
return 1.0 - self.utilization

@property
def false_positive_rate(self) -> float:
"""Return the probability of recording a false positive"""
return self.utilization() ** self.num_hashes
return self.utilization ** self.num_hashes

def insert(self, token: str) -> None:
"""Insert a token into the filter"""
Expand All @@ -67,7 +71,7 @@ def insert(self, token: str) -> None:

changed = True

if changed and self.false_positive_rate() > self.max_false_positive_rate:
if changed and self.false_positive_rate > self.max_false_positive_rate:
self._add_layer()

def exists(self, token: str) -> bool:
Expand Down Expand Up @@ -116,7 +120,7 @@ def exists_or_insert(self, token: str) -> bool:

exists = False

if not exists and self.false_positive_rate() > self.max_false_positive_rate:
if not exists and self.false_positive_rate > self.max_false_positive_rate:
self._add_layer()

return exists
Expand Down

0 comments on commit ba9106f

Please sign in to comment.