Skip to content

Commit caad0d9

Browse files
author
VirtualRoyalty
committed
opened hash template added, quadratic and linear hashing implemented
1 parent fa2dec1 commit caad0d9

File tree

7 files changed

+377
-248
lines changed

7 files changed

+377
-248
lines changed

algorithm/include/OpenedHash.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
#include <random>
3+
#include <math.h>
4+
#include <string>
5+
6+
#include "UniversalHash.h"
7+
8+
9+
template<typename T>
10+
struct KeyAttempt
11+
{
12+
const T& key;
13+
unsigned int attempt;
14+
};
15+
16+
17+
template <typename K>
18+
class OpenKeyHash
19+
{
20+
private:
21+
size_t tableSize;
22+
KeyHash<K> uniHashFunc;
23+
KeyHash<K> uniHashFuncAUX;
24+
unsigned int p = 101027;
25+
std::string hashType;
26+
public:
27+
OpenKeyHash(size_t tableSize, std::string hashType) :
28+
tableSize(tableSize),
29+
uniHashFunc(tableSize),
30+
uniHashFuncAUX(tableSize),
31+
hashType(hashType){};
32+
33+
unsigned long operator[](KeyAttempt<K> const& pairKA) const
34+
{
35+
if (hashType == "LINEAR"){
36+
return (uniHashFunc[pairKA.key] + pairKA.attempt) % tableSize;
37+
}
38+
else if (hashType == "QUADRATIC")
39+
{
40+
return (uniHashFunc[pairKA.key] +
41+
((pairKA.attempt * pairKA.key) % p) +
42+
((pairKA.attempt * pairKA.attempt * pairKA.key) % p)) % tableSize;
43+
}
44+
else{
45+
return (uniHashFunc[pairKA.key] + pairKA.attempt) % tableSize;
46+
}
47+
}
48+
};

algorithm/include/OpenedHashMap.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "VComparator.h"
44
#include "UniversalHash.h"
55
#include "OpenedHashNode.h"
6+
#include "OpenedHash.h"
7+
68

79
const int DELETED = -1;
810

@@ -15,14 +17,13 @@ class OpenHashMap
1517
size_t tableSize;
1618
OpenHashNode<K, V>** table;
1719
VComparator<K> comp;
18-
KeyHash<K> hashFunc;
19-
20+
OpenKeyHash<K> hashFunc;
2021
public:
21-
OpenHashMap(size_t tableSize):
22-
hashFunc(tableSize),
22+
OpenHashMap(size_t tableSize, std::string hashType):
23+
tableSize(tableSize),
24+
hashFunc(tableSize, hashType),
2325
comp()
2426
{
25-
this->tableSize = tableSize;
2627
table = new OpenHashNode<K, V>*[tableSize]();
2728
}
2829

@@ -39,13 +40,15 @@ class OpenHashMap
3940
bool insert(const K& key, const V& value)
4041
{
4142
unsigned int attempt = 0;
42-
unsigned long hashValue = hashFunc[key];
43+
KeyAttempt<K> pairKA = {key, attempt};
44+
unsigned long hashValue = hashFunc[pairKA];
4345
OpenHashNode<K, V>* entry = table[hashValue];
4446
// std::cout <<"\nkey: " << key << " hash: " << hashValue << std::endl;
4547
while (entry != NULL && entry->getKey() != DELETED) {
4648
attempt++;
4749
// std::cout << attempt << " ";
48-
hashValue = (hashFunc[key] + attempt) % tableSize;
50+
pairKA.attempt = attempt;
51+
hashValue = hashFunc[pairKA];
4952
entry = table[hashValue];
5053
if (attempt >= tableSize-1)
5154
{
@@ -76,7 +79,8 @@ class OpenHashMap
7679
bool search(const K& key, V& value)
7780
{
7881
unsigned int attempt = 0;
79-
unsigned long hashValue = hashFunc[key];
82+
KeyAttempt<K> pairKA = {key, attempt};
83+
unsigned long hashValue = hashFunc[pairKA];
8084
OpenHashNode<K, V>* entry = table[hashValue];
8185
while (entry != NULL) {
8286
// std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
@@ -85,7 +89,8 @@ class OpenHashMap
8589
return true;
8690
}
8791
attempt++;
88-
hashValue = (hashFunc[key] + attempt) % tableSize;
92+
pairKA.attempt = attempt;
93+
hashValue = hashFunc[pairKA];//(hashFunc[key, attempt] + attempt) % tableSize;
8994
entry = table[hashValue];
9095
if (attempt >= tableSize-1)
9196
{
@@ -103,12 +108,14 @@ class OpenHashMap
103108
void remove(const K& key)
104109
{
105110
unsigned int attempt = 0;
106-
unsigned long hashValue = hashFunc[key];
111+
KeyAttempt<K> pairKA = {key, attempt};
112+
unsigned long hashValue = hashFunc[pairKA];
107113
OpenHashNode<K, V>* entry = table[hashValue];
108114

109115
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
110116
attempt++;
111-
hashValue = (hashFunc[key] + attempt) % tableSize;
117+
pairKA.attempt = attempt;
118+
hashValue = hashFunc[pairKA];//(hashFunc[key] + attempt) % tableSize;
112119
entry = table[hashValue];
113120
if (attempt >= tableSize-1)
114121
{

algorithm/include/OpenedHashNode.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ class OpenHashNode
1212

1313
public:
1414
OpenHashNode(const K& key, const V& value) :
15-
_key(key), _value(value)
16-
{
17-
}
15+
_key(key),
16+
_value(value)
17+
{}
18+
1819
K getKey() const
1920
{
2021
return _key;

algorithm/include/UniversalHash.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ class KeyHash{
88
private:
99
size_t tableSize;
1010
public:
11-
KeyHash(size_t tableSize){
12-
this->tableSize = tableSize;
13-
}
11+
KeyHash(size_t tableSize) : tableSize(tableSize) {}
12+
1413
unsigned long operator[](const K& key) const
1514
{
1615
return reinterpret_cast<unsigned long>(key) % this->tableSize;

algorithm/src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ int main() {
2929

3030
// performance evaluation
3131
std::cout << "\n\n****Integer Numbers Hashing******";
32-
// getPerformanceInteger();
32+
getPerformanceInteger();
3333
// getPerformanceIntegerLoop();
34-
getPerformanceIntegerToFile();
34+
// getPerformanceIntegerToFile();
3535
//
3636
//
3737
// std::cout << "\n\n****Integer Vectors Hashing******";

0 commit comments

Comments
 (0)