Skip to content

HashMap

Download Source Files

Specification

IHashMap

Bases: Mapping[KT, VT]

Interface for a HashMap.

Source code in src/datastructures/ihashmap.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class IHashMap(Mapping[KT, VT]):
    """
    Interface for a HashMap.
    """

    @abstractmethod
    def __init__(self, initial_capacity=7, load_factor=0.75, data_type: type=object) -> None:
        """
        Initializes the HashMap with the given initial capacity and load factor.

        Args:
            initial_capacity (int): The initial capacity of the map.
            load_factor (float): The load factor for resizing the map.
            data_type (type): The type of values stored in the map.

        Returns:
            None
        """
        ...

    @abstractmethod
    def __getitem__(self, key: KT) -> VT:
        """
        Retrieves the value associated with the given key.

        Args:
            key (KT): The key to retrieve the value for.

        Returns:
            VT: The value associated with the key.
        """
        ...

    @abstractmethod
    def __setitem__(self, key: KT, value: VT) -> None:
        """
        Sets the value for the given key.

        Args:
            key (KT): The key to set the value for.
            value (VT): The value to set.

        Returns:
            None
        Raises:
            TypeError: If the value is not of the expected type.
            KeyError: If the key already exists in the map.
        """
        ...

    @abstractmethod
    def keys(self):
        """
        Returns an iterable of the `keys` in the map.

        Returns:
            Iterable[KT]: An iterable of the `keys` in the map.
        """
        ...

    @abstractmethod
    def values(self):
        """
        Returns an iterable of the `values` in the map.

        Returns:
            Iterable[VT]: An iterable of the `values` in the map.
        """
        ...

    @abstractmethod
    def items(self):
        """
        Returns an iterable of the `items` in the map.

        Returns:
            Iterable[tuple[KT, VT]]: An iterable of the `items` in the map.
        """
        ...

    @abstractmethod
    def __delitem__(self, key: KT) -> None:
        """
        Deletes the key-value pair associated with the given key.

        Args:
            key (KT): The key to delete.

        Returns:
            None
        Raises:
            KeyError: If the key does not exist in the map.
        """
        ...

    @abstractmethod
    def __contains__(self, key: KT) -> bool:
        """
        Checks if the map contains the specified key.

        Args:
            key (KT): The key to check for existence.

        Returns:
            bool: True if the key exists, False otherwise.
        """
        ...

    @abstractmethod
    def __len__(self) -> int:
        """
        Returns the number of key-value pairs in the map.

        Returns:
            int: The number of key-value pairs in the map.
        """
        ...

    @abstractmethod
    def __iter__(self) -> Iterator[KT]:
        """
        Returns an iterator over the keys in the map.

        Returns:
            Iterator[KT]: An iterator over the keys in the map.
        """
        ...

    @abstractmethod
    def __str__(self) -> str:
        """
        Returns a string representation of the map.

        Returns:
            str: A string representation of the map.
        """
        ...

    @abstractmethod
    def __repr__(self) -> str:
        """
        Returns a string representation of the map for debugging.

        Returns:
            str: A string representation of the map for debugging.
        """
        ...

    @abstractmethod
    def __eq__(self, other: object) -> bool:
        """
        Checks if the map is equal to another object.

        Args:
            other (object): The object to compare with.

        Returns:
            bool: True if the map is equal to the other object, False otherwise.
        """
        ...

    def __hash__(self) -> int:
        """
        This interface also acts as a base class with a hash method override that always raises a TypeError.
        HashMap objects are unhashable because they are mutable so this should always raise a TypeError.

        Raises:
            TypeError: Always raised to indicate that HashMap objects are unhashable.
        """
        raise TypeError("HashMap objects are unhashable")

__contains__(key) abstractmethod

Checks if the map contains the specified key.

Parameters:

Name Type Description Default
key KT

The key to check for existence.

required

Returns:

Name Type Description
bool bool

True if the key exists, False otherwise.

Source code in src/datastructures/ihashmap.py
104
105
106
107
108
109
110
111
112
113
114
115
@abstractmethod
def __contains__(self, key: KT) -> bool:
    """
    Checks if the map contains the specified key.

    Args:
        key (KT): The key to check for existence.

    Returns:
        bool: True if the key exists, False otherwise.
    """
    ...

__delitem__(key) abstractmethod

Deletes the key-value pair associated with the given key.

Parameters:

Name Type Description Default
key KT

The key to delete.

required

Returns:

Type Description
None

None

Raises: KeyError: If the key does not exist in the map.

Source code in src/datastructures/ihashmap.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@abstractmethod
def __delitem__(self, key: KT) -> None:
    """
    Deletes the key-value pair associated with the given key.

    Args:
        key (KT): The key to delete.

    Returns:
        None
    Raises:
        KeyError: If the key does not exist in the map.
    """
    ...

__eq__(other) abstractmethod

Checks if the map is equal to another object.

Parameters:

Name Type Description Default
other object

The object to compare with.

required

Returns:

Name Type Description
bool bool

True if the map is equal to the other object, False otherwise.

Source code in src/datastructures/ihashmap.py
157
158
159
160
161
162
163
164
165
166
167
168
@abstractmethod
def __eq__(self, other: object) -> bool:
    """
    Checks if the map is equal to another object.

    Args:
        other (object): The object to compare with.

    Returns:
        bool: True if the map is equal to the other object, False otherwise.
    """
    ...

__getitem__(key) abstractmethod

Retrieves the value associated with the given key.

Parameters:

Name Type Description Default
key KT

The key to retrieve the value for.

required

Returns:

Name Type Description
VT VT

The value associated with the key.

Source code in src/datastructures/ihashmap.py
29
30
31
32
33
34
35
36
37
38
39
40
@abstractmethod
def __getitem__(self, key: KT) -> VT:
    """
    Retrieves the value associated with the given key.

    Args:
        key (KT): The key to retrieve the value for.

    Returns:
        VT: The value associated with the key.
    """
    ...

__hash__()

This interface also acts as a base class with a hash method override that always raises a TypeError. HashMap objects are unhashable because they are mutable so this should always raise a TypeError.

Raises:

Type Description
TypeError

Always raised to indicate that HashMap objects are unhashable.

Source code in src/datastructures/ihashmap.py
170
171
172
173
174
175
176
177
178
def __hash__(self) -> int:
    """
    This interface also acts as a base class with a hash method override that always raises a TypeError.
    HashMap objects are unhashable because they are mutable so this should always raise a TypeError.

    Raises:
        TypeError: Always raised to indicate that HashMap objects are unhashable.
    """
    raise TypeError("HashMap objects are unhashable")

__init__(initial_capacity=7, load_factor=0.75, data_type=object) abstractmethod

Initializes the HashMap with the given initial capacity and load factor.

Parameters:

Name Type Description Default
initial_capacity int

The initial capacity of the map.

7
load_factor float

The load factor for resizing the map.

0.75
data_type type

The type of values stored in the map.

object

Returns:

Type Description
None

None

Source code in src/datastructures/ihashmap.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@abstractmethod
def __init__(self, initial_capacity=7, load_factor=0.75, data_type: type=object) -> None:
    """
    Initializes the HashMap with the given initial capacity and load factor.

    Args:
        initial_capacity (int): The initial capacity of the map.
        load_factor (float): The load factor for resizing the map.
        data_type (type): The type of values stored in the map.

    Returns:
        None
    """
    ...

__iter__() abstractmethod

Returns an iterator over the keys in the map.

Returns:

Type Description
Iterator[KT]

Iterator[KT]: An iterator over the keys in the map.

Source code in src/datastructures/ihashmap.py
127
128
129
130
131
132
133
134
135
@abstractmethod
def __iter__(self) -> Iterator[KT]:
    """
    Returns an iterator over the keys in the map.

    Returns:
        Iterator[KT]: An iterator over the keys in the map.
    """
    ...

__len__() abstractmethod

Returns the number of key-value pairs in the map.

Returns:

Name Type Description
int int

The number of key-value pairs in the map.

Source code in src/datastructures/ihashmap.py
117
118
119
120
121
122
123
124
125
@abstractmethod
def __len__(self) -> int:
    """
    Returns the number of key-value pairs in the map.

    Returns:
        int: The number of key-value pairs in the map.
    """
    ...

__repr__() abstractmethod

Returns a string representation of the map for debugging.

Returns:

Name Type Description
str str

A string representation of the map for debugging.

Source code in src/datastructures/ihashmap.py
147
148
149
150
151
152
153
154
155
@abstractmethod
def __repr__(self) -> str:
    """
    Returns a string representation of the map for debugging.

    Returns:
        str: A string representation of the map for debugging.
    """
    ...

__setitem__(key, value) abstractmethod

Sets the value for the given key.

Parameters:

Name Type Description Default
key KT

The key to set the value for.

required
value VT

The value to set.

required

Returns:

Type Description
None

None

Raises: TypeError: If the value is not of the expected type. KeyError: If the key already exists in the map.

Source code in src/datastructures/ihashmap.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@abstractmethod
def __setitem__(self, key: KT, value: VT) -> None:
    """
    Sets the value for the given key.

    Args:
        key (KT): The key to set the value for.
        value (VT): The value to set.

    Returns:
        None
    Raises:
        TypeError: If the value is not of the expected type.
        KeyError: If the key already exists in the map.
    """
    ...

__str__() abstractmethod

Returns a string representation of the map.

Returns:

Name Type Description
str str

A string representation of the map.

Source code in src/datastructures/ihashmap.py
137
138
139
140
141
142
143
144
145
@abstractmethod
def __str__(self) -> str:
    """
    Returns a string representation of the map.

    Returns:
        str: A string representation of the map.
    """
    ...

items() abstractmethod

Returns an iterable of the items in the map.

Returns:

Type Description

Iterable[tuple[KT, VT]]: An iterable of the items in the map.

Source code in src/datastructures/ihashmap.py
79
80
81
82
83
84
85
86
87
@abstractmethod
def items(self):
    """
    Returns an iterable of the `items` in the map.

    Returns:
        Iterable[tuple[KT, VT]]: An iterable of the `items` in the map.
    """
    ...

keys() abstractmethod

Returns an iterable of the keys in the map.

Returns:

Type Description

Iterable[KT]: An iterable of the keys in the map.

Source code in src/datastructures/ihashmap.py
59
60
61
62
63
64
65
66
67
@abstractmethod
def keys(self):
    """
    Returns an iterable of the `keys` in the map.

    Returns:
        Iterable[KT]: An iterable of the `keys` in the map.
    """
    ...

values() abstractmethod

Returns an iterable of the values in the map.

Returns:

Type Description

Iterable[VT]: An iterable of the values in the map.

Source code in src/datastructures/ihashmap.py
69
70
71
72
73
74
75
76
77
@abstractmethod
def values(self):
    """
    Returns an iterable of the `values` in the map.

    Returns:
        Iterable[VT]: An iterable of the `values` in the map.
    """
    ...