-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-hash.h
89 lines (73 loc) · 2.24 KB
/
table-hash.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef TABLE_H
#define TABLE_H
#include <stdbool.h>
#include <stddef.h>
typedef void *KEY;
typedef void *VALUE;
typedef bool (*key_compare_func)(KEY, KEY);
typedef size_t (*key_hash_func)(KEY);
typedef void (*keyFreeFunc)(void*);
typedef void (*valFreeFunc)(void*);
typedef struct table table;
/**
* Create a dynamically allocated empty table.
*
* Free resources by calling table_kill().
*
* \param [in] capacity The capacity of the underlying data structure (if applicable).
* \param [in] cmp The key compare function (tests key equality).
* \param [in] hash The key hash function (may be NULL if not a hash table).
* \param [in] keyFree The free function for the keys (may be NULL if not wanted).
* \param [in] valFree The free function for the values (may be NULL if not wanted)-
* \return A dynamically allocated empty table.
*/
table *table_empty(int capacity, key_compare_func cmp, key_hash_func hash,
keyFreeFunc keyFree, valFreeFunc valFree);
/**
* Check if a table is empty.
*
* \param [in] t The table.
* \return True if the table is empty.
*/
bool table_is_empty(table *t);
/**
* Insert a new key/value pair into the table.
*
* Note: The table assumes ownership of the dynamically allocated key
* and value and will deallocate them using free() in table_remove()
* and table_kill().
*
* \param [in,out] t The table.
* \param [in] key The key (dynamically allocated).
* \param [in] val The value (dynamically allocated).
*/
void table_insert(table *t, KEY key, VALUE val);
/**
* Lookup the value associated with a given key.
*
* \param [in] t The table.
* \param [in] key The key.
* \return The value if the key exists, otherwise NULL.
*/
VALUE table_lookup(table *t, KEY key);
/**
* Remove a specified key (if it exists) from the table.
*
* Note: Any matching key/value pairs will be deallocated using
* free().
*
* \param [in,out] t The table.
* \param [in] key The key to remove.
*/
void table_remove(table *t, KEY key);
/**
* Free resources allocated previously by table_empty().
*
* The table must not be used again after return from this function.
*
* Note: All key/value pairs will be deallocated using free().
*
* \param [in] t The table to deallocate.
*/
void table_kill(table *t);
#endif