-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.h
137 lines (124 loc) · 4.23 KB
/
table.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef TABLE_H
#define TABLE_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a generic table for the "Datastructures and
* algorithms" courses at the Department of Computing Science, Umea
* University. The table stores void pointers, so it can be used to
* store all types of values. After use, the function table_kill must
* be called to de-allocate the dynamic memory used by the table
* itself. The de-allocation of any dynamic memory allocated for the
* key and/or value values is the responsibility of the user of the
* table, unless a corresponding free_function is registered in
* table_empty.
*
* Duplicates are handled by lookup and remove. Lookup will return
* the last value added for a duplicate key. Remove will remove all
* elements with matching keys. WARNING: If the key or value
* free_function is set, do not add the same pointer twice as this
* will result in memory errors.
*
* Authors: Niclas Borlin ([email protected])
* Adam Dahlgren Lindstrom ([email protected])
*
* Based on earlier code by: Johan Eliasson ([email protected]).
*
* Version information:
* 2018-02-06: v1.0, first public version.
*/
// ==========PUBLIC DATA TYPES============
// Table type.
typedef struct table table;
// ==========DATA STRUCTURE INTERFACE==========
/**
* table_empty() - Create an empty table.
* @key_cmp_func: A pointer to a function to be used to compare keys.
* @key_free_func: A pointer to a function (or NULL) to be called to
* de-allocate memory for keys on remove/kill.
* @value_free_func: A pointer to a function (or NULL) to be called to
* de-allocate memory for values on remove/kill.
*
* Returns: Pointer to a new table.
*/
table *table_empty(compare_function key_cmp_func,
free_function key_free_func,
free_function value_free_func);
/**
* table_is_empty() - Check if a table is empty.
* @t: Table to check.
*
* Returns: True if table contains no key/value pairs, false otherwise.
*/
bool table_is_empty(const table *t);
/**
* table_insert() - Add a key/value pair to a table.
* @t: Table to manipulate.
* @key: A pointer to the key value.
* @value: A pointer to the value value.
*
* Insert the key/value pair into the table. No test is performed to
* check if key is a duplicate. table_lookup() will return the latest
* added value for a duplicate key. table_remove() will remove all
* duplicates for a given key.
*
* Returns: Nothing.
*/
void table_insert(table *t, void *key, void *value);
/**
* table_lookup() - Look up a given key in a table.
* @t: Table to inspect.
* @key: Key to look up.
*
* Returns: The value corresponding to a given key, or NULL if the key
* is not found in the table. If the table contains duplicate keys,
* the value that was latest inserted will be returned.
*/
void *table_lookup(const table *t, const void *key);
/**
* table_choose_key() - Return an arbitrary key.
* @t: Table to inspect.
*
* Return an arbitrary key stored in the table. Can be used together
* with table_remove() to deconstruct the table if no duplicates have
* been stored in the table. Undefined for an empty table.
*
* Returns: An arbitrary key stored in the table.
*/
void *table_choose_key(const table *t);
/**
* table_remove() - Remove a key/value pair in the table.
* @t: Table to manipulate.
* @key: Key for which to remove pair.
*
* Any matching duplicates will be removed. Will call any free
* functions set for keys/values. Does nothing if key is not found in
* the table.
*
* Returns: Nothing.
*/
void table_remove(table *t, const void *key);
/**
* table_kill() - Destroy a table.
* @t: Table to destroy.
*
* Return all dynamic memory used by the table and its elements. If a
* free_func was registered for keys and/or values at table creation,
* it is called each element to free any user-allocated memory
* occupied by the element values.
*
* Returns: Nothing.
*/
void table_kill(table *t);
/**
* table_print() - Print the given table.
* @t: Table to print.
* @print_func: Function called for each key/value pair in the table.
*
* Iterates over the key/value pairs in the table and prints them.
* Will print all stored elements, including duplicates.
*
* Returns: Nothing.
*/
void table_print(const table *t, inspect_callback_pair print_func);
#endif