-
Notifications
You must be signed in to change notification settings - Fork 91
/
hash.h
106 lines (91 loc) · 3.11 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
+----------------------------------------------------------------------+
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author: Oleg Grenrus <[email protected]> |
| See CREDITS for contributors |
+----------------------------------------------------------------------+
*/
#ifndef HASH_H
#define HASH_H
#include <assert.h>
#ifdef PHP_WIN32
# include "ig_win32.h"
#else
# include <stdint.h> /* defines uint32_t etc */
#endif
#include <stddef.h>
/** Key/value pair of hash_si.
* @author Oleg Grenrus <[email protected]>
* @see hash_si
*/
struct hash_si_pair
{
char *key; /**< Pointer to key. */
size_t key_len; /**< Key length. */
uint32_t value; /**< Value. */
};
/** Hash-array.
* Like c++ map<char *, int32_t>.
* Current implementation uses linear probing.
* @author Oleg Grenrus <[email protected]>
*/
struct hash_si {
size_t size; /**< Allocated size of array. */
size_t used; /**< Used size of array. */
struct hash_si_pair *data; /**< Pointer to array or pairs of data. */
};
/** Inits hash_si structure.
* @param h pointer to hash_si struct.
* @param size initial size of the hash array.
* @return 0 on success, 1 else.
*/
int hash_si_init (struct hash_si *h, size_t size);
/** Frees hash_si structure.
* Doesn't call free(h).
* @param h pointer to hash_si struct.
*/
void hash_si_deinit (struct hash_si *h);
/** Inserts value into hash_si.
* @param h Pointer to hash_si struct.
* @param key Pointer to key.
* @param key_len Key length.
* @param value Value.
* @return 0 on success, 1 or 2 else.
*/
int hash_si_insert (struct hash_si *h, const char *key, size_t key_len, uint32_t value);
/** Finds value from hash_si.
* Value returned thru value param.
* @param h Pointer to hash_si struct.
* @param key Pointer to key.
* @param key_len Key length.
* @param[out] value Found value.
* @return 0 if found, 1 if not.
*/
int hash_si_find (struct hash_si *h, const char *key, size_t key_len, uint32_t * value);
/** Remove value from hash_si.
* Removed value is available thru value param.
* @param h Pointer to hash_si struct.
* @param key Pointer to key.
* @param key_len Key length.
* @param[out] value Removed value.
* @return 0 ivalue removed, 1 if not existed.
*/
int hash_si_remove (struct hash_si *h, const char *key, size_t key_len, uint32_t * value);
/** Travarses hash_si.
* Calls traverse_function on every item. Traverse function should not modify hash
* @param h Pointer to hash_si struct.
* @param traverse_function Function to call on every item of hash_si.
*/
void hash_si_traverse (struct hash_si *h, int (*traverse_function) (const char *key, size_t key_len, uint32_t value));
/** Returns size of hash_si.
* @param h Pointer to hash_si struct.
* @return Size of hash_si.
*/
size_t hash_si_size (struct hash_si *h);
/** Returns capacity of hash_si.
* @param h Pointer to hash_si struct.
* @return Capacity of hash_si.
*/
size_t hash_si_capacity (struct hash_si *h);
#endif /* HASH_H */