-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTable.h
74 lines (53 loc) · 2.07 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
69
70
71
72
73
74
#ifndef MY_MALLOC_HASHTABLE_H
#define MY_MALLOC_HASHTABLE_H
#include "pages.h"
#include "global.h" /* for ARRAY_SIZE constant */
class HashEntry;
/**
* A hash table for storing, during allocating space,
* data (namely a pointer to the corresponding page-node)
* required for de-allocating later on.
*/
class HashTable {
HashEntry* array[ARRAY_SIZE]; /** The inner array for data to be stored **/
int (*hashFunction)(uintptr_t key); /** the hash function to be used **/
public:
HashTable();
/**
* A constructor allowing a user-defined hash function to be passed as a parameter.
* @param customHashFunction The custom function to be used for hashing.
*/
explicit HashTable(int (*customHashFunction)(uintptr_t key));
/**
* Store info of a page-node in the hash table
* @param key The key associated with the data
* @param value The value to be stored
*/
void store(uintptr_t key, PageListNode *value);
/**
* Retrieve the data associated with the given key from the hash table
* @param key The key to be passed to te hash function
* @return The corresponding info stored in the table
*/
PageListNode* get(uintptr_t key);
/**
* The hash function to be used by default
* @param key The key to hash
* @return A corresponding index of the array
*/
static int defaultHashFunction(uintptr_t key);
};
/**
* A packaging structure to store data inside the HashTable's array.
* It is also a list-node (for conflict resolutions).
*/
class HashEntry{
public:
HashEntry(uintptr_t key, PageListNode*node):prev(nullptr), next(nullptr), node(node), key(key){}
// public members are ok. Defining setters and getters is actually equivalent to public members
// and this class is only supposed to be used by HashTable.
HashEntry *prev, *next; /** pointers to previous and next info-nodes created in conflicts */
uintptr_t key; /** The associated key to be stored as well */
PageListNode *node; /** The actual info to store */
};
#endif //MY_MALLOC_HASHTABLE_H