11#pragma once
2-
2+ # include < list >
33#include " VComparator.h"
44#include " UniversalHash.h"
55#include " LinkedHashNode.h"
66
77
88// Hash map class template
9- template <typename K, typename V, size_t tableSize >
9+ template <typename K, typename V>
1010class HashMap
1111{
1212private:
1313 HashMap (const HashMap& other);
1414 const HashMap& operator =(const HashMap& other);
15- HashNode<K, V>* table[tableSize] ;
15+ // std::vector< HashNode<K, V>*> * table;
1616 KeyHash<K> hashFunc;
17+ HashNode<K, V>** table; // int *ptr;
1718 VComparator<K> comp;
19+ size_t tableSize;
1820
1921public:
20- HashMap () :
21- table (),
22+ HashMap (const size_t tableSize) :
2223 hashFunc (tableSize),
2324 comp ()
2425 {
26+ this ->tableSize = tableSize;
27+ table = new HashNode<K, V>*[tableSize]();// (HashNode<K, V>*) malloc(tableSize*sizeof(HashNode<K, V>)); //ptr = (int*) malloc(5*sizeof(int));
2528 }
26-
27- ~HashMap ()
28- {
29- // destroy all buckets one by one
30- for (size_t i = 0 ; i < tableSize; ++i) {
31- HashNode<K, V>* entry = table[i];
32-
33- while (entry != NULL ) {
34- HashNode<K, V>* prev = entry;
35- entry = entry->getNext ();
36- delete prev;
37- }
38-
39- table[i] = NULL ;
40- }
41- }
42-
43- bool search (const K& key, V& value)
44- {
45- unsigned long hashValue = hashFunc[key];
46- HashNode<K, V>* entry = table[hashValue];
47- unsigned int ccounter = 0 ;
48- while (entry != NULL ) {
49- ccounter++;
50- // std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
51- if (comp.compare (entry->getKey (), key)) {
52- value = entry->getValue ();
53- // std::cout << "Number of tries: "<< ccounter <<std::endl;
54- return true ;
55- }
56-
57- entry = entry->getNext ();
58- }
59- // std::cout << "Number of tries: "<< ccounter <<std::endl;
60- // std::cout << "UKNOWN KEY!"<< std::endl;
61- return false ;
62- }
63-
6429 void insert (const K& key, const V& value)
6530 {
6631 unsigned long hashValue = hashFunc[key];
6732 HashNode<K, V>* prev = NULL ;
6833 HashNode<K, V>* entry = table[hashValue];
34+ // std::cout << "before while" << &entry <<std::endl;
6935 while (entry != NULL && !comp.compare (entry->getKey (), key)) {
36+ // std::cout << "entry != NULL" << std::endl;
7037 prev = entry;
7138 entry = entry->getNext ();
7239 }
7340
7441 if (entry == NULL ) {
42+ // std::cout << "entry == NULL" << std::endl;
7543 entry = new HashNode<K, V>(key, value);
7644
7745 if (prev == NULL ) {
@@ -89,34 +57,67 @@ class HashMap
8957 entry->setValue (value);
9058 }
9159 }
92-
93- void remove (const K& key)
60+ bool search (const K& key, V& value)
9461 {
95- unsigned long hashValue = hashFunc[key];
96- HashNode<K, V>* prev = NULL ;
97- HashNode<K, V>* entry = table[hashValue];
98-
99- while (entry != NULL && !comp.compare (entry->getKey (), key)) {
100- prev = entry;
101- entry = entry->getNext ();
102- }
103-
104- if (entry == NULL ) {
105- // key not found
106- return ;
107-
62+ unsigned long hashValue = hashFunc[key];
63+ HashNode<K, V>* entry = table[hashValue];
64+ unsigned int ccounter = 0 ;
65+ while (entry != NULL ) {
66+ ccounter++;
67+ // std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
68+ if (comp.compare (entry->getKey (), key)) {
69+ value = entry->getValue ();
70+ // std::cout << "Number of tries: "<< ccounter <<std::endl;
71+ return true ;
10872 }
109- else {
110- if (prev == NULL ) {
111- // remove first bucket of the list
112- table[hashValue] = entry->getNext ();
11373
114- }
115- else {
116- prev->setNext (entry->getNext ());
117- }
74+ entry = entry->getNext ();
75+ }
76+ // std::cout << "Number of tries: "<< ccounter <<std::endl;
77+ // std::cout << "UKNOWN KEY!"<< std::endl;
78+ return false ;
79+ }
11880
119- delete entry;
120- }
81+ void remove (const K& key)
82+ {
83+ unsigned long hashValue = hashFunc[key];
84+ HashNode<K, V>* prev = NULL ;
85+ HashNode<K, V>* entry = table[hashValue];
86+
87+ while (entry != NULL && !comp.compare (entry->getKey (), key)) {
88+ prev = entry;
89+ entry = entry->getNext ();
90+ }
91+
92+ if (entry == NULL ) {
93+ // key not found
94+ return ;
95+
96+ }
97+ else {
98+ if (prev == NULL ) {
99+ // remove first bucket of the list
100+ table[hashValue] = entry->getNext ();
101+
102+ }
103+ else {
104+ prev->setNext (entry->getNext ());
105+ }
106+
107+ delete entry;
108+ }
109+ }
110+ void displayHash () {
111+ HashNode<K,V>* entry = NULL ;
112+ for (int i = 0 ; i < tableSize; i++) {
113+ entry = table[i];
114+ std::cout << i;
115+ while (entry != NULL ) {
116+ std::cout << " -> " << entry->getKey ();
117+ entry = entry->getNext ();
118+ }
119+ std::cout << std::endl;
121120 }
121+ }
122+
122123};
0 commit comments