This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable_kukushka.cpp
106 lines (96 loc) · 2.17 KB
/
hashtable_kukushka.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <vector>
#include <utility>
#include <list>
#include <string>
using namespace std;
class NotFoundException{};
class DublicateKeyException{};
class Hasher {
public:
unsigned __int64 hash(string key) {
unsigned char result = 0;
for (unsigned int i = 0; i < key.length(); i++) {
result ^= key[i];
}
return result;
}
unsigned __int64 hash(unsigned __int64 key) {
unsigned char result = 34;
return result ^ (key >> 2);
}
unsigned __int64 operator () (string key) {
return hash(key);
}
};
template <class T>
class HashTable {
struct row{
unsigned __int64 hash;
struct {
string key;
T data;
} pair;
};
row* values[256];
Hasher hasher;
public:
HashTable(Hasher h = Hasher()): hasher(h){
memset(this->values, 0, sizeof(this->values));
};
bool insert(string key, T data) {
try {
this->find(key);
throw DublicateKeyException();
return false;
}
catch (NotFoundException) {
unsigned __int64 hash = hasher(key);
row **ptr = this->values + hash;
unsigned char max_hashes = 5;
while (*ptr != NULL && max_hashes) {
hash = hasher.hash(hash);
ptr = this->values + hash;
max_hashes--;
}
if (max_hashes) {
*ptr = new row({ hash, { key, data } });
return true;
}
else {
throw "shit!";
}
}
}
T find(string key) {
unsigned __int64 hash = hasher(key);
row **ptr = this->values + hash;
unsigned char max_hashes = 5;
while (*ptr != NULL && max_hashes) {
if ((*ptr)->pair.key == key)
return (*ptr)->pair.data;
hash = hasher.hash(hash);
ptr = this->values + hash;
max_hashes--;
}
throw NotFoundException();
}
T operator [] (string key) {
return find(key);
}
};
int main() {
HashTable<string> ht;
ht.insert("key", "value");
ht.insert("key2", "value2");
ht.insert("limepie3", "value3");
ht.insert("limepie4", "value4");
ht.insert("key255", "value2");
ht.insert("key3", "some cookies!");
cout << ht["key"] << endl;
cout << ht["key2"] << endl;
cout << ht["key255"] << endl;
cout << ht["limepie3"] << endl;
cout << ht["limepie4"] << endl;
return 0;
}