-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashSet.c
48 lines (43 loc) · 1.11 KB
/
HashSet.c
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
#include "HashSet.h"
HashSet HashSet_construct(hash_func hash)
{
HashSet map;
map.hash = hash;
for (size_t i = 0; i < HS_TABLE_SIZE; i++)
{
map.m_hashTable[i] = List_construct();
}
return map;
}
void HashSet_destruct(HashSet * set, destructor destruct)
{
for (size_t i = 0; i < HS_TABLE_SIZE; i++)
{
List_destruct(&set->m_hashTable[i], destruct);
}
}
void HashSet_insert(HashSet * set, void * element)
{
size_t index = set->hash(element) % HS_TABLE_SIZE;
List_append(&set->m_hashTable[index], element);
}
void HashSet_iterate(HashSet * set, iterator iterate)
{
for (size_t i = 0; i < HS_TABLE_SIZE; i++)
{
List_iterate(&set->m_hashTable[i], iterate);
}
}
void * HashSet_find(HashSet * set, void * element, equality_predicate are_equal)
{
size_t index = set->hash(element) % HS_TABLE_SIZE;
List slot = set->m_hashTable[index];
ListIterator iter = List_begin(&slot);
void * e;
for (;(e = ListIterator_get(&iter)) != NULL; ListIterator_next(&iter))
{
if (are_equal(element, e))
return e;
}
return NULL;
}