-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
70 lines (63 loc) · 2.03 KB
/
hashtable.cpp
File metadata and controls
70 lines (63 loc) · 2.03 KB
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
#include "hashtable.h"
#include <stdlib.h>
#include <string.h>
// create a hashtable with num_buckets buckets
Hashtable *createHash(int num_buckets = DEFAULT_NUM_BUCKETS) {
Hashtable *new_hash = (Hashtable*)malloc(sizeof(Hashtable));
new_hash->num_buckets = num_buckets;
new_hash->buckets = (List**)malloc(sizeof(List**)*num_buckets);
for(int i = 0; i < new_hash->num_buckets; i++) {
new_hash->buckets[i] = NULL; // buckets are created on demand
}
return new_hash;
}
void deleteHash(Hashtable **hashtable_ptr, deleter delete_fn) {
//free the memory associated with hash
if ((hashtable_ptr != NULL) && (*hashtable_ptr != NULL)) {
Hashtable *hash_table = *hashtable_ptr;
for(int i = 0; i < hash_table->num_buckets; i++) {
deleteList(&hash_table->buckets[i], delete_fn);
}
free(hash_table->buckets);
free(hash_table);
*hashtable_ptr = NULL;
}
}
// hash function - return hash of given string key modulo number of buckets.
// (taken from p460 of Algorithms by Sedgewick vol 4)
int hash(Hashkey key, int num_buckets) {
int hash_val = 0;
int len = strlen(key);
for (int i = 0; i < len; i++) {
hash_val = (DEFAULT_NUM_BUCKETS * hash_val + key[i]) % num_buckets;
}
return hash_val;
}
// add key - data to the appropriate bucket
void add(Hashtable *h, Hashkey key, void* data) {
int index = hash(key, h->num_buckets);
if (h->buckets[index] == NULL) {
h->buckets[index] = createList();
}
prependTo(h->buckets[index], data);
}
// lookup - determine which bucket; determine if there
// is a value with the given key in that bucket
void *lookup(Hashtable *h, Hashkey key, compare mf) {
int index = hash(key, h->num_buckets);
void *data = NULL;
List *bucket = h->buckets[index];
if (bucket != NULL) {
data = getMatch(bucket, mf, key);
}
return data;
}
// remove - determine which bucket; determine if the value
// we want to remove is in that bucket
void remove(Hashtable* h, Hashkey key, void* data) {
int index = hash(key, h->num_buckets);
List *bucket = h->buckets[index];
if (bucket != NULL) {
removeIfExists(bucket, data);
}
}