exip
Alpha 0.5.4
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
•
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
Pages
src
common
include
hashtable.h
Go to the documentation of this file.
1
/* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
2
/* @par[Revision] $Id: hashtable.h 310 2013-08-05 09:43:03Z kjussakov $
3
**/
4
5
#ifndef __HASHTABLE_CWC22_H__
6
#define __HASHTABLE_CWC22_H__
7
8
#include "exipConfig.h"
9
#include "procTypes.h"
10
22
uint32_t
djbHash
(
String
str);
23
24
struct
hashtable
;
25
26
/* Example of use:
27
*
28
* struct hashtable *h;
29
* struct some_key *k;
30
* struct some_value *v;
31
*
32
* static unsigned int hash_from_key_fn( void *k );
33
* static int keys_equal_fn ( void *key1, void *key2 );
34
*
35
* h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
36
* k = (struct some_key *) malloc(sizeof(struct some_key));
37
* v = (struct some_value *) malloc(sizeof(struct some_value));
38
*
39
* (initialise k and v to suitable values)
40
*
41
* if (! hashtable_insert(h,k,v) )
42
* { exit(-1); }
43
*
44
* if (NULL == (found = hashtable_search(h,k) ))
45
* { printf("not found!"); }
46
*
47
* if (NULL == (found = hashtable_remove(h,k) ))
48
* { printf("Not found\n"); }
49
*
50
*/
51
52
/* Macros may be used to define type-safe(r) hashtable access functions, with
53
* methods specialized to take known key and value types as parameters.
54
*
55
* Example:
56
*
57
* Insert this at the start of your file:
58
*
59
* DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
60
* DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
61
* DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
62
*
63
* This defines the functions 'insert_some', 'search_some' and 'remove_some'.
64
* These operate just like hashtable_insert etc., with the same parameters,
65
* but their function signatures have 'struct some_key *' rather than
66
* 'void *', and hence can generate compile time errors if your program is
67
* supplying incorrect data as a key (and similarly for value).
68
*
69
* Note that the hash and key equality functions passed to create_hashtable
70
* still take 'void *' parameters instead of 'some key *'. This shouldn't be
71
* a difficult issue as they're only defined and passed once, and the other
72
* functions will ensure that only valid keys are supplied to them.
73
*
74
* The cost for this checking is increased code size and runtime overhead
75
* - if performance is important, it may be worth switching back to the
76
* unsafe methods once your program has been debugged with the safe methods.
77
* This just requires switching to some simple alternative defines - eg:
78
* #define insert_some hashtable_insert
79
*
80
*/
81
82
/*****************************************************************************
83
* create_hashtable
84
85
* @name create_hashtable
86
* @param minsize minimum initial size of hashtable
87
* @param hashfunction function for hashing keys
88
* @param key_eq_fn function for determining key equality
89
* @return newly created hashtable or NULL on failure
90
*/
91
92
struct
hashtable
*
93
create_hashtable
(
unsigned
int
minsize,
94
uint32_t
(*
hashfn
) (
String
key),
95
boolean
(*
eqfn
) (
const
String
str1,
const
String
str2));
96
97
/*****************************************************************************
98
* hashtable_insert
99
100
* @name hashtable_insert
101
* @param h the hashtable to insert into
102
* @param k the key - hashtable claims ownership and will free on removal
103
* @param v the value - does not claim ownership
104
* @return EXIP_OK for successful insertion
105
*
106
* This function will cause the table to expand if the insertion would take
107
* the ratio of entries to table size over the maximum load factor.
108
*
109
* This function does not check for repeated insertions with a duplicate key.
110
* The value returned when using a duplicate key is undefined -- when
111
* the hashtable changes size, the order of retrieval of duplicate key
112
* entries is reversed.
113
* If in doubt, remove before insert.
114
*/
115
116
errorCode
hashtable_insert
(
struct
hashtable
*h,
String
key,
Index
value);
117
118
/*
119
//#define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
120
//int fnname (struct hashtable *h, keytype *k, unsigned int len, valuetype *v) \
121
//{ \
122
// return hashtable_insert(h,k,len,v); \
123
//}
124
*/
125
126
/*****************************************************************************
127
* hashtable_search
128
129
* @name hashtable_search
130
* @param h the hashtable to search
131
* @param k the key to search for - does not claim ownership
132
* @return the value associated with the key, or INDEX_MAX if none found
133
*/
134
135
Index
hashtable_search
(
struct
hashtable
*h,
String
key);
136
137
/*
138
//#define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
139
//valuetype * fnname (struct hashtable *h, keytype *k, unsigned int len) \
140
//{ \
141
// return (valuetype *) (hashtable_search(h,k,len)); \
142
//}
143
*/
144
145
/*****************************************************************************
146
* hashtable_remove
147
148
* @name hashtable_remove
149
* @param h the hashtable to remove the item from
150
* @param k the key to search for - does not claim ownership
151
* @return the value associated with the key, or INDEX_MAX if none found
152
*/
153
154
Index
hashtable_remove
(
struct
hashtable
*h,
String
key);
155
156
/*
157
//#define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \
158
//valuetype * fnname (struct hashtable *h, keytype *k, unsigned int len) \
159
//{ \
160
// return (valuetype *) (hashtable_remove(h,k,len)); \
161
//}
162
*/
163
164
/*****************************************************************************
165
* hashtable_count
166
167
* @name hashtable_count
168
* @param h the hashtable
169
* @return the number of items stored in the hashtable
170
*/
171
unsigned
int
hashtable_count
(
struct
hashtable
*h);
172
173
174
/*****************************************************************************
175
* hashtable_destroy
176
177
* @name hashtable_destroy
178
* @param h the hashtable
179
* @param free_values whether to call 'free' on the remaining values
180
*/
181
182
void
hashtable_destroy
(
struct
hashtable
*h);
183
184
#endif
/* __HASHTABLE_CWC22_H__ */
185
186
/*
187
* Copyright (c) 2002, Christopher Clark
188
* All rights reserved.
189
*
190
* Redistribution and use in source and binary forms, with or without
191
* modification, are permitted provided that the following conditions
192
* are met:
193
*
194
* * Redistributions of source code must retain the above copyright
195
* notice, this list of conditions and the following disclaimer.
196
*
197
* * Redistributions in binary form must reproduce the above copyright
198
* notice, this list of conditions and the following disclaimer in the
199
* documentation and/or other materials provided with the distribution.
200
*
201
* * Neither the name of the original author; nor the names of any contributors
202
* may be used to endorse or promote products derived from this software
203
* without specific prior written permission.
204
*
205
*
206
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
207
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
209
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
210
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
211
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
212
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
213
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
214
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
215
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
216
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
217
*/
Generated on Thu Nov 27 2014 10:56:09 for exip by
1.8.4