-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeparateChainingHashTable.h
More file actions
172 lines (138 loc) · 4.58 KB
/
SeparateChainingHashTable.h
File metadata and controls
172 lines (138 loc) · 4.58 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifndef SEPARATE_CHAINING_HASH_TABLE_HEADER
#define SEPARATE_CHAINING_HASH_TABLE_HEADER
#include "LinkedList.h"
#include <string>
#include <iostream>
unsigned long hashing( std::string const& s )
{
unsigned long result = 2166136261U ;
std::string::const_iterator end = s.end() ;
for ( std::string::const_iterator iter = s.begin() ;
iter != end ;
++ iter ) {
result = (16777619 * result)
^ static_cast< unsigned char >( *iter ) ;
}
return result ;
}
template <typename KeyT, typename ValueT>
class Pair
{
private:
// unsinged long hash;
KeyT key;
ValueT value;
public:
Pair(KeyT key, ValueT value): key(key), value(value){};
Pair(const Pair &pair);
Pair()
{
key = KeyT();
value = ValueT();
}
Pair &operator=(const Pair &pair);
const KeyT &getKey(){return key;};
ValueT &getValue(){return value;};
void setKey(KeyT &key){this->key = key;};
void setValue(ValueT &value){this->value = value;};
};
template <typename KeyT, typename ValueT>
Pair<KeyT, ValueT>::Pair(const Pair &pair)
{
this->key = pair.key;
this->value = pair.value;
}
template <typename KeyT, typename ValueT>
Pair<KeyT, ValueT> &Pair<KeyT, ValueT>::operator=(const Pair &pair)
{
this->key = pair.key;
this->value = pair.value;
return *this;
}
template <typename KeyT, typename ValueT>
class SeparateChainingHashTable
{
private:
unsigned int capacity;
LinkedList<Pair<KeyT, ValueT>> *pairs;
public:
SeparateChainingHashTable(unsigned int capacity);
~SeparateChainingHashTable();
bool contains(const KeyT &key);
ValueT &get(const KeyT &key);
ValueT &operator[](const KeyT &key){return this->get(key);}
unsigned long getCapacity(){return this->capacity;}
void resize(unsigned long new_size);
template <typename T1, typename T2>
friend std::ostream &operator<<(std::ostream &out, const SeparateChainingHashTable<T1, T2> &table);
};
template <typename KeyT, typename ValueT>
SeparateChainingHashTable<KeyT, ValueT>::SeparateChainingHashTable(unsigned int capacity): capacity(capacity)
{
this->pairs = new LinkedList<Pair<KeyT, ValueT>>[capacity];
}
template <typename KeyT, typename ValueT>
SeparateChainingHashTable<KeyT, ValueT>::~SeparateChainingHashTable()
{
delete[] this->pairs;
}
template <typename KeyT, typename ValueT>
void SeparateChainingHashTable<KeyT, ValueT>::resize(unsigned long new_size)
{
LinkedList<Pair<KeyT, ValueT>> *new_pairs = new LinkedList<Pair<KeyT, ValueT>>[new_size];
for(unsigned int i = 0; i < this->capacity; i++)
{
auto row = &this->pairs[i];
if(!row->is_empty())
{
for(LinkedListNode<Pair<KeyT, ValueT>> *it = row->getTail(); it != nullptr; it = it->getNext())
{
auto data = it->getData();
auto hash = hashing(data.getKey());
new_pairs[hash % new_size].push_head(data);
}
}
}
delete[] this->pairs;
this->capacity = new_size;
this->pairs = new_pairs;
}
template <typename KeyT, typename ValueT>
ValueT &SeparateChainingHashTable<KeyT, ValueT>::get(const KeyT &key)
{
unsigned long hash = hashing(key);
LinkedList<Pair<KeyT, ValueT>> *pair_list = &this->pairs[hash % this->capacity];
if(pair_list->contains([key](Pair<KeyT, ValueT> pair){return pair.getKey() == key;}))
return pair_list->find([key](Pair<KeyT, ValueT> pair){return pair.getKey() == key;}).getValue();
pair_list->push_head(Pair<KeyT, ValueT>(key, ValueT()));
return pair_list->getHead()->getData().getValue();
}
template <typename KeyT, typename ValueT>
bool SeparateChainingHashTable<KeyT, ValueT>::contains(const KeyT &key)
{
unsigned long hash = hashing(key);
LinkedList<Pair<KeyT, ValueT>> *pair_list = &this->pairs[hash % this->capacity];
return pair_list->contains([key](Pair<KeyT, ValueT> pair){return pair.getKey() == key;});
}
template <typename KeyT, typename ValueT>
std::ostream &operator<<(std::ostream &out, const SeparateChainingHashTable<KeyT, ValueT> &table)
{
out << "{";
std::string sep = "";
for(unsigned int i = 0; i < table.capacity; i++)
{
auto row = &table.pairs[i];
if(!row->is_empty())
{
for(LinkedListNode<Pair<KeyT, ValueT>> *it = row->getTail(); it != nullptr; it = it->getNext())
{
out << sep << it->getData().getKey() << ": " << it->getData().getValue();
if(sep == "")
sep = ", ";
}
}
}
out << "}";
return out;
}
#endif