HashMap
Download Source Files
- Download and place the following in your datastructures folder:
- Download and place the following in your tests folder:
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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
__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 | |
items()
abstractmethod
Returns an iterable of the items in the map.
Returns:
| Type | Description |
|---|---|
|
Iterable[tuple[KT, VT]]: An iterable of the |
Source code in src/datastructures/ihashmap.py
79 80 81 82 83 84 85 86 87 | |
keys()
abstractmethod
Returns an iterable of the keys in the map.
Returns:
| Type | Description |
|---|---|
|
Iterable[KT]: An iterable of the |
Source code in src/datastructures/ihashmap.py
59 60 61 62 63 64 65 66 67 | |
values()
abstractmethod
Returns an iterable of the values in the map.
Returns:
| Type | Description |
|---|---|
|
Iterable[VT]: An iterable of the |
Source code in src/datastructures/ihashmap.py
69 70 71 72 73 74 75 76 77 | |