/
rshash.h
146 lines (116 loc) · 3.94 KB
/
rshash.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
/*
* Copyright (c) 2010, Richard Schwarz, git@richardschwarz.de
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Real Simple Hashing Module
*
* for void* -> void* mappings
*/
#ifndef _RSHASH_H_
#define _RSHASH_H_
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
/* initial size of an empty hash, power of two needed */
#define RSH_INIT_SIZE 32
/* default value for non existing keys */
#define RSH_DEFAULT_VALUE NULL
/* while adding, a load > RSH_MAX_LOAD causes growth */
#define RSH_MAX_LOAD 0.6
/* while deleting, a load < RSH_MIN_LOAD causes shrinking */
#define RSH_MIN_LOAD 0.125
/* Type definitions */
typedef struct rshash_s rshash_t;
typedef struct rshash_iterator_s rshash_iterator_t;
/* Function to deallocate a key. */
typedef void (*rshash_key_dealloc_t)(void*);
/* Function to deallocate a value. */
typedef void (*rshash_value_dealloc_t)(void*);
/* Function prototypes */
/*
* Allocates memory for a new rshash_t and returns a pointer to it.
* Also allocates memory for the table, and sets all rows to NULL.
* Sets size to RSH_INIT_SIZE and occupied and deleted to 0.
*/
rshash_t* rshash_init(rshash_key_dealloc_t, rshash_value_dealloc_t);
/*
* Free's all memory that the hash consumes. Free's every row, the
* table, and the hash itself. If deallocation functions are not NULL,
* use them to free keys and values.
*
*/
void rshash_free(void*);
/*
* Returns iterator over hash _hash_.
*/
rshash_iterator_t* rshash_iterator(const rshash_t* hash);
/*
* Advances the iterator.
*/
rshash_iterator_t* rshash_iterator_next(rshash_iterator_t* iter);
/*
* Returns the key of the current row the iterator points to.
*/
const void* rshash_iterator_key(const rshash_iterator_t*);
/*
* Returns the value of the current row the iterator points to.
*/
void* rshash_iterator_value(const rshash_iterator_t*);
/*
* Returns a 32 bit hash value for the first _len_ bytes of a given key
* _key_. The hash function is hash(i) = hash(i-1) * 8719 (for each byte
* i in _key_). With some bit mangling afterwards.
*/
uint32_t rshash_hash(const void* key, size_t len);
/*
* Return integral number of populated rows in _h_.
*/
size_t rshash_size(const rshash_t* h);
/*
* Return true if the given _key_ with length _len_ is a key in the
* table of _h_, according to the comparison function.
*/
bool rshash_has(const rshash_t* h, const void* key, size_t len);
/*
* Delete the given _key_ with length _len_ from _h_, or do nothing if
* _key_ is not in _h_.
*/
void rshash_del(rshash_t* h, const void* key, size_t len);
/*
* Add _key_/_value_ pair to hash. Returns false if _key_ exists in _h_,
* true if adding was successfull.
*/
bool rshash_add(rshash_t* h, const void* key, size_t len, void* value);
/*
* Returns value of given _key_ or RSH_DEFAULT_VALUE if _key_ doesn't
* exist in _h_.
*/
void* rshash_get(const rshash_t* h, const void* key, size_t len);
/*
* Set the value of _key_ to _value_. If _key_ doesn't exist in _h_, do
* nothing.
*/
void rshash_set(rshash_t* h, const void* key, size_t len, void* value);
/*
* Returns array of keys that populate hash _h_.
*/
const void** rshash_keys(rshash_t* h);
/*
* Returns array of values that populate hash _h_.
*/
void** rshash_values(rshash_t* h);
#endif /* _RSHASH_H_ */