Last active
May 2, 2022 10:35
-
-
Save jku/bccc355d0daea56449375013fa5243c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Succinct: | |
def __init__(self, name_prefix: str, bin_bits: int): | |
self.name_prefix = name_prefix | |
# number of bins | |
bin_count = 2**bin_bits | |
# length of string prefix to get from targetpath hash | |
self.prefix_len = math.ceil(math.log(bin_count, 16)) | |
# how many different prefixes are there? | |
prefix_count = 16**self.prefix_len | |
# how many prefixes per bin | |
self.prefixes_per_bin = prefix_count // bin_count | |
def get_role(self, target_path_hash: str) -> str: | |
# prefix of the hash of the targetpath we are looking for | |
prefix_number = int(target_path_hash[:self.prefix_len], 16) | |
# number of the bin that contains prefix | |
bin_number = prefix_number // self.prefixes_per_bin | |
# form the rolename: bin number is in hex with leading zero | |
return f"{self.name_prefix}-{bin_number:0{self.prefix_len}x}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment