13 template <
typename KeyType,
typename ValueType>
49 Entry*
findEntry(
int bucket,
const KeyType& key)
const;
55 Entry*
findEntry(
int bucket,
const KeyType& key, Entry*& parent)
const;
80 void put(
const KeyType& key,
const ValueType& value);
86 ValueType
get(
const KeyType& key)
const;
163 return !(*
this == rhs);
195 template <
typename KeyType,
typename ValueType>
197 if (nBuckets == 0) nBuckets = 1;
198 buckets = std::vector<Entry*>(nBuckets,
nullptr);
199 this->nBuckets = nBuckets;
203 template <
typename KeyType,
typename ValueType>
205 for (
int i = 0; i < buckets.size(); i++) {
206 Entry* current = buckets[i];
207 while (current !=
nullptr) {
212 buckets[i] =
nullptr;
216 template <
typename KeyType,
typename ValueType>
218 std::vector<Entry*> oldBuckets = buckets;
219 createBuckets(oldBuckets.size() * 2 + 1);
220 for (
int i = 0; i < oldBuckets.size(); i++) {
221 for (
Entry* current = oldBuckets[i]; current !=
nullptr; current = current->
next) {
222 put(current->key, current->value);
225 deleteBuckets(oldBuckets);
228 template <
typename KeyType,
typename ValueType>
232 return findEntry(bucket, key, parent);
235 template <
typename KeyType,
typename ValueType>
239 Entry* current = buckets.at(bucket);
240 while (current !=
nullptr && key != current->
key) {
242 current = current->
next;
247 template <
typename KeyType,
typename ValueType>
250 for (
int i = 0; i < src.
nBuckets; i++) {
251 for (
Entry* current = src.
buckets.at(i); current !=
nullptr; current = current->next) {
252 put(current->key, current->value);
257 template <
typename KeyType,
typename ValueType>
262 template <
typename KeyType,
typename ValueType>
264 deleteBuckets(buckets);
267 template <
typename KeyType,
typename ValueType>
272 template <
typename KeyType,
typename ValueType>
281 template <
typename KeyType,
typename ValueType>
286 template <
typename KeyType,
typename ValueType>
291 template <
typename KeyType,
typename ValueType>
293 (*this)[key] = value;
296 template <
typename KeyType,
typename ValueType>
299 if (current ==
nullptr)
return ValueType();
300 return current->
value;
303 template <
typename KeyType,
typename ValueType>
305 return findEntry(
hashCode(key) % nBuckets, key) !=
nullptr;
308 template <
typename KeyType,
typename ValueType>
310 int bucket =
hashCode(key) % nBuckets;
312 Entry* current = findEntry(bucket, key, parent);
313 if (current !=
nullptr) {
314 if (parent ==
nullptr) {
315 buckets[bucket] = current->
next;
325 template <
typename KeyType,
typename ValueType>
327 deleteBuckets(buckets);
331 template <
typename KeyType,
typename ValueType>
333 int bucket =
hashCode(key) % nBuckets;
334 Entry* current = findEntry(bucket, key);
335 if (current ==
nullptr) {
342 current->
value = ValueType();
343 current->
next = buckets[bucket];
344 buckets[bucket] = current;
347 return current->
value;
350 template <
typename KeyType,
typename ValueType>
This file exports the constants used in throughout the project.
constexpr auto MAX_LOAD_PERCENTAGE
constexpr auto INITIAL_BUCKET_COUNT
Constants for HashMap.
int hashCode(int key)
Returns a hash code for the specified key, which is always a nonnegative integer.
This file contains prototype for the hashCode functions used in class HashMap.
Iterator support for class HashMap.
Entry * current
Current cell in bucket chain.
int bucket
Index of current bucket
iterator(const iterator &it)
std::forward_iterator_tag iterator_category
const HashMap * map
Pointer to the map
iterator(const HashMap *map, bool end)
bool operator!=(const iterator &rhs)
bool operator==(const iterator &rhs)
This class implements an association between keys and values.
int numEntries
Total number of entries.
Entry * findEntry(int bucket, const KeyType &key) const
Finds a cell in the chain for the specified bucket that matches key.
ValueType & operator[](const KeyType &key)
Selects the value associated with key.
void remove(const KeyType &key)
removes any entry for key from this map.
Entry * findEntry(int bucket, const KeyType &key, Entry *&parent) const
ValueType operator[](const KeyType &key) const
void expandAndRehash()
Increases the buckets in the map and then rehashes all existing entries.
void createBuckets(int nBuckets)
Creates vector of buckets to have nBuckets entries, each NULL.
void deleteBuckets(std::vector< Entry * > &buckets)
Deletes all the cells in the linked lists contained in vector.
int nBuckets
Total number of buckets.
HashMap(const HashMap &src)
This copy constructor is to make a deep copy of HashMap.
ValueType get(const KeyType &key) const
void deepCopy(const HashMap &src)
Creates deep copy of src HashMap.
HashMap & operator=(const HashMap &src)
This operator= are defined assign from one map to another.
void clear()
Removes all entries from this map.
std::vector< Entry * > buckets
Vector of pointer to Entry.
void put(const KeyType &key, const ValueType &value)
Associates key with value in this map.
bool containsKey(const KeyType &key) const
Type definition for each cells in the bucket chain.