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)
