-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadraticProbing-impl.hpp
56 lines (48 loc) · 1.31 KB
/
QuadraticProbing-impl.hpp
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
/**
Lab 3 - Linear Probing, Quadratic Probing, and Double Hashing
CSC 255 Objects and Algorithms (Fall 2020)
Oakton Community College
Professor: Kamilla Murashkina
@file QuadraticProbing-impl.hpp
@author Russell Taylor
@date 9/9/20
*/
#include "QuadraticProbing.hpp"
/**
Constructor
@param initialSize the initial size of the hash table
*/
template <typename Key, typename Value>
QuadraticProbing<Key, Value>::QuadraticProbing(int initialSize) {
this->table.resize(initialSize);
this->collisions = 0;
}
/**
Destructor
*/
template <typename Key, typename Value>
QuadraticProbing<Key, Value>::~QuadraticProbing() {
for (const auto p : this->table)
delete p;
}
/**
Looks up a key in the hash table
@param key the key
@return the index of the key in the hash table
*/
template <typename Key, typename Value>
int QuadraticProbing<Key, Value>::lookUp(const Key& key) {
const int startIndex = this->hashIndex(key);
int index = startIndex, i = 0;
while (true) {
const Record<Key, Value> *p = this->table[index];
if (p == nullptr || p->key == key)
return index;
this->collisions += 1;
i += 1;
index = startIndex + i * i;
index %= this->table.size();
if (index == startIndex)
return int(this->table.size()) + 1;
}
}