Skip to content

Commit 8ccbea8

Browse files
author
VirtualRoyalty
committed
fix OpenHash + PerformanceLoop added
1 parent e86b73c commit 8ccbea8

File tree

11 files changed

+350
-57
lines changed

11 files changed

+350
-57
lines changed

algorithm/include/LinkedHashMap.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class HashMap
2424
comp()
2525
{
2626
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));
27+
table = new HashNode<K, V>*[tableSize]();
2828
}
29-
void insert(const K& key, const V& value)
29+
bool insert(const K& key, const V& value)
3030
{
3131
unsigned long hashValue = hashFunc[key];
3232
HashNode<K, V>* prev = NULL;
@@ -45,17 +45,21 @@ class HashMap
4545
if (prev == NULL) {
4646
// insert as first bucket
4747
table[hashValue] = entry;
48+
return true;
4849

4950
}
5051
else {
5152
prev->setNext(entry);
53+
return true;
5254
}
5355

5456
}
5557
else {
5658
// just update the value
5759
entry->setValue(value);
60+
return true;
5861
}
62+
return false;
5963
}
6064
bool search(const K& key, V& value)
6165
{

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) :

algorithm/include/OpenedHashMap.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
#pragma once
22

3+
#include "VComparator.h"
34
#include "UniversalHash.h"
45
#include "OpenedHashNode.h"
56

67
const int DELETED = -1;
78

8-
template <typename K, typename V, size_t tableSize>
9+
template <typename K, typename V>
910
class OpenHashMap
1011
{
1112
private:
1213
OpenHashMap(const OpenHashMap& other);
1314
const OpenHashMap& operator=(const OpenHashMap& other);
14-
OpenHashNode<K, V>* table[tableSize];
15-
KeyHash<K> hashFunc;
15+
size_t tableSize;
16+
OpenHashNode<K, V>** table;
1617
VComparator<K> comp;
18+
KeyHash<K> hashFunc;
1719

1820
public:
19-
OpenHashMap():
20-
table(),
21+
OpenHashMap(size_t tableSize):
2122
hashFunc(tableSize),
2223
comp()
2324
{
25+
this->tableSize = tableSize;
26+
table = new OpenHashNode<K, V>*[tableSize]();
2427
}
2528

2629
~OpenHashMap()
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#pragma once
2+
3+
#include "VComparator.h"
4+
#include "UniversalHash.h"
5+
#include "OpenedHashNode.h"
6+
7+
const int DELETED = -1;
8+
9+
template <typename K, typename V, size_t tableSize>
10+
class OpenHashMap
11+
{
12+
private:
13+
OpenHashMap(const OpenHashMap& other);
14+
const OpenHashMap& operator=(const OpenHashMap& other);
15+
OpenHashNode<K, V>* table[tableSize];
16+
VComparator<K> comp;
17+
KeyHash<K> hashFunc;
18+
19+
public:
20+
OpenHashMap():
21+
table(),
22+
hashFunc(tableSize),
23+
comp()
24+
{
25+
}
26+
27+
~OpenHashMap()
28+
{
29+
// destroy all buckets one by one
30+
for (size_t i = 0; i < tableSize; ++i) {
31+
OpenHashNode<K, V>* entry = table[i];
32+
delete entry;
33+
table[i] = NULL;
34+
}
35+
}
36+
37+
bool insert(const K& key, const V& value)
38+
{
39+
unsigned int attempt = 0;
40+
unsigned long hashValue = hashFunc[key];
41+
OpenHashNode<K, V>* entry = table[hashValue];
42+
// std::cout <<"\nkey: " << key << " hash: " << hashValue << std::endl;
43+
while (entry != NULL && entry->getKey() != DELETED) {
44+
attempt++;
45+
// std::cout << attempt << " ";
46+
hashValue = (hashFunc[key] + attempt) % tableSize;
47+
entry = table[hashValue];
48+
if (attempt >= tableSize-1)
49+
{
50+
// std::cout << "Opened Hash Table is full!";
51+
return false;
52+
53+
}
54+
}
55+
if (entry == NULL) {
56+
// std::cout << "entry == NULL"<< std::endl;
57+
entry = new OpenHashNode<K, V>(key, value);
58+
table[hashValue] = entry;
59+
return true;
60+
}
61+
else if (entry->getKey() == DELETED) {
62+
// std::cout << "entry == DELETED"<< std::endl;
63+
entry->setKey(key);
64+
entry->setValue(value);
65+
return true;
66+
}
67+
else {
68+
// std::cout << "entry set new value" << std::endl;
69+
entry->setValue(value);
70+
return true;
71+
}
72+
}
73+
74+
bool search(const K& key, V& value)
75+
{
76+
unsigned int attempt = 0;
77+
unsigned long hashValue = hashFunc[key];
78+
OpenHashNode<K, V>* entry = table[hashValue];
79+
while (entry != NULL) {
80+
// std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
81+
if (comp.compare(entry->getKey(), key)) {
82+
value = entry->getValue();
83+
return true;
84+
}
85+
attempt++;
86+
hashValue = (hashFunc[key] + attempt) % tableSize;
87+
entry = table[hashValue];
88+
if (attempt >= tableSize-1)
89+
{
90+
// std::cout << "Opened Hash Table is full!";
91+
// std::cout << "Number of tries: "<< attempt <<std::endl;
92+
// std::cout << "UKNOWN KEY!"<< std::endl;
93+
return false;
94+
95+
}
96+
}
97+
return false;
98+
}
99+
100+
101+
void remove(const K& key)
102+
{
103+
unsigned int attempt = 0;
104+
unsigned long hashValue = hashFunc[key];
105+
OpenHashNode<K, V>* entry = table[hashValue];
106+
107+
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
108+
attempt++;
109+
hashValue = (hashFunc[key] + attempt) % tableSize;
110+
entry = table[hashValue];
111+
if (attempt >= tableSize-1)
112+
{
113+
// std::cout << "Opened Hash Table is full!";
114+
// std::cout << "Number of tries: "<< attempt <<std::endl;
115+
// std::cout << "UKNOWN KEY!"<< std::endl;
116+
return;
117+
118+
}
119+
}
120+
121+
if (entry == NULL) {
122+
// std::cout << "Number of tries: "<< attempt <<std::endl;
123+
// std::cout << "UKNOWN KEY!"<< std::endl;
124+
return;
125+
126+
}
127+
else {
128+
entry -> setKey(DELETED);
129+
entry -> setValue(DELETED);
130+
}
131+
}
132+
};

algorithm/include/OpenedHashNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class OpenHashNode
77
private:
88
K _key;
99
V _value;
10-
OpenHashNode(const OpenHashNode&);
11-
OpenHashNode& operator=(const OpenHashNode&);
10+
// OpenHashNode(const OpenHashNode&);
11+
// OpenHashNode& operator=(const OpenHashNode&);
1212

1313
public:
1414
OpenHashNode(const K& key, const V& value) :

algorithm/include/UniversalHash.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <random>
33
#include <math.h>
44
#include <string>
5-
// #include <time>
65

76
template <typename K>
87
class KeyHash{
@@ -34,7 +33,7 @@ class KeyHash<int>{
3433
std::uniform_int_distribution<int> b_uid(0, this->p-1);
3534
this->a = a_uid(mers);
3635
this->b = b_uid(mers);
37-
std::cout<<"a " <<a <<" b "<<b<<std::endl;
36+
// std::cout<<"a " <<a <<" b "<<b<<std::endl;
3837
}
3938
unsigned long operator[](const int& key) const
4039
{

algorithm/include/utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ std::vector<int> genRandVec(int N, unsigned int a, unsigned int b);
99

1010
// unit tests
1111
void unitTestOpenHashMap();
12-
//
12+
void unitTestLinkedHashMap();
13+
1314
// performance evaluation
1415
void getPerformanceInteger();
16+
void getPerformanceIntegerLoop();
1517
// void testVectorLinkedHashMap();

algorithm/src/main.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <random>
77

88
#include "utils.h"
9-
#include "LinkedHashMap.h"
9+
// #include "LinkedHashMap.h"
1010

1111
int main() {
1212
// Make three vectors, each of length 100 filled with 1s, 2s, and 3s
@@ -38,11 +38,13 @@ int main() {
3838
*/
3939

4040
// unit tests
41-
// unitTestOpenHashMap();
41+
unitTestLinkedHashMap();
42+
unitTestOpenHashMap();
4243

4344
// performance evaluation
4445
std::cout << "\n\n****Integer Numbers Hashing******";
4546
// getPerformanceInteger();
47+
getPerformanceIntegerLoop();
4648
//
4749
//
4850
// std::cout << "\n\n****Integer Vectors Hashing******";
@@ -52,32 +54,32 @@ int main() {
5254
//test vector of integer number hashing
5355
// std::cout << "Hashing Vectors:\n";
5456
// testVectorLinkedHashMap();
55-
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
56-
std::vector<int> t = {3, 4, 5, 9, 10, 20, 21, 6};
57-
const size_t tableSize = 11;
58-
HashMap<int, int> hmap(tableSize);
57+
// std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
58+
// std::vector<int> t = {3, 4, 5, 9, 10, 20, 21, 6};
59+
// const size_t tableSize = 11;
60+
// HashMap<int, int> hmap(tableSize);
61+
// // hmap.displayHash();
62+
// for(auto const& value: v){
63+
// // std::cout << value << " ";
64+
// hmap.insert(value, value);
65+
// }
66+
// hmap.displayHash();
67+
// int value;
68+
// for(auto const& key: t){
69+
// // std::cout << value << " ";
70+
// if(hmap.search(key, value)){
71+
// std::cout<<" found " << key <<" "<< value<<std::endl;
72+
// }
73+
// else{
74+
// std::cout<<key<<" not found "<<std::endl;
75+
// }
76+
//
77+
// }
78+
// for(auto const& value: v){
79+
// // std::cout << value << " ";
80+
// hmap.remove(value);
81+
// }
5982
// hmap.displayHash();
60-
for(auto const& value: v){
61-
// std::cout << value << " ";
62-
hmap.insert(value, value);
63-
}
64-
hmap.displayHash();
65-
int value;
66-
for(auto const& key: t){
67-
// std::cout << value << " ";
68-
if(hmap.search(key, value)){
69-
std::cout<<" found " << key <<" "<< value<<std::endl;
70-
}
71-
else{
72-
std::cout<<key<<" not found "<<std::endl;
73-
}
74-
75-
}
76-
for(auto const& value: v){
77-
// std::cout << value << " ";
78-
hmap.remove(value);
79-
}
80-
hmap.displayHash();
8183

8284

8385
return 0;

0 commit comments

Comments
 (0)