-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoa_hash.h
122 lines (109 loc) · 3.58 KB
/
oa_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef OA_HASH_H
#define OA_HASH_H
#include <stddef.h>
/** @brief Hash table entry state */
enum oa_hash_entry_state {
OA_HASH_ENTRY_EMPTY = 0, /**< empty entry */
OA_HASH_ENTRY_OCCUPIED, /**< occupied entry */
OA_HASH_ENTRY_DELETED /**< deleted entry */
};
/** @brief Entry holding key-value pair in hash table */
struct oa_hash_entry {
enum oa_hash_entry_state state; /**< entry state */
struct {
const char *buf; /**< key buffer */
size_t length; /**< key length */
} key;
void *value; /**< value pointer */
};
/** @brief Open addressing hash table */
struct oa_hash {
size_t length; /**< amount of entries */
size_t capacity; /**< total buckets capacity */
struct oa_hash_entry *buckets; /**< entries array */
};
/**
* @brief Initialize hash table with given buckets array
*
* @param ht the hash table to be initialized
* @param buckets pre-allocated array of entries
* @param capacity amount of buckets
*/
void oa_hash_init(struct oa_hash *ht,
struct oa_hash_entry *buckets,
const size_t capacity);
/**
* @brief Clean up hash table entries and struct
*
* @param ht the hash table to be cleaned
*/
void oa_hash_cleanup(struct oa_hash *ht);
/**
* @brief Retrieve entry by key
*
* @param ht the hash table
* @param key the key to search for
* @param key_len the key length
* @return entry if found, NULL otherwise
*/
struct oa_hash_entry *oa_hash_get_entry(struct oa_hash *ht,
const char *key,
const size_t key_len);
/**
* @brief Retrieve value by key (wrapper around oa_hash_get_entry)
*
* @param ht the hash table
* @param key the key to search for
* @param key_len the key length
* @return value if found, NULL otherwise
*/
void *oa_hash_get(struct oa_hash *ht, const char *key, const size_t key_len);
/**
* @brief Insert or update entry
*
* @param ht the hash table
* @param key the key to insert/update
* @param key_len the key length
* @param value the value to be assigned
* @return entry if successful, or NULL if no space left, in which case
* oa_hash_rehash() should be called
*/
struct oa_hash_entry *oa_hash_set_entry(struct oa_hash *ht,
const char *key,
const size_t key_len,
void *value);
/**
* @brief Insert or update entry (wrapper around oa_hash_set_entry)
*
* @param ht the hash table
* @param key the key to insert/update
* @param key_len the key length
* @param value the value to be assigned
* @return value if successful, or NULL if no space left, in which case
* oa_hash_rehash() should be called
*/
void *oa_hash_set(struct oa_hash *ht,
const char *key,
const size_t key_len,
void *value);
/**
* @brief Remove entry by key
*
* @param ht the hash table
* @param key the key to be removed
* @param key_len the key length
* @return 1 if found and removed, 0 otherwise
*/
int oa_hash_remove(struct oa_hash *ht, const char *key, const size_t key_len);
/**
* @brief Rehash table to new buckets array
*
* @param ht the hash table
* @param new_buckets the new buckets array
* @param new_capacity the new buckets capacity
* @return pointer to old (now unused) bucket if successful, or NULL otherwise
*/
struct oa_hash_entry *oa_hash_rehash(struct oa_hash *ht,
struct oa_hash_entry *new_buckets,
const size_t new_capacity);
#endif /* OA_HASH_H */