Download Latest Version class Hashmap.zip (14.9 kB)
Email in envelope

Get an email when there's a new version of Class-Hashmap

Home / class.hashmap
Name Modified Size InfoDownloads / Week
Parent folder
class.HashMap.pdf 2023-03-12 17.0 kB
class Hashmap.tar.gz 2023-03-12 14.7 kB
class Hashmap.zip 2023-03-12 14.9 kB
README.md 2023-03-12 2.9 kB
Totals: 4 Items   49.4 kB 0

class HashMap: def init(self): self.size = 16 self.map = [None] * self.size

def _get_hash(self, key):
    return hash(key) % self.size

def add(self, key, value):
    key_hash = self._get_hash(key)
    key_value = [key, value]

    if self.map[key_hash] is None:
        self.map[key_hash] = list([key_value])
        return True
    else:
        for pair in self.map[key_hash]:
            if pair[0] == key:
                pair[1] = value
                return True
        self.map[key_hash].append(key_value)
        return True

def get(self, key):
    key_hash = self._get_hash(key)
    if self.map[key_hash] is not None:
        for pair in self.map[key_hash]:
            if pair[0] == key:
                return pair[1]
    return None

def delete(self, key):
    key_hash = self._get_hash(key)
    if self.map[key_hash] is None:
        return False
    for i in range(0, len(self.map[key_hash])):
        if self.map[key_hash][i][0] == key:
            self.map[key_hash].pop(i)
            return True
    return False

def print(self):
    for item in self.map:
        if item is not None:
            print(str(item))

This implementation uses a simple hash function that takes the modulus of the hash value with the size of the hashmap to get the index of the bucket where the key-value pair should be stored. It handles collisions by using a linked list to store multiple key-value pairs in the same bucket.

The add method takes a key-value pair and adds it to the hashmap. If the bucket at the index returned by the hash function is empty, it creates a new linked list with the key-value pair. If the bucket is not empty, it checks if the key already exists in the linked list, and updates the value if it does. If the key does not exist in the linked list, it adds the key-value pair to the end of the linked list.

The get method takes a key and returns the corresponding value from the hashmap. If the bucket at the index returned by the hash function is not empty, it iterates over the linked list to find the key-value pair with the matching key, and returns its value. If the key is not found in the linked list, it returns None.

The delete method takes a key and removes the corresponding key-value pair from the hashmap. If the bucket at the index returned by the hash function is not empty, it iterates over the linked list to find the key-value pair with the matching key, and removes it. If the key is not found in the linked list, it returns False.

The print method is a utility method that prints the contents of the hashmap. It iterates over each bucket and prints any non-empty linked lists.

Source : apunkagames

Source: README.md, updated 2023-03-12