
from src.linked_list_phonebook import ll_insert, ll_find, ll_delete, ll_list_all

def hash_function(name, table_size):
    hash_value = 0
    for char in name:
        hash_value = (hash_value * 31 + ord(char)) % table_size
    return hash_value

def create_hash_table(size=1000):
    return [None] * size

def ht_insert(buckets, name, phone):
    index = hash_function(name, len(buckets))
    buckets[index] = ll_insert(buckets[index], name, phone)
    return buckets

def ht_find(buckets, name):
    index = hash_function(name, len(buckets))
    return ll_find(buckets[index], name)

def ht_delete(buckets, name):
    index = hash_function(name, len(buckets))
    buckets[index] = ll_delete(buckets[index], name)
    return buckets

def ht_list_all(buckets):
    all_records = []
    for bucket in buckets:
        if bucket is not None:
            records = ll_list_all(bucket)
            all_records.extend(records)
    
    all_records.sort(key=lambda x: x[0])
    return all_records