-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhash_table.h
164 lines (144 loc) · 4.49 KB
/
hash_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @file hash_table.h
*
* @author hutusi ([email protected])
*
* @brief Hash table.
*
* @date 2019-07-25
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_HASH_TABLE_H
#define RETHINK_C_HASH_TABLE_H
/**
* @brief The type of a key to be stored in a @ref HashTable.
* (void *) can be changed to int, char *, or other types if needed.
*/
typedef void *HashTableKey;
/**
* @brief The type of a key to be stored in a @ref HashTable.
* (void *) can be changed to int, char *, or other types if needed.
*/
typedef void *HashTableValue;
/**
* A null @ref HashTableValue.
*/
#define HASH_TABLE_VALUE_NULL ((void *)0)
/**
* @brief Definition of a @ref HashTableEntity.
*
*/
typedef struct _HashTableEntity {
HashTableKey key;
HashTableValue value;
struct _HashTableEntity *next;
} HashTableEntity;
typedef unsigned int (*HashTableHashFunc)(HashTableKey key);
typedef int (*HashTableEqualFunc)(HashTableKey key1, HashTableKey key2);
typedef void (*HashTableFreeKeyFunc)(HashTableKey key);
typedef void (*HashTableFreeValueFunc)(HashTableValue value);
/**
* @brief Definition of a @ref HashTable.
*
*/
typedef struct _HashTable HashTable;
/**
* @brief Allcate a new HashTable.
*
* @param hash_func The hash function.
* @param equal_func The equal function that compare keys.
* @param free_key_func The free function that free keys.
* @param free_value_func The free function that free values.
* @return HashTable* The new HashTable if success, otherwise NULL.
*/
HashTable *hash_table_new(HashTableHashFunc hash_func,
HashTableEqualFunc equal_func,
HashTableFreeKeyFunc free_key_func,
HashTableFreeKeyFunc free_value_func);
/**
* @brief Delete a HashTable and free back memory.
*
* @param hash_table The HashTable to delete.
*/
void hash_table_free(HashTable *hash_table);
/**
* @brief Insert a key/value pair to a HashTable.
*
* @param hash_table The HashTable.
* @param key The key.
* @param value The value.
* @return int 0 if success.
*/
int hash_table_insert(HashTable *hash_table,
HashTableKey key,
HashTableValue value);
/**
* @brief Get the value by a key from a HashTable.
*
* @param hash_table The HashTable.
* @param key The key.
* @return HashTableValue The value.
*/
HashTableValue hash_table_get(HashTable *hash_table, HashTableKey key);
/**
* @brief Set a value by a key to a HashTable, insert a key/value pair if key
* not exists in HashTable.
*
* @param hash_table The HashTable.
* @param key The key.
* @param value The value.
* @return int 0 if success.
*/
int hash_table_set(HashTable *hash_table,
HashTableKey key,
HashTableValue value);
/**
* @brief Delete an entity by a key from a HashTable.
*
* @param hash_table The HashTable.
* @param key The key.
* @return int 0 if success.
*/
int hash_table_delete(HashTable *hash_table, HashTableKey key);
/**
* @brief Get the size of a HashTable.
*
* @param hash_table The HashTable.
* @return unsigned int The size of HashTable.
*/
unsigned int hash_table_size(const HashTable *hash_table);
/**
* @brief Get the first entity of a HashTable.
*
* @param hash_table The HashTable.
* @return HashTableEntity* The first entity.
*/
HashTableEntity *hash_table_first_entity(const HashTable *hash_table);
/**
* @brief Get the last entity of a HashTable.
*
* @param hash_table The HashTable.
* @return HashTableEntity* The last entity.
*/
HashTableEntity *hash_table_last_entity(const HashTable *hash_table);
/**
* @brief Get the next entity of a HashTable by given entity.
*
* @param hash_table The HashTable.
* @param entity The given entity.
* @return HashTableEntity* The next entity.
*/
HashTableEntity *hash_table_next_entity(const HashTable *hash_table,
HashTableEntity *entity);
/**
* @brief Get the previous entity of a HashTable by given entity.
*
* @param hash_table The HashTable.
* @param entity The given entity.
* @return HashTableEntity* The previous entity.
*/
HashTableEntity *hash_table_prev_entity(const HashTable *hash_table,
HashTableEntity *entity);
#endif /* #ifndef RETHINK_C_HASH_TABLE_H */