Hashmaps

A hashmap is a list of multiple groups of data, each located by a unique hash number, generated by a hash function. In Python, the dict() structure creates this behaviour.

Each space in the vector where the information is stored is called a bucket:

Untitled

To index a file, we provide the hash function with the insertion id and it will return the exact position of the bucket in the vector.

Structure of a simple HashMap

With this structure we create a HashMap with the positions ranging from 0 to 9, where each position is a hash.

class HashMap:
    def __init__(self):
        self._buckets = [None for i in range(10)]

    def get_address(self, id_num):
        return id_num % 10

    def insert(self, employee):
        address = self.get_address(employee.id_num)
        self._buckets[address] = employee

    def get_value(self, id_num):
        address = self.get_address(id_num)
        return self._buckets[address].name

Separate Chain

The problem of the simple example above is the value collision. Since we have a limited amount of 10 positions, different ids can result in the same hash meaning that it will override the current value.

One possible solution is Separate Chain, where multiple values are stored within the same hash position creating a list.

This would create yet another complexity problem when indexing the list within the hash, but the complexity is still lower than it would be in a normal list.

Sets

Set is yet another list structure that is optimized for both space and order complexity but it is restricted in some aspects if compared to lists or dicts.

new_set = {1, 2, 3, 'str'}

Operations