11#pragma once
22
3+
34#include " KeyHash.h"
45#include " HashNode.h"
5-
6+ # include " VComparator.h "
67
78// Hash map class template
8- template <typename K, typename V, size_t tableSize, typename F = KeyHash<K, tableSize> >
9+ template <typename K, typename V, size_t tableSize>
910class HashMap
1011{
1112private:
1213 HashMap (const HashMap& other);
1314 const HashMap& operator =(const HashMap& other);
14- // hash table
1515 HashNode<K, V>* table[tableSize];
16- F hashFunc;
16+ KeyHash<K> hashFunc;
17+ VComparator<K> comp;
1718
1819public:
1920 HashMap () :
2021 table (),
21- hashFunc ()
22+ hashFunc (tableSize),
23+ comp ()
2224 {
2325 }
2426
@@ -38,30 +40,33 @@ class HashMap
3840 }
3941 }
4042
41- bool get (const K& key, V& value)
43+ bool search (const K& key, V& value)
4244 {
43- unsigned long hashValue = hashFunc ( key) ;
45+ unsigned long hashValue = hashFunc[ key] ;
4446 HashNode<K, V>* entry = table[hashValue];
45-
47+ unsigned int ccounter = 0 ;
4648 while (entry != NULL ) {
47- if (entry->getKey () == key) {
49+ ccounter++;
50+ // std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
51+ if (comp.compare (entry->getKey (), key)) {
4852 value = entry->getValue ();
53+ std::cout << " Number of tries: " << ccounter <<std::endl;
4954 return true ;
5055 }
5156
5257 entry = entry->getNext ();
5358 }
54-
59+ std::cout << " Number of tries: " << ccounter <<std::endl;
60+ std::cout << " UKNOWN KEY!" << std::endl;
5561 return false ;
5662 }
5763
58- void put (const K& key, const V& value)
64+ void insert (const K& key, const V& value)
5965 {
60- unsigned long hashValue = hashFunc ( key) ;
66+ unsigned long hashValue = hashFunc[ key] ;
6167 HashNode<K, V>* prev = NULL ;
6268 HashNode<K, V>* entry = table[hashValue];
63-
64- while (entry != NULL && entry->getKey () != key) {
69+ while (entry != NULL && !comp.compare (entry->getKey (), key)) {
6570 prev = entry;
6671 entry = entry->getNext ();
6772 }
@@ -87,11 +92,11 @@ class HashMap
8792
8893 void remove (const K& key)
8994 {
90- unsigned long hashValue = hashFunc ( key) ;
95+ unsigned long hashValue = hashFunc[ key] ;
9196 HashNode<K, V>* prev = NULL ;
9297 HashNode<K, V>* entry = table[hashValue];
9398
94- while (entry != NULL && entry->getKey () != key) {
99+ while (entry != NULL && !comp. compare ( entry->getKey (), key) ) {
95100 prev = entry;
96101 entry = entry->getNext ();
97102 }
@@ -114,4 +119,4 @@ class HashMap
114119 delete entry;
115120 }
116121 }
117- };
122+ };
0 commit comments