-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
163 lines (144 loc) · 2.9 KB
/
hashtable.cpp
File metadata and controls
163 lines (144 loc) · 2.9 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
/*
* hashtable.cpp
*
* Implementations for HashTable
*
* @authors Juan Arias & Nick Tibbott
*/
#include <climits>
/*
* Default constructor
*/
template <class T>
HashTable<T>::IndexState::IndexState() :key(INT_MIN), occupied(false) {
}
/*
* Return true if index in HashTable occupied, else false
*/
template <class T>
bool HashTable<T>::IndexState::isOccupied() const {
return occupied;
}
/*
* Returns the item at the index
*/
template <class T>
T& HashTable<T>::IndexState::getItem() {
return item;
}
/*
* Return the key at the index
*/
template <class T>
int HashTable<T>::IndexState::getKey() const {
return key;
}
/*
* Sets given item as the stored item in the index of the HashTable
*/
template <class T>
void HashTable<T>::IndexState::setItem(int key, const T& item) {
this->key = key;
this->item = item;
occupied = true;
}
/*
* Hash function for lookup in table
*/
template <class T>
int HashTable<T>::hash(int key) const {
return key % tableSz;
}
/*
* Gets the quadratic probe of the hash based on iteration i
*/
template <class T>
int HashTable<T>::quadraticProbe(int hash, int i) const {
return (hash + i * i) % tableSz;
}
/*
* Deallocates all dynamic memory
*/
template <class T>
void HashTable<T>::clear() {
delete[] table;
}
/*
* Constructs a HashTable with optional size
*/
template <class T>
HashTable<T>::HashTable(int size) :table(new IndexState[size]), tableSz(size) {
}
/*
* Deallocates all dynamic memory on destruction
*/
template <class T>
HashTable<T>::~HashTable() {
clear();
}
/*
* Add new T item mapped with given key
*/
template <class T>
void HashTable<T>::add(int key, const T& item) {
int hash(this->hash(key));
for (int i(0); table[hash].isOccupied(); ++i) {
hash = quadraticProbe(hash, i);
}
table[hash].setItem(key, item);
}
/*
* Get T item mapped to given key
*/
template <class T>
T& HashTable<T>::get(int key) const {
int hash(this->hash(key));
IndexState* iState = &table[hash];
for (int i(0); iState->isOccupied() && iState->getKey() != key; ++i) {
hash = quadraticProbe(hash, i);
iState = &table[hash];
}
return iState->getItem();
}
/*
*
*/
template <class T>
bool HashTable<T>::has(int key) const {
int hash(this->hash(key));
IndexState* iState = &table[hash];
bool hasKey;
for (int i(0); !(hasKey = iState->getKey() == key) && iState->isOccupied(); ++i) {
hash = quadraticProbe(hash, i);
iState = &table[hash];
}
return hasKey;
}
/*
* Returns iterator at first Item in table
*/
template<class T>
T* HashTable<T>::begin() {
return &table[0].getItem();
}
/*
* Returns iterator at last Item in table
*/
template<class T>
T* HashTable<T>::end() {
return &table[tableSz - 1].getItem();
}
/*
* Returns const iterator at first Item in table
*/
template<class T>
const T* HashTable<T>::begin() const {
return &table[0].getItem();
}
/*
* Returns const iterator at last Item in table
*/
template<class T>
const T* HashTable<T>::end() const {
return &table[tableSz - 1].getItem();
}