diff --git a/soninrv/docs/data/lab1/phonebook.py b/soninrv/docs/data/lab1/phonebook.py index 89243d4..ae7e231 100644 --- a/soninrv/docs/data/lab1/phonebook.py +++ b/soninrv/docs/data/lab1/phonebook.py @@ -50,4 +50,42 @@ def ll_list_all(head): result.append((node['name'], node['phone'])) node = node['next'] result.sort(key=lambda x: x[0]) + return result + +# 2. ХЕШ-ТАБЛИЦА (цепочки через связный список) + +HT_SIZE = 1024 # число корзин (степень двойки) + + +def ht_create(size=HT_SIZE): + return [None] * size + + +def _ht_hash(name, size): + h = 5381 + for ch in name: + h = ((h << 5) + h) ^ ord(ch) + return h % size + + +def ht_insert(buckets, name, phone): + idx = _ht_hash(name, len(buckets)) + buckets[idx] = ll_insert(buckets[idx], name, phone) + + +def ht_find(buckets, name): + idx = _ht_hash(name, len(buckets)) + return ll_find(buckets[idx], name) + + +def ht_delete(buckets, name): + idx = _ht_hash(name, len(buckets)) + buckets[idx] = ll_delete(buckets[idx], name) + + +def ht_list_all(buckets): + result = [] + for head in buckets: + result.extend(ll_list_all(head)) + result.sort(key=lambda x: x[0]) return result \ No newline at end of file