11#pragma once
2- #include < list>
3- #include " VComparator.h"
4- #include " UniversalHash.h"
52#include " ChainedHashNode.h"
6-
3+ #include " UniversalHash.h"
4+ #include " VComparator.h"
5+ #include < list>
76
87// Hash map class template
9- template <typename K, typename V>
10- class HashMap
11- {
8+ template <typename K, typename V> class HashMap {
129private:
13- HashMap (const HashMap& other);
14- const HashMap& operator =(const HashMap& other);
15- // std::vector<HashNode<K, V>*> *table;
16- KeyHash<K> hashFunc;
17- HashNode<K, V>** table; // int *ptr;
18- VComparator<K> comp;
19- size_t tableSize;
10+ HashMap (const HashMap & other);
11+ const HashMap & operator =(const HashMap & other);
12+ // std::vector<HashNode<K, V>*> *table;
13+ KeyHash<K> hashFunc;
14+ HashNode<K, V> ** table; // int *ptr;
15+ VComparator<K> comp;
16+ size_t tableSize;
2017
2118public:
22- HashMap (const size_t tableSize) :
23- hashFunc (tableSize),
24- comp ()
25- {
26- this ->tableSize = tableSize;
27- table = new HashNode<K, V>*[tableSize]();
19+ HashMap (const size_t tableSize) : hashFunc(tableSize), comp() {
20+ this ->tableSize = tableSize;
21+ table = new HashNode<K, V> *[tableSize]();
22+ }
23+ bool insert (const K &key, const V &value) {
24+ unsigned int hashValue = hashFunc[key];
25+ HashNode<K, V> *prev = NULL ;
26+ HashNode<K, V> *entry = table[hashValue];
27+ // std::cout << "before while" << &entry <<std::endl;
28+ while (entry != NULL && !comp.compare (entry->getKey (), key)) {
29+ // std::cout << "entry != NULL" << std::endl;
30+ prev = entry;
31+ entry = entry->getNext ();
2832 }
29- bool insert (const K& key, const V& value)
30- {
31- unsigned int hashValue = hashFunc[key];
32- HashNode<K, V>* prev = NULL ;
33- HashNode<K, V>* entry = table[hashValue];
34- // std::cout << "before while" << &entry <<std::endl;
35- while (entry != NULL && !comp.compare (entry->getKey (), key)) {
36- // std::cout << "entry != NULL" << std::endl;
37- prev = entry;
38- entry = entry->getNext ();
39- }
4033
41- if (entry == NULL ) {
42- // std::cout << "entry == NULL" << std::endl;
43- entry = new HashNode<K, V>(key, value);
34+ if (entry == NULL ) {
35+ // std::cout << "entry == NULL" << std::endl;
36+ entry = new HashNode<K, V>(key, value);
4437
45- if (prev == NULL ) {
46- // insert as first bucket
47- table[hashValue] = entry;
48- return true ;
38+ if (prev == NULL ) {
39+ // insert as first bucket
40+ table[hashValue] = entry;
41+ return true ;
4942
50- }
51- else {
52- prev->setNext (entry);
53- return true ;
54- }
43+ } else {
44+ prev->setNext (entry);
45+ return true ;
46+ }
5547
56- }
57- else {
58- // just update the value
59- entry->setValue (value);
60- return true ;
61- }
62- return false ;
48+ } else {
49+ // just update the value
50+ entry->setValue (value);
51+ return true ;
6352 }
64- bool search (const K& key, V& value)
65- {
53+ return false ;
54+ }
55+ bool search (const K &key, V &value) {
6656 unsigned int hashValue = hashFunc[key];
67- HashNode<K, V>* entry = table[hashValue];
57+ HashNode<K, V> * entry = table[hashValue];
6858 unsigned int ccounter = 0 ;
6959 while (entry != NULL ) {
70- ccounter++;
71- // std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
72- if (comp.compare (entry->getKey (), key)) {
73- value = entry->getValue ();
74- // std::cout << "Number of tries: "<< ccounter <<std::endl;
75- return true ;
76- }
60+ ccounter++;
61+ // std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue()
62+ // <<std::endl;
63+ if (comp.compare (entry->getKey (), key)) {
64+ value = entry->getValue ();
65+ // std::cout << "Number of tries: "<< ccounter <<std::endl;
66+ return true ;
67+ }
7768
78- entry = entry->getNext ();
69+ entry = entry->getNext ();
7970 }
8071 // std::cout << "Number of tries: "<< ccounter <<std::endl;
8172 // std::cout << "UKNOWN KEY!"<< std::endl;
8273 return false ;
83- }
84-
85- void remove (const K& key)
86- {
87- unsigned int hashValue = hashFunc[key];
88- HashNode<K, V>* prev = NULL ;
89- HashNode<K, V>* entry = table[hashValue];
74+ }
9075
91- while (entry != NULL && !comp. compare (entry-> getKey (), key) ) {
92- prev = entry ;
93- entry = entry-> getNext () ;
94- }
76+ void remove ( const K & key) {
77+ unsigned int hashValue = hashFunc[key] ;
78+ HashNode<K, V> *prev = NULL ;
79+ HashNode<K, V> *entry = table[hashValue];
9580
96- if (entry == NULL ) {
97- // key not found
98- return ;
81+ while (entry != NULL && !comp.compare (entry->getKey (), key)) {
82+ prev = entry;
83+ entry = entry->getNext ();
84+ }
9985
100- }
101- else {
102- if (prev == NULL ) {
103- // remove first bucket of the list
104- table[hashValue] = entry->getNext ();
86+ if (entry == NULL ) {
87+ // key not found
88+ return ;
10589
106- }
107- else {
108- prev-> setNext (entry-> getNext ());
109- }
90+ } else {
91+ if (prev == NULL ) {
92+ // remove first bucket of the list
93+ table[hashValue] = entry-> getNext ();
11094
111- delete entry;
95+ } else {
96+ prev->setNext (entry->getNext ());
11297 }
98+
99+ delete entry;
100+ }
113101 }
114102 // void displayHash() {
115103 // HashNode<K,V>* entry = NULL;
@@ -123,5 +111,4 @@ void remove(const K& key)
123111 // std::cout << std::endl;
124112 // }
125113 // }
126-
127114};
0 commit comments