-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.h
58 lines (52 loc) · 1.57 KB
/
HashTable.h
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
/// Citations:
/// Code for TrivialHashTable::roundUpBaseTwo from https://stackoverflow.com/a/466242/10252771
#ifndef HASHTABLE_H
#define HASHTABLE_H
#define MAX_UINT_20 0xFFFFF // 2^20-1
#define NUM_BYTES_PER_INT 4
#define FNV_offset_basis 0x811C9DC5
#define FNV_prime 0x01000193
#include <iostream>
#include <forward_list>
#include <iterator>
#include <cstdint>
#include <cmath>
using namespace std;
class HashTable {
protected:
forward_list<uint32_t> * lists;
virtual uint32_t hash(uint32_t) = 0;
uint32_t numTotalLists;
public:
HashTable();
virtual ~HashTable() = default;
uint64_t getNumCollisions() const;
virtual void insert(uint32_t) = 0;
};
class TrivialHashTable : public HashTable {
private:
uint32_t hash(uint32_t) override;
uint32_t roundUpBaseTwo(uint32_t);
short numBitsToMask;
public:
explicit TrivialHashTable(uint32_t numIntegers) : HashTable() {
///Trivial hash algorithm uses a table size that is a power of two.
numTotalLists = roundUpBaseTwo(numIntegers) / 4;
lists = new forward_list<uint32_t> [numTotalLists];
numBitsToMask = log2(numTotalLists);
}
~TrivialHashTable() override { delete [] lists; }
void insert(uint32_t) override;
};
class FNVHashTable : public HashTable {
private:
uint32_t hash(uint32_t) override;
public:
explicit FNVHashTable() : HashTable() {
numTotalLists = MAX_UINT_20;
lists = new forward_list<uint32_t> [numTotalLists];
}
~FNVHashTable() override { delete [] lists; }
void insert(uint32_t) override;
};
#endif //HASHTABLE_H