Created
November 26, 2021 19:30
-
-
Save kelvingakuo/989b9e2a32c5c8644ad062a6c7610c77 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 HashIndex(object): | |
def __init__(self, data: List, col: str) -> None: | |
""" Init the hash index class | |
Params: | |
data (list) - Our data as a list of dicts | |
col (str) - The column (dict key) to create an index on | |
""" | |
self.table = data | |
self.on = col | |
self.create_hash_index() | |
def hash_function(self, key: int) -> tuple: | |
""" A hash function that generates a simple hash of our key, and a bucket value | |
Params: | |
key (int)- The value of the column we want to hash | |
Returns: | |
a_hash (int) - A hash of the key | |
bucket_no (int) - The bucket number to insert the value | |
""" | |
buckets = 97 # An arbitrary number of buckets | |
# Generate a hash (square the number twice then extract 4 chars ) | |
keyy = key ** 2 ** 2 | |
a_hash = int(str(keyy)[1:5]) | |
# Get the bucket number | |
bucket_no = a_hash % buckets | |
return a_hash, bucket_no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment