-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hashtable.h
68 lines (51 loc) · 1.27 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
59
60
61
62
63
64
65
66
67
68
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <list>
#include <vector>
#include <functional>
#include "Exceptions.h"
/**
* This class is a a template implementation of a hashtable using user given
* hash function.
* Collisions are solved via appending keys to a list in the relevant hash
* table entry.
*/
template <class T>
class Hashtable final {
private:
// Entry in the hash table
class Entry;
public:
// hash function turning objects of type T into hashes
using hash_func_t = std::function<size_t(T)>;
// Construct an empty hash table using hash_func as hashing function
// and of size 'size'.
Hashtable(hash_func_t hash_func, size_t size);
~Hashtable();
// Adds a key to the hash table
void insert(T key);
// Check whether a key is in the hash table
bool lookup(T key) const;
private:
// hash function
hash_func_t m_hash_func;
// Table linking hashed to entries
std::vector<Entry> m_table;
// size of hash table
size_t m_size;
};
template <class T>
class Hashtable<T>::Entry final {
public:
Entry();
~Entry();
// Insert a new key to the hash table
void insert(T key);
// Check whether a certain key is in the table
bool lookup(T key) const;
private:
// List of keys with current hash
std::list<T> m_keys;
};
#include "Hashtable.hpp"
#endif