"""class HashTable
"""


class HashTable:
    """
    A hash table for (key, value) 2-tuples

    === Attributes ===
    @param int capacity: total slots available
    @param list[list[tuple]] table: contents of table
    @param int collisions: number of collisions
    @param int items: number of items
    """

    def __init__(self, capacity):
        """
        Create a hash table with capacity slots

        @param HashTable self: this hash table
        @param int capacity: number of slots in this table
        @rtype: None
        """
        self.capacity, self.collisions, self.items = capacity, 0, 0
        self.table = [[] for _ in range(self.capacity)]

    def __contains__(self, value):
        """ Return whether HashTable self contains value"

        @param HashTable self: this hash table
        @param object value: value to search for
        @rtype: bool
        """
        pass

    def double(self):
        """
        Double the capacity of this hash table, and re-hash all items.

        @param HashTable self: this hash table
        @rtype: None
        """
        # stats before doubling
        # print("Stats before doubling: {}".format(self.stats()))
        # temporarily save self.table
        # reset items
        # create double-sized table
        # insert old items into new table
        # stats after doubling
        # print("Stats after doubling: {}".format(self.stats()))

    def insert(self, item):
        """
        Insert (key, value) item into HashTable self.

        @param HashTable self: this HashTable
        @param (object, object) item: key/value pair, key is hashable
        @rtype: None
        """
        # find the appropriate bucket
        # insert item if it's not already there
        # update items and collisions
        # if the capacity is high, double it

    def retrieve(self, key):
        """
        Return value corresponding to key, or else raise Exception.

        @param HashTable key: this hash table
        @param object key: hashable key
        @rtype: object
        """
        # get the right bucket
        # get item from bucket
        # raise an error if key not present

    def stats(self):
        """
        Provide statistics.

        @param HashTable self: this hash table
        @rtype: str
        """
        buckets = sum([1 for b in self.table if len(b) > 0])
        max_bucket_length = max([len(b) for b in self.table])
        average = "Average bucket length: {}.\n".format(self.items / buckets)
        ideal = "Density: {}\n".format(self.items / self.capacity)
        collisions = "Collisions: {}\n".format(self.collisions)
        maximum = "Maximum bucket length: {}".format(max_bucket_length)
        return average + ideal + collisions + maximum


if __name__ == '__main__':
    import random
    word_list = open('words').readlines()
    random.shuffle(word_list)
    ht = HashTable(2)
    for j in range(99171):
        ht.insert((word_list[j], hash(word_list[j])))
    print(ht.stats())
    # from time import time
    # for size in [9, 90, 900, 9000, 90000]:
    #     ht = HashTable(2)
    #     list_ = extra_word_list[:size]
    #     for word in range(size):
    #         ht.insert((word, hash(word)))
    #     start = time()
    #     for i in range(20):
    #         x = word_list[i] in ht
    #     print("ht of size {} searches 20 words in {} seconds.".format(size, time() - start))
    #     start = time()
    #     for i in range(20):
    #         x = word_list[i] in list_
    #     print("list of size {} searches 20 words in {} seconds.".format(size, time() - start))
    print(ht.retrieve("centre"))
