-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.h
66 lines (58 loc) · 1.97 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
// Writting my own hashtable implementation
/**
* Start with a static implementation with a size of 16
* Step 1 - Use hash function to create hash of input key
* Step 2 - Use modulo operator to get index of size 16 array
* Step 3 - Use linear probing to fit the hash into the array
*
* Need to be able to create and remove a hashmap
*
*
* Things that still need to be considered
* - Test that linear probing works
* - Add searching for value given a key
* - Resize hashtable once count is 33% of the total size
* - Add removal of key/value pair from hashmap
*
* - When storing the string key, we are simply passing the pointer
* which means we are creating a shallow copy and not a deep copy.
* This needs to be fixed by copying each character at a time and
* allocating new memory for it so that it is immutable.
*
* Linear probing affects addition, searching, and deletion of keys in
* the hashtable. More information can be found here
* https://stackoverflow.com/questions/6338798/is-searching-a-hashtable-for-a-value-that-isnt-there-on-linear-probing
*
*/
typedef struct Hashtable
{
int *val; // key value pair
unsigned long *key; // hash key pair (used to check for collision)
char **str_key; // string key
int size; // size of hashtable
int count; // current number of items in hashtable
} Hashtable;
/**
* Initializes pointer of type Hashtable
*/
Hashtable *initialize_hashtable();
/**
* Free allocated space to hashtable
*/
void free_hashtable(Hashtable **map);
/**
* Treat hashtable as a Counter and add 1 to current count of key
*/
void add_to_hashtable(Hashtable **map, char *key);
/**
* Return the value of a key/value pair. Returns -1 if no key was found in the hashtable
*/
int search_hashtable(Hashtable *map, char *key);
/**
* Remove the a key from a hashtable if it exists
*/
void remove_from_hashtable(Hashtable *map, char *key);
/**
* Print the contents of the hashtable
*/
void print_hashtable(Hashtable *map);