hashtbl

This library was created to simplify the concept of a hash table and explain how it works in the background in a clear and simple way, especially for beginners in data structures.
Installation
You can install hashtbl via pip:
pip install hashtbl
Usage
from hashtbl import hashMap
x = hashMap(
[
["key1", "value1"],
["key2", "value2"],
["key3", "value3"],
]
)
print(x)
Output
{"key1": "value1", "key2": "value2", "key3": "value3"}
You Can Show All Details
from hashtbl import hashMap
x = hashMap(
[
["key1", "value1"],
["key2", "value2"],
["key3", "value3"],
],
detail=True,
)
print(x)
Output
Hash function :
_____________
f(x) = ord(x) % N (Hash table capacity)
Example :
_______
N = 26
f("ABC") = ord("ABC") % 26 = (ord('A') + ord('B') + ord('C')) % 26 = (65 + 66 + 67) % 26 = 198 % 26 = 16
The value associated with the key "ABC" will be placed at index 16 in the hash table (array) with a capacity of 26.
Notes :
_____
- If a key has the same index as an existing key in the hash table, it will be placed after it because, in a hash table, each index is a linked list.
- If a key is duplicated in the hash table, the last value associated with this key will be saved.
Hash Table :
__________
ββββββββββ€βββββββββββββββββββββββββββββββββ€βββββββββββ
β Key β Hash function Output (Index) β Value β
ββββββββββͺβββββββββββββββββββββββββββββββββͺβββββββββββ‘
β "key1" β 14 β "value1" β
ββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββ€
β "key2" β 15 β "value2" β
ββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββ€
β "key3" β 16 β "value3" β
ββββββββββ§βββββββββββββββββββββββββββββββββ§βββββββββββ
Advanced Usage
You Can See The Key/Value Pairs In The Linked List If There Is More Than One Key In One Index (Hash Function Output).
from hashtbl import hashMap
x = hashMap(
[
["algorithm", "algo"],
["logarithm", "log"],
],
detail=False,
)
print(x.get_linked_list(5))
Output
[('algorithm', 'algo')] -> [('logarithm', 'log')] -> None (NULL)
You Can See It With All Details
from hashtbl import hashMap
x = hashMap(
[
["algorithm", "algo"],
["logarithm", "log"],
],
detail=True,
)
print(x.get_linked_list(5))
Output
βββββββββββββββββββββββββ€ββββββββββββββββββββββββββ€βββββββββββββββββββββββ€βββββββββββββββββββββββ
β Current Value β Current Value @ddress β Next Value β Next Value @ddress β
βββββββββββββββββββββββββͺββββββββββββββββββββββββββͺβββββββββββββββββββββββͺβββββββββββββββββββββββ‘
β ('algorithm', 'algo') β 0x7f527dd225d0 β ('logarithm', 'log') β 0x7f527dd21d50 β
βββββββββββββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββββββΌβββββββββββββββββββββββ€
β ('logarithm', 'log') β 0x7f527dd21d50 β None (NULL) β 0x95bcc0 (nil/0x0) β
βββββββββββββββββββββββββΌββββββββββββββββββββββββββΌβββββββββββββββββββββββΌβββββββββββββββββββββββ€
β None (NULL) β 0x95bcc0 (nil/0x0) β β β
βββββββββββββββββββββββββ§ββββββββββββββββββββββββββ§βββββββββββββββββββββββ§βββββββββββββββββββββββ
Note
You can use all methods of the built-in hash table (dictionary) with this custom hash table.
License
This project is licensed under the MIT LICENSE - see the LICENSE for more details.