Skip to content

Commit 200cfe8

Browse files
Merge pull request #11 from CodeSopranos/vadik_alp/dev
Vadik alp/dev
2 parents 2cae7b2 + f9583ee commit 200cfe8

16 files changed

+725
-104
lines changed

algorithm/include/LinkedHashMap.h renamed to algorithm/include/ChainedHashMap.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <list>
33
#include "VComparator.h"
44
#include "UniversalHash.h"
5-
#include "LinkedHashNode.h"
5+
#include "ChainedHashNode.h"
66

77

88
// Hash map class template
@@ -28,7 +28,7 @@ class HashMap
2828
}
2929
bool insert(const K& key, const V& value)
3030
{
31-
unsigned long hashValue = hashFunc[key];
31+
unsigned int hashValue = hashFunc[key];
3232
HashNode<K, V>* prev = NULL;
3333
HashNode<K, V>* entry = table[hashValue];
3434
// std::cout << "before while" << &entry <<std::endl;
@@ -63,7 +63,7 @@ class HashMap
6363
}
6464
bool search(const K& key, V& value)
6565
{
66-
unsigned long hashValue = hashFunc[key];
66+
unsigned int hashValue = hashFunc[key];
6767
HashNode<K, V>* entry = table[hashValue];
6868
unsigned int ccounter = 0;
6969
while (entry != NULL) {
@@ -84,7 +84,7 @@ class HashMap
8484

8585
void remove(const K& key)
8686
{
87-
unsigned long hashValue = hashFunc[key];
87+
unsigned int hashValue = hashFunc[key];
8888
HashNode<K, V>* prev = NULL;
8989
HashNode<K, V>* entry = table[hashValue];
9090

@@ -111,17 +111,17 @@ void remove(const K& key)
111111
delete entry;
112112
}
113113
}
114-
void displayHash() {
115-
HashNode<K,V>* entry = NULL;
116-
for (int i = 0; i < tableSize; i++) {
117-
entry = table[i];
118-
std::cout << i;
119-
while (entry != NULL) {
120-
std::cout << " -> " << entry->getKey();
121-
entry = entry->getNext();
122-
}
123-
std::cout << std::endl;
124-
}
125-
}
114+
// void displayHash() {
115+
// HashNode<K,V>* entry = NULL;
116+
// for (int i = 0; i < tableSize; i++) {
117+
// entry = table[i];
118+
// std::cout << i;
119+
// while (entry != NULL) {
120+
// std::cout << " -> " << entry->getKey();
121+
// entry = entry->getNext();
122+
// }
123+
// std::cout << std::endl;
124+
// }
125+
// }
126126

127127
};

algorithm/include/LinkedHashMapOLD.h renamed to algorithm/include/ChainedHashMapOLD.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "VComparator.h"
44
#include "UniversalHash.h"
5-
#include "LinkedHashNode.h"
5+
#include "ChainedHashNode.h"
66

77

88
// Hash map class template
@@ -42,7 +42,7 @@ class HashMap
4242

4343
bool search(const K& key, V& value)
4444
{
45-
unsigned long hashValue = hashFunc[key];
45+
unsigned int hashValue = hashFunc[key];
4646
HashNode<K, V>* entry = table[hashValue];
4747
unsigned int ccounter = 0;
4848
while (entry != NULL) {
@@ -63,7 +63,7 @@ class HashMap
6363

6464
void insert(const K& key, const V& value)
6565
{
66-
unsigned long hashValue = hashFunc[key];
66+
unsigned int hashValue = hashFunc[key];
6767
HashNode<K, V>* prev = NULL;
6868
HashNode<K, V>* entry = table[hashValue];
6969
// std::cout << "before while" << entry <<std::endl;
@@ -93,7 +93,7 @@ class HashMap
9393

9494
void remove(const K& key)
9595
{
96-
unsigned long hashValue = hashFunc[key];
96+
unsigned int hashValue = hashFunc[key];
9797
HashNode<K, V>* prev = NULL;
9898
HashNode<K, V>* entry = table[hashValue];
9999

File renamed without changes.

algorithm/include/OpenedHash.h

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class OpenKeyHash
2121
size_t tableSize;
2222
KeyHash<K> uniHashFunc;
2323
KeyHash<K> uniHashFuncAUX;
24-
unsigned int p = 101027;
24+
unsigned int prime= 101027;
25+
unsigned int c1 = 11;
26+
unsigned int c2 = 7;
2527
std::string hashType;
2628
public:
2729
OpenKeyHash(size_t tableSize, std::string hashType) :
@@ -30,16 +32,69 @@ class OpenKeyHash
3032
uniHashFuncAUX(tableSize),
3133
hashType(hashType){};
3234

33-
unsigned long operator[](KeyAttempt<K> const& pairKA) const
35+
unsigned int operator[](KeyAttempt<K> const& pairKA) const
3436
{
3537
if (hashType == "LINEAR"){
3638
return (uniHashFunc[pairKA.key] + pairKA.attempt) % tableSize;
3739
}
3840
else if (hashType == "QUADRATIC")
3941
{
4042
return (uniHashFunc[pairKA.key] +
41-
((pairKA.attempt * pairKA.key) % p) +
42-
((pairKA.attempt * pairKA.attempt * pairKA.key) % p)) % tableSize;
43+
((pairKA.attempt * c1) % prime) +
44+
((pairKA.attempt * pairKA.attempt * c2) % prime))
45+
% tableSize;
46+
}
47+
else if (hashType == "DOUBLE")
48+
{
49+
unsigned int auxHash1 = uniHashFunc[pairKA.key];
50+
// unsigned int auxHash2 = pairKA.key[0]; // ((auxHash1 << 1) >> 2);///(auxHash1 % 2) + (auxHash1 / 2); //(auxHash1 >> 1);
51+
unsigned int l = auxHash1 + pairKA.attempt * (2*(pairKA.key % 1071)) ;//* auxHash2;
52+
return (l >= tableSize) ? l % tableSize : l;
53+
}
54+
else{
55+
return (uniHashFunc[pairKA.key] + pairKA.attempt) % tableSize;
56+
}
57+
}
58+
};
59+
60+
61+
template <>
62+
class OpenKeyHash<std::string>
63+
{
64+
private:
65+
size_t tableSize;
66+
KeyHash<std::string> uniHashFunc;
67+
KeyHash<std::string> uniHashFuncAUX;
68+
unsigned int prime= 97;
69+
unsigned int c1 = 11;
70+
unsigned int c2 = 7;
71+
std::string hashType;
72+
public:
73+
OpenKeyHash(size_t tableSize, std::string hashType) :
74+
tableSize(tableSize),
75+
uniHashFunc(tableSize),
76+
uniHashFuncAUX(tableSize),
77+
hashType(hashType){};
78+
79+
unsigned int operator[](KeyAttempt<std::string> const& pairKA) const
80+
{
81+
if (hashType == "LINEAR"){
82+
unsigned int l = uniHashFunc[pairKA.key] + pairKA.attempt;
83+
return (l >= tableSize) ? l % tableSize : l;
84+
}
85+
else if (hashType == "QUADRATIC")
86+
{
87+
return (uniHashFunc[pairKA.key] +
88+
((pairKA.attempt * c1) % tableSize) +
89+
((pairKA.attempt * pairKA.attempt * c2) % tableSize))
90+
% tableSize;
91+
}
92+
else if (hashType == "DOUBLE")
93+
{
94+
unsigned int auxHash1 = uniHashFunc[pairKA.key];
95+
unsigned int auxHash2 = auxHash1 % 2 + auxHash1;
96+
unsigned int l = auxHash1 + pairKA.attempt * auxHash2;
97+
return (l >= tableSize) ? l % tableSize : l;
4398
}
4499
else{
45100
return (uniHashFunc[pairKA.key] + pairKA.attempt) % tableSize;

algorithm/include/OpenedHashMap.h

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "OpenedHash.h"
77

88

9-
const int DELETED = -1;
9+
#define DELETED true
1010

1111
template <typename K, typename V>
1212
class OpenHashMap
@@ -41,36 +41,38 @@ class OpenHashMap
4141
{
4242
unsigned int attempt = 0;
4343
KeyAttempt<K> pairKA = {key, attempt};
44-
unsigned long hashValue = hashFunc[pairKA];
44+
unsigned int hashValue = hashFunc[pairKA];
4545
OpenHashNode<K, V>* entry = table[hashValue];
4646
// std::cout <<"\nkey: " << key << " hash: " << hashValue << std::endl;
47-
while (entry != NULL && entry->getKey() != DELETED) {
47+
while (entry != NULL && entry->getState() != DELETED) {
4848
attempt++;
4949
// std::cout << attempt << " ";
5050
pairKA.attempt = attempt;
5151
hashValue = hashFunc[pairKA];
5252
entry = table[hashValue];
53-
if (attempt >= tableSize-1)
53+
if (attempt > tableSize)
5454
{
55-
// std::cout << "Opened Hash Table is full!";
55+
// std::cout << "Opened Hash Table is full! " << value << std::endl;
5656
return false;
5757

5858
}
5959
}
6060
if (entry == NULL) {
6161
// std::cout << "entry == NULL"<< std::endl;
62+
// std::cout << "entry set " << value << std::endl;
6263
entry = new OpenHashNode<K, V>(key, value);
6364
table[hashValue] = entry;
6465
return true;
6566
}
66-
else if (entry->getKey() == DELETED) {
67+
else if (entry->getState() == DELETED) {
6768
// std::cout << "entry == DELETED"<< std::endl;
6869
entry->setKey(key);
6970
entry->setValue(value);
71+
entry->setState(!DELETED);
7072
return true;
7173
}
7274
else {
73-
// std::cout << "entry set new value" << std::endl;
75+
// std::cout << "entry set new value " << value << std::endl;
7476
entry->setValue(value);
7577
return true;
7678
}
@@ -80,19 +82,21 @@ class OpenHashMap
8082
{
8183
unsigned int attempt = 0;
8284
KeyAttempt<K> pairKA = {key, attempt};
83-
unsigned long hashValue = hashFunc[pairKA];
85+
unsigned int hashValue = hashFunc[pairKA];
8486
OpenHashNode<K, V>* entry = table[hashValue];
8587
while (entry != NULL) {
8688
// std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
87-
if (comp.compare(entry->getKey(), key)) {
89+
if (entry->getState() != DELETED) {
90+
if (comp.compare(entry->getKey(), key)) {
8891
value = entry->getValue();
8992
return true;
93+
}
9094
}
9195
attempt++;
9296
pairKA.attempt = attempt;
9397
hashValue = hashFunc[pairKA];//(hashFunc[key, attempt] + attempt) % tableSize;
9498
entry = table[hashValue];
95-
if (attempt >= tableSize-1)
99+
if (attempt > tableSize)
96100
{
97101
// std::cout << "Opened Hash Table is full!";
98102
// std::cout << "Number of tries: "<< attempt <<std::endl;
@@ -109,15 +113,15 @@ class OpenHashMap
109113
{
110114
unsigned int attempt = 0;
111115
KeyAttempt<K> pairKA = {key, attempt};
112-
unsigned long hashValue = hashFunc[pairKA];
116+
unsigned int hashValue = hashFunc[pairKA];
113117
OpenHashNode<K, V>* entry = table[hashValue];
114118

115119
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
116120
attempt++;
117121
pairKA.attempt = attempt;
118122
hashValue = hashFunc[pairKA];//(hashFunc[key] + attempt) % tableSize;
119123
entry = table[hashValue];
120-
if (attempt >= tableSize-1)
124+
if (attempt > tableSize-1)
121125
{
122126
// std::cout << "Opened Hash Table is full!";
123127
// std::cout << "Number of tries: "<< attempt <<std::endl;
@@ -134,8 +138,25 @@ class OpenHashMap
134138

135139
}
136140
else {
137-
entry -> setKey(DELETED);
138-
entry -> setValue(DELETED);
141+
entry -> setState(DELETED);
142+
entry -> setState(DELETED);
139143
}
140144
}
145+
146+
void displayHash() {
147+
std::cout << std::endl;
148+
for (int i = 0; i < tableSize; i++) {
149+
std::cout << i;
150+
if (table[i]){
151+
if (!table[i]->getState())
152+
std::cout << " -> " << table[i]->getKey() << std::endl;
153+
else
154+
std::cout << " -> NULL (DELETED)" << std::endl;
155+
}
156+
else
157+
std::cout << " -> NULL" << std::endl;
158+
}
159+
160+
std::cout << std::endl;
161+
}
141162
};

algorithm/include/OpenedHashMapOLD.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class OpenHashMap
3737
bool insert(const K& key, const V& value)
3838
{
3939
unsigned int attempt = 0;
40-
unsigned long hashValue = hashFunc[key];
40+
unsigned int hashValue = hashFunc[key];
4141
OpenHashNode<K, V>* entry = table[hashValue];
4242
// std::cout <<"\nkey: " << key << " hash: " << hashValue << std::endl;
4343
while (entry != NULL && entry->getKey() != DELETED) {
@@ -74,7 +74,7 @@ class OpenHashMap
7474
bool search(const K& key, V& value)
7575
{
7676
unsigned int attempt = 0;
77-
unsigned long hashValue = hashFunc[key];
77+
unsigned int hashValue = hashFunc[key];
7878
OpenHashNode<K, V>* entry = table[hashValue];
7979
while (entry != NULL) {
8080
// std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl;
@@ -101,7 +101,7 @@ class OpenHashMap
101101
void remove(const K& key)
102102
{
103103
unsigned int attempt = 0;
104-
unsigned long hashValue = hashFunc[key];
104+
unsigned int hashValue = hashFunc[key];
105105
OpenHashNode<K, V>* entry = table[hashValue];
106106

107107
while (entry != NULL && !comp.compare(entry->getKey(), key)) {

algorithm/include/OpenedHashNode.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class OpenHashNode
77
private:
88
K _key;
99
V _value;
10+
bool isDeleted = false;
1011
// OpenHashNode(const OpenHashNode&);
1112
// OpenHashNode& operator=(const OpenHashNode&);
1213

@@ -35,4 +36,15 @@ class OpenHashNode
3536
{
3637
_key = key;
3738
}
39+
40+
void setState(bool state)
41+
{
42+
isDeleted = state;
43+
}
44+
45+
bool getState() const
46+
{
47+
return isDeleted;
48+
}
49+
3850
};

0 commit comments

Comments
 (0)