forked from UNN/2026-rff_mp
feat: implement hash table
This commit is contained in:
parent
a741e56d3c
commit
0a2e237e5b
34
lomakinae/docs/data/01/src/ht.py
Normal file
34
lomakinae/docs/data/01/src/ht.py
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
from ll import ll_insert, ll_find, ll_delete, ll_list_all
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_SIZE = 128
|
||||||
|
|
||||||
|
|
||||||
|
def ht_new(size=DEFAULT_SIZE):
|
||||||
|
return [None] * size
|
||||||
|
|
||||||
|
|
||||||
|
def _ht_index(buckets, name):
|
||||||
|
return hash(name) % len(buckets)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_insert(buckets, name, phone):
|
||||||
|
i = _ht_index(buckets, name)
|
||||||
|
buckets[i] = ll_insert(buckets[i], name, phone)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_find(buckets, name):
|
||||||
|
i = _ht_index(buckets, name)
|
||||||
|
return ll_find(buckets[i], name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_delete(buckets, name):
|
||||||
|
i = _ht_index(buckets, name)
|
||||||
|
buckets[i] = ll_delete(buckets[i], name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_list_all(buckets):
|
||||||
|
records = []
|
||||||
|
for head in buckets:
|
||||||
|
records.extend(ll_list_all(head))
|
||||||
|
return sorted(records)
|
||||||
Loading…
Reference in New Issue
Block a user