Skip to content

Commit e86b73c

Browse files
author
VirtualRoyalty
committed
Linked Hash map template refactor+fix
1 parent 15a2927 commit e86b73c

14 files changed

+485
-197
lines changed

algorithm/include/LinkedHashMap.h

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,45 @@
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>
1010
class HashMap
1111
{
1212
private:
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

1921
public:
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
};
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#pragma once
2+
3+
#include "VComparator.h"
4+
#include "UniversalHash.h"
5+
#include "LinkedHashNode.h"
6+
7+
8+
// Hash map class template
9+
template <typename K, typename V, size_t tableSize>
10+
class HashMap
11+
{
12+
private:
13+
HashMap(const HashMap& other);
14+
const HashMap& operator=(const HashMap& other);
15+
HashNode<K, V>* table[tableSize];
16+
KeyHash<K> hashFunc;
17+
VComparator<K> comp;
18+
19+
public:
20+
HashMap():
21+
table(),
22+
hashFunc(tableSize),
23+
comp()
24+
{
25+
}
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+
64+
void insert(const K& key, const V& value)
65+
{
66+
unsigned long hashValue = hashFunc[key];
67+
HashNode<K, V>* prev = NULL;
68+
HashNode<K, V>* entry = table[hashValue];
69+
// std::cout << "before while" << entry <<std::endl;
70+
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
71+
prev = entry;
72+
entry = entry->getNext();
73+
}
74+
75+
if (entry == NULL) {
76+
entry = new HashNode<K, V>(key, value);
77+
78+
if (prev == NULL) {
79+
// insert as first bucket
80+
table[hashValue] = entry;
81+
82+
}
83+
else {
84+
prev->setNext(entry);
85+
}
86+
87+
}
88+
else {
89+
// just update the value
90+
entry->setValue(value);
91+
}
92+
}
93+
94+
void remove(const K& key)
95+
{
96+
unsigned long hashValue = hashFunc[key];
97+
HashNode<K, V>* prev = NULL;
98+
HashNode<K, V>* entry = table[hashValue];
99+
100+
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
101+
prev = entry;
102+
entry = entry->getNext();
103+
}
104+
105+
if (entry == NULL) {
106+
// key not found
107+
return;
108+
109+
}
110+
else {
111+
if (prev == NULL) {
112+
// remove first bucket of the list
113+
table[hashValue] = entry->getNext();
114+
115+
}
116+
else {
117+
prev->setNext(entry->getNext());
118+
}
119+
120+
delete entry;
121+
}
122+
}
123+
};

algorithm/include/LinkedHashNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class HashNode
1414
// next bucket with the same key
1515
HashNode* _next;
1616
// disallow copy and assignment
17-
HashNode(const HashNode&);
18-
HashNode& operator=(const HashNode&);
17+
// HashNode(const HashNode&);
18+
// HashNode& operator=(const HashNode&);
1919

2020
public:
2121
HashNode(const K& key, const V& value) :

0 commit comments

Comments
 (0)