sdsykes-ferret 0.11.6.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +102 -0
  4. data/Rakefile +338 -0
  5. data/TODO +17 -0
  6. data/TUTORIAL +231 -0
  7. data/bin/ferret-browser +79 -0
  8. data/ext/analysis.c +1555 -0
  9. data/ext/analysis.h +219 -0
  10. data/ext/api.c +69 -0
  11. data/ext/api.h +27 -0
  12. data/ext/array.c +123 -0
  13. data/ext/array.h +53 -0
  14. data/ext/bitvector.c +540 -0
  15. data/ext/bitvector.h +272 -0
  16. data/ext/compound_io.c +383 -0
  17. data/ext/config.h +42 -0
  18. data/ext/document.c +156 -0
  19. data/ext/document.h +53 -0
  20. data/ext/except.c +120 -0
  21. data/ext/except.h +168 -0
  22. data/ext/extconf.rb +14 -0
  23. data/ext/ferret.c +402 -0
  24. data/ext/ferret.h +91 -0
  25. data/ext/filter.c +156 -0
  26. data/ext/fs_store.c +483 -0
  27. data/ext/global.c +418 -0
  28. data/ext/global.h +117 -0
  29. data/ext/hash.c +567 -0
  30. data/ext/hash.h +473 -0
  31. data/ext/hashset.c +170 -0
  32. data/ext/hashset.h +187 -0
  33. data/ext/header.h +58 -0
  34. data/ext/helper.c +62 -0
  35. data/ext/helper.h +13 -0
  36. data/ext/inc/lang.h +48 -0
  37. data/ext/inc/threading.h +31 -0
  38. data/ext/index.c +6425 -0
  39. data/ext/index.h +961 -0
  40. data/ext/lang.h +66 -0
  41. data/ext/libstemmer.c +92 -0
  42. data/ext/libstemmer.h +79 -0
  43. data/ext/mempool.c +87 -0
  44. data/ext/mempool.h +35 -0
  45. data/ext/modules.h +162 -0
  46. data/ext/multimapper.c +310 -0
  47. data/ext/multimapper.h +51 -0
  48. data/ext/posh.c +1006 -0
  49. data/ext/posh.h +1007 -0
  50. data/ext/priorityqueue.c +151 -0
  51. data/ext/priorityqueue.h +143 -0
  52. data/ext/q_boolean.c +1608 -0
  53. data/ext/q_const_score.c +161 -0
  54. data/ext/q_filtered_query.c +209 -0
  55. data/ext/q_fuzzy.c +268 -0
  56. data/ext/q_match_all.c +148 -0
  57. data/ext/q_multi_term.c +677 -0
  58. data/ext/q_parser.c +2825 -0
  59. data/ext/q_phrase.c +1126 -0
  60. data/ext/q_prefix.c +100 -0
  61. data/ext/q_range.c +350 -0
  62. data/ext/q_span.c +2402 -0
  63. data/ext/q_term.c +337 -0
  64. data/ext/q_wildcard.c +171 -0
  65. data/ext/r_analysis.c +2575 -0
  66. data/ext/r_index.c +3472 -0
  67. data/ext/r_qparser.c +585 -0
  68. data/ext/r_search.c +4105 -0
  69. data/ext/r_store.c +513 -0
  70. data/ext/r_utils.c +963 -0
  71. data/ext/ram_store.c +471 -0
  72. data/ext/search.c +1741 -0
  73. data/ext/search.h +885 -0
  74. data/ext/similarity.c +150 -0
  75. data/ext/similarity.h +82 -0
  76. data/ext/sort.c +983 -0
  77. data/ext/stem_ISO_8859_1_danish.c +338 -0
  78. data/ext/stem_ISO_8859_1_danish.h +16 -0
  79. data/ext/stem_ISO_8859_1_dutch.c +635 -0
  80. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  81. data/ext/stem_ISO_8859_1_english.c +1156 -0
  82. data/ext/stem_ISO_8859_1_english.h +16 -0
  83. data/ext/stem_ISO_8859_1_finnish.c +792 -0
  84. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  85. data/ext/stem_ISO_8859_1_french.c +1276 -0
  86. data/ext/stem_ISO_8859_1_french.h +16 -0
  87. data/ext/stem_ISO_8859_1_german.c +512 -0
  88. data/ext/stem_ISO_8859_1_german.h +16 -0
  89. data/ext/stem_ISO_8859_1_italian.c +1091 -0
  90. data/ext/stem_ISO_8859_1_italian.h +16 -0
  91. data/ext/stem_ISO_8859_1_norwegian.c +296 -0
  92. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  93. data/ext/stem_ISO_8859_1_porter.c +776 -0
  94. data/ext/stem_ISO_8859_1_porter.h +16 -0
  95. data/ext/stem_ISO_8859_1_portuguese.c +1035 -0
  96. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  97. data/ext/stem_ISO_8859_1_spanish.c +1119 -0
  98. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  99. data/ext/stem_ISO_8859_1_swedish.c +307 -0
  100. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  101. data/ext/stem_KOI8_R_russian.c +701 -0
  102. data/ext/stem_KOI8_R_russian.h +16 -0
  103. data/ext/stem_UTF_8_danish.c +344 -0
  104. data/ext/stem_UTF_8_danish.h +16 -0
  105. data/ext/stem_UTF_8_dutch.c +653 -0
  106. data/ext/stem_UTF_8_dutch.h +16 -0
  107. data/ext/stem_UTF_8_english.c +1176 -0
  108. data/ext/stem_UTF_8_english.h +16 -0
  109. data/ext/stem_UTF_8_finnish.c +808 -0
  110. data/ext/stem_UTF_8_finnish.h +16 -0
  111. data/ext/stem_UTF_8_french.c +1296 -0
  112. data/ext/stem_UTF_8_french.h +16 -0
  113. data/ext/stem_UTF_8_german.c +526 -0
  114. data/ext/stem_UTF_8_german.h +16 -0
  115. data/ext/stem_UTF_8_italian.c +1113 -0
  116. data/ext/stem_UTF_8_italian.h +16 -0
  117. data/ext/stem_UTF_8_norwegian.c +302 -0
  118. data/ext/stem_UTF_8_norwegian.h +16 -0
  119. data/ext/stem_UTF_8_porter.c +794 -0
  120. data/ext/stem_UTF_8_porter.h +16 -0
  121. data/ext/stem_UTF_8_portuguese.c +1055 -0
  122. data/ext/stem_UTF_8_portuguese.h +16 -0
  123. data/ext/stem_UTF_8_russian.c +709 -0
  124. data/ext/stem_UTF_8_russian.h +16 -0
  125. data/ext/stem_UTF_8_spanish.c +1137 -0
  126. data/ext/stem_UTF_8_spanish.h +16 -0
  127. data/ext/stem_UTF_8_swedish.c +313 -0
  128. data/ext/stem_UTF_8_swedish.h +16 -0
  129. data/ext/stopwords.c +401 -0
  130. data/ext/store.c +692 -0
  131. data/ext/store.h +777 -0
  132. data/ext/term_vectors.c +352 -0
  133. data/ext/threading.h +31 -0
  134. data/ext/utilities.c +446 -0
  135. data/ext/win32.h +54 -0
  136. data/lib/ferret.rb +29 -0
  137. data/lib/ferret/browser.rb +246 -0
  138. data/lib/ferret/browser/s/global.js +192 -0
  139. data/lib/ferret/browser/s/style.css +148 -0
  140. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  141. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  142. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  143. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  144. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  145. data/lib/ferret/browser/views/layout.rhtml +22 -0
  146. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  147. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  148. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  149. data/lib/ferret/browser/webrick.rb +14 -0
  150. data/lib/ferret/document.rb +130 -0
  151. data/lib/ferret/field_infos.rb +44 -0
  152. data/lib/ferret/index.rb +786 -0
  153. data/lib/ferret/number_tools.rb +157 -0
  154. data/lib/ferret_version.rb +3 -0
  155. data/setup.rb +1555 -0
  156. data/test/test_all.rb +5 -0
  157. data/test/test_helper.rb +24 -0
  158. data/test/threading/number_to_spoken.rb +132 -0
  159. data/test/threading/thread_safety_index_test.rb +79 -0
  160. data/test/threading/thread_safety_read_write_test.rb +76 -0
  161. data/test/threading/thread_safety_test.rb +133 -0
  162. data/test/unit/analysis/tc_analyzer.rb +548 -0
  163. data/test/unit/analysis/tc_token_stream.rb +646 -0
  164. data/test/unit/index/tc_index.rb +762 -0
  165. data/test/unit/index/tc_index_reader.rb +699 -0
  166. data/test/unit/index/tc_index_writer.rb +437 -0
  167. data/test/unit/index/th_doc.rb +315 -0
  168. data/test/unit/largefile/tc_largefile.rb +46 -0
  169. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  170. data/test/unit/search/tc_filter.rb +135 -0
  171. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  172. data/test/unit/search/tc_index_searcher.rb +61 -0
  173. data/test/unit/search/tc_multi_searcher.rb +128 -0
  174. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  175. data/test/unit/search/tc_search_and_sort.rb +179 -0
  176. data/test/unit/search/tc_sort.rb +49 -0
  177. data/test/unit/search/tc_sort_field.rb +27 -0
  178. data/test/unit/search/tc_spans.rb +190 -0
  179. data/test/unit/search/tm_searcher.rb +384 -0
  180. data/test/unit/store/tc_fs_store.rb +77 -0
  181. data/test/unit/store/tc_ram_store.rb +35 -0
  182. data/test/unit/store/tm_store.rb +34 -0
  183. data/test/unit/store/tm_store_lock.rb +68 -0
  184. data/test/unit/tc_document.rb +81 -0
  185. data/test/unit/ts_analysis.rb +2 -0
  186. data/test/unit/ts_index.rb +2 -0
  187. data/test/unit/ts_largefile.rb +4 -0
  188. data/test/unit/ts_query_parser.rb +2 -0
  189. data/test/unit/ts_search.rb +2 -0
  190. data/test/unit/ts_store.rb +2 -0
  191. data/test/unit/ts_utils.rb +2 -0
  192. data/test/unit/utils/tc_bit_vector.rb +295 -0
  193. data/test/unit/utils/tc_number_tools.rb +117 -0
  194. data/test/unit/utils/tc_priority_queue.rb +106 -0
  195. metadata +285 -0
@@ -0,0 +1,473 @@
1
+ #ifndef FRT_HASH_H
2
+ #define FRT_HASH_H
3
+
4
+ #include "global.h"
5
+
6
+ /****************************************************************************
7
+ *
8
+ * HashTable
9
+ *
10
+ ****************************************************************************/
11
+
12
+ #define HASH_MINSIZE 8
13
+ #define SLOW_DOWN 50000 /* stop increasing the hash table so quickly to
14
+ * conserve memory */
15
+
16
+ /**
17
+ * Return values for h_set
18
+ */
19
+ enum HashSetValues
20
+ {
21
+ HASH_KEY_DOES_NOT_EXIST = 0,
22
+ HASH_KEY_EQUAL = 1,
23
+ HASH_KEY_SAME = 2
24
+ };
25
+
26
+ /**
27
+ * struct used internally to store values in the HashTable
28
+ */
29
+ typedef struct
30
+ {
31
+ unsigned long hash;
32
+ void *key;
33
+ void *value;
34
+ } HashEntry;
35
+
36
+ /**
37
+ * As the hash table is filled and entries are deleted, Dummy HashEntries are
38
+ * put in place. We therefor keep two counts. +size+ is the number active
39
+ * elements and +fill+ is the number of active elements together with the
40
+ * number of dummy elements. +fill+ is basically just kept around so that we
41
+ * know when to resize. The HashTable is resized when more than two thirds of
42
+ * the HashTable is Filled.
43
+ */
44
+ typedef struct HashTable
45
+ {
46
+ int fill; /* num Active + num Dummy */
47
+ int size; /* num Active ie, num keys set */
48
+ int mask; /* capacity_of_table - 1 */
49
+ int ref_cnt;
50
+
51
+ /* table points to smalltable initially. If the table grows beyond 2/3 of
52
+ * HASH_MINSIZE it will point to newly malloced memory as it grows. */
53
+ HashEntry *table;
54
+
55
+ /* When a HashTable is created it needs an initial table to start if off.
56
+ * All HashTables will start with smalltable and then malloc a larger
57
+ * table as the HashTable grows */
58
+ HashEntry smalltable[HASH_MINSIZE];
59
+
60
+ /* the following function pointers are used internally and should not be
61
+ * used outside of the HashTable methods */
62
+ HashEntry *(*lookup_i)(struct HashTable *self, register const void *key);
63
+ unsigned long (*hash_i)(const void *key);
64
+ int (*eq_i)(const void *key1, const void *key2);
65
+ void (*free_key_i)(void *p);
66
+ void (*free_value_i)(void *p);
67
+ } HashTable;
68
+
69
+ /**
70
+ * Hashing function type used by HashTable. A function of this type must be
71
+ * passed to create a new HashTable.
72
+ *
73
+ * @param key object to hash
74
+ * @return an unsigned 32-bit integer hash value
75
+ */
76
+ typedef unsigned long (*hash_ft)(const void *key);
77
+
78
+ /**
79
+ * Equals function type used by HashTable. A function of this type must be
80
+ * passed to create a new HashTable.
81
+ */
82
+ typedef int (*eq_ft)(const void *key1, const void *key2);
83
+
84
+ /**
85
+ * Determine a hash value for a string. The string must be null terminated
86
+ *
87
+ * @param str string to hash
88
+ * @return an unsigned long integer hash value
89
+ */
90
+ extern unsigned long str_hash(const char *const str);
91
+
92
+ /**
93
+ * Determine a hash value for a pointer. Just cast the pointer to an unsigned
94
+ * long.
95
+ *
96
+ * @param ptr pointer to hash
97
+ * @return an unsigned long integer hash value
98
+ */
99
+ extern unsigned long ptr_hash(const void *const ptr);
100
+
101
+ /**
102
+ * Determine if two pointers point to the same point in memory.
103
+ *
104
+ * @param q1 first pointer
105
+ * @param q2 second pointer
106
+ * @return true if the pointers are equal
107
+ */
108
+ extern int ptr_eq(const void *q1, const void *q2);
109
+
110
+ /**
111
+ * Create a new HashTable that uses any type of object as it's key. The
112
+ * HashTable will store all keys and values so if you want to destroy those
113
+ * values when the HashTable is destroyed then you should pass free functions.
114
+ * NULL will suffice otherwise.
115
+ *
116
+ * @param hash function to determine the hash value of a key in the HashTable
117
+ * @param eq function to determine the equality of to keys in the HashTable
118
+ * @param free_key function to free the key stored in the HashTable when an
119
+ * entry is deleted, replaced or when the HashTable is destroyed. If you
120
+ * pass NULL in place of this parameter the key will not be destroyed.
121
+ * @param free_value function to free the value stored in the HashTable when
122
+ * an entry is deleted, replaced or when the HashTable is destroyed. If you
123
+ * pass NULL in place of this parameter the value will not be destroyed.
124
+ * @return A newly allocated HashTable
125
+ */
126
+ extern HashTable *h_new(hash_ft hash, eq_ft eq, free_ft free_key,
127
+ free_ft free_value);
128
+
129
+ /**
130
+ * Create a new HashTable that uses null-terminated strings as it's keys. The
131
+ * HashTable will store all keys and values so if you want to destroy those
132
+ * values when the HashTable is destroyed then you should pass free functions.
133
+ * NULL will suffice otherwise.
134
+ *
135
+ * @param free_key function to free the key stored in the HashTable when an
136
+ * entry is deleted, replaced or when the HashTable is destroyed. If you
137
+ * pass NULL in place of this parameter the key will not be destroyed.
138
+ * @param free_value function to free the value stored in the HashTable when
139
+ * an entry is deleted, replaced or when the HashTable is destroyed. If you
140
+ * pass NULL in place of this parameter the value will not be destroyed.
141
+ * @return A newly allocated HashTable
142
+ */
143
+ extern HashTable *h_new_str(free_ft free_key, free_ft free_value);
144
+
145
+ /**
146
+ * Create a new HashTable that uses integers as it's keys. The
147
+ * HashTable will store all values so if you want to destroy those
148
+ * values when the HashTable is destroyed then you should pass a free function.
149
+ * NULL will suffice otherwise.
150
+ *
151
+ * @param free_value function to free the value stored in the HashTable when
152
+ * an entry is deleted, replaced or when the HashTable is destroyed. If you
153
+ * pass NULL in place of this parameter the value will not be destroyed.
154
+ * @return A newly allocated HashTable
155
+ */
156
+ extern HashTable *h_new_int(free_ft free_value);
157
+
158
+ /**
159
+ * Destroy the HashTable. This function will also destroy all keys and values
160
+ * in the HashTable depending on how the free_key and free_value were set.
161
+ *
162
+ * @param self the HashTable to destroy
163
+ */
164
+ extern void h_destroy(HashTable *self);
165
+
166
+ /**
167
+ * Clear the HashTable. This function will delete all keys and values from the
168
+ * hash table, also destroying all keys and values in the HashTable depending
169
+ * on how the free_key and free_value were set.
170
+ *
171
+ * @param self the HashTable to clear
172
+ */
173
+ extern void h_clear(HashTable *self);
174
+
175
+ /**
176
+ * Get the value in the HashTable referenced by the key +key+.
177
+ *
178
+ * @param self the HashTable to reference
179
+ * @param key the key to lookup
180
+ * @return the value referenced by the key +key+. If there is no value
181
+ * referenced by that key, NULL is returned.
182
+ */
183
+ extern void *h_get(HashTable *self, const void *key);
184
+
185
+ /**
186
+ * Delete the value in HashTable referenced by the key +key+. When the value
187
+ * is deleted it is also destroyed along with the key depending on how
188
+ * free_key and free_value where set when the HashTable was created. If you
189
+ * don't want to destroy the value use h_rem.
190
+ *
191
+ * This functions returns +true+ if the value was deleted successfully or
192
+ * false if the key was not found.
193
+ *
194
+ * @see h_rem
195
+ *
196
+ * @param self the HashTable to reference
197
+ * @param key the key to lookup
198
+ * @return true if the object was successfully deleted or false if the key was
199
+ * not found
200
+ */
201
+ extern int h_del(HashTable *self, const void *key);
202
+
203
+ /**
204
+ * Remove the value in HashTable referenced by the key +key+. When the value
205
+ * is removed it is returned rather than destroyed. The key however is
206
+ * destroyed using the free_key functions passed when the HashTable is created
207
+ * if del_key is true.
208
+ *
209
+ * If you want the value to be destroyed, use the h_del function.
210
+ *
211
+ * @see h_del
212
+ *
213
+ * @param self the HashTable to reference
214
+ * @param key the key to lookup
215
+ * @param del_key set to true if you want the key to be deleted when the value
216
+ * is removed from the HashTable
217
+ * @return the value referenced by +key+ if it can be found or NULL otherwise
218
+ */
219
+ extern void *h_rem(HashTable *self, const void *key, bool del_key);
220
+
221
+ /**
222
+ * WARNING: this function may destroy an old value or key if the key already
223
+ * exists in the HashTable, depending on how free_value and free_key were set
224
+ * for this HashTable.
225
+ *
226
+ * Add the value +value+ to the HashTable referencing it with key +key+.
227
+ *
228
+ * When a value is added to the HashTable it replaces any value that
229
+ * might already be stored under that key. If free_value is already set then
230
+ * the old value will be freed using that function.
231
+ *
232
+ * Similarly the old key might replace be replaced by the new key if they are
233
+ * are equal (according to the HashTable's eq function) but seperately
234
+ * allocated objects.
235
+ *
236
+ * @param self the HashTable to add the value to
237
+ * @param key the key to use to reference the value
238
+ * @param value the value to add to the HashTable
239
+ * @return one of three values;
240
+ * <pre>
241
+ * HASH_KEY_DOES_NOT_EXIST there was no value stored with that key
242
+ * HASH_KEY_EQUAL the key existed and was seperately allocated.
243
+ * In this situation the old key will have been
244
+ * destroyed if free_key was set
245
+ * HASH_KEY_SAME the key was identical (same memory pointer) to
246
+ * the existing key so no key was freed
247
+ * </pre>
248
+ */
249
+ extern int h_set(HashTable *self, const void *key, void *value);
250
+
251
+ /**
252
+ * Add the value +value+ to the HashTable referencing it with key +key+. If
253
+ * the key already exists in the HashTable, the value won't be added and the
254
+ * function will return false. Otherwise it will return true.
255
+ *
256
+ * @param self the HashTable to add the value to
257
+ * @param key the key to use to reference the value
258
+ * @param value the value to add to the HashTable
259
+ * @return true if the value was successfully added or false otherwise
260
+ */
261
+ extern int h_set_safe(HashTable *self, const void *key, void *value);
262
+
263
+ /**
264
+ * Return a hash entry object so you can handle the insert yourself. This can
265
+ * be used for performance reasons or for more control over how a value is
266
+ * added. Say, for example, you wanted to append a value to an array, or add a
267
+ * new array if non-existed, you could use this method by checking the value
268
+ * of the HashEntry returned.
269
+ *
270
+ * @param self the HashTable to add the value to
271
+ * @param key the key to use to reference the value
272
+ * @return HashEntry a pointer to the hash entry object now reserved for this
273
+ * value. Be sure to set both the *key* and the *value*
274
+ */
275
+ extern HashEntry *h_set_ext(HashTable *ht, const void *key);
276
+
277
+ /**
278
+ * Check whether key +key+ exists in the HashTable.
279
+ *
280
+ * @param self the HashTable to check in
281
+ * @param key the key to check for in the HashTable
282
+ * @return true if the key exists in the HashTable, false otherwise.
283
+ */
284
+ extern int h_has_key(HashTable *self, const void *key);
285
+
286
+ /**
287
+ * Get the value in the HashTable referenced by an integer key +key+.
288
+ *
289
+ * @param self the HashTable to reference
290
+ * @param key the integer key to lookup
291
+ * @return the value referenced by the key +key+. If there is no value
292
+ * referenced by that key, NULL is returned.
293
+ */
294
+ extern void *h_get_int(HashTable *self, const unsigned long key);
295
+
296
+ /**
297
+ * Delete the value in HashTable referenced by the integer key +key+. When the
298
+ * value is deleted it is also destroyed using the free_value function. If you
299
+ * don't want to destroy the value use h_rem.
300
+ *
301
+ * This functions returns +true+ if the value was deleted successfully or
302
+ * false if the key was not found.
303
+ *
304
+ * @see h_rem
305
+ *
306
+ * @param self the HashTable to reference
307
+ * @param key the integer key to lookup
308
+ * @return true if the object was successfully deleted or false if the key was
309
+ * not found
310
+ */
311
+ extern int h_del_int(HashTable *self, const unsigned long key);
312
+
313
+ /**
314
+ * Remove the value in HashTable referenced by the integer key +key+. When the
315
+ * value is removed it is returned rather than destroyed.
316
+ *
317
+ * If you want the value to be destroyed, use the h_del function.
318
+ *
319
+ * @see h_del
320
+ *
321
+ * @param self the HashTable to reference
322
+ * @param key the integer key to lookup
323
+ * @return the value referenced by +key+ if it can be found or NULL otherwise
324
+ */
325
+ extern void *h_rem_int(HashTable *self, const unsigned long key);
326
+
327
+ /**
328
+ * WARNING: this function may destroy an old value if the key already exists
329
+ * in the HashTable, depending on how free_value was set for this HashTable.
330
+ *
331
+ * Add the value +value+ to the HashTable referencing it with an integer key
332
+ * +key+.
333
+ *
334
+ * When a value is added to the HashTable it replaces any value that
335
+ * might already be stored under that key. If free_value is already set then
336
+ * the old value will be freed using that function.
337
+ *
338
+ * Similarly the old key might replace be replaced by the new key if they are
339
+ * are equal (according to the HashTable's eq function) but seperately
340
+ * allocated objects.
341
+ *
342
+ * @param self the HashTable to add the value to
343
+ * @param key the integer key to use to reference the value
344
+ * @param value the value to add to the HashTable
345
+ * @return one of three values;
346
+ * <pre>
347
+ * HASH_KEY_DOES_NOT_EXIST there was no value stored with that key
348
+ * HASH_KEY_EQUAL the key existed and was seperately allocated.
349
+ * In this situation the old key will have been
350
+ * destroyed if free_key was set
351
+ * HASH_KEY_SAME the key was identical (same memory pointer) to
352
+ * the existing key so no key was freed
353
+ * </pre>
354
+ */
355
+ extern int h_set_int(HashTable *self, const unsigned long key, void *value);
356
+
357
+ /**
358
+ * Add the value +value+ to the HashTable referencing it with integer key
359
+ * +key+. If the key already exists in the HashTable, the value won't be added
360
+ * and the function will return false. Otherwise it will return true.
361
+ *
362
+ * @param self the HashTable to add the value to
363
+ * @param key the integer key to use to reference the value
364
+ * @param value the value to add to the HashTable
365
+ * @return true if the value was successfully added or false otherwise
366
+ */
367
+ extern int h_set_safe_int(HashTable *self, const unsigned long key, void *value);
368
+ /**
369
+ * Check whether integer key +key+ exists in the HashTable.
370
+ *
371
+ * @param self the HashTable to check in
372
+ * @param key the integer key to check for in the HashTable
373
+ * @return true if the key exists in the HashTable, false otherwise.
374
+ */
375
+ extern int h_has_key_int(HashTable *self, const unsigned long key);
376
+
377
+ typedef void (*h_each_key_val_ft)(void *key, void *value, void *arg);
378
+
379
+ /**
380
+ * Run function +each_key_val+ on each key and value in the HashTable. The third
381
+ * argument +arg+ will also be passed to +each_key_val+. If you need to pass
382
+ * more than one argument to the function you should pass a struct.
383
+ *
384
+ * example;
385
+ *
386
+ * // Lets say we have stored strings in a HashTable and we want to put them
387
+ * // all into an array. First we need to create a struct to store the
388
+ * // strings;
389
+ *
390
+ * struct StringArray {
391
+ * char **strings;
392
+ * int cnt;
393
+ * int size;
394
+ * };
395
+ *
396
+ * static void add_string_ekv(void *key, void *value,
397
+ * struct StringArray *str_arr)
398
+ * {
399
+ * str_arr->strings[str_arr->cnt] = (char *)value;
400
+ * str_arr->cnt++;
401
+ * }
402
+ *
403
+ * struct StringArray *h_extract_strings(HashTable *ht)
404
+ * {
405
+ * struct StringArray *str_arr = ALLOC(struct StringArray);
406
+ *
407
+ * str_arr->strings = ALLOC_N(char *, ht->size);
408
+ * str_arr->cnt = 0;
409
+ * str_arr->size = ht->size;
410
+ *
411
+ * h_each(ht, (h_each_key_val_ft)add_string_ekv, str_arr);
412
+ *
413
+ * return str_arr;
414
+ * }
415
+ *
416
+ * @param self the HashTable to run the function on
417
+ * @param each_key_val function to run on on each key and value in the
418
+ * HashTable
419
+ * @param arg an extra argument to pass to each_key_val each time it is called
420
+ */
421
+ extern void h_each(HashTable *self,
422
+ void (*each_key_val)(void *key, void *value, void *arg),
423
+ void *arg);
424
+
425
+ typedef void *(*h_clone_func_t)(void *val);
426
+ /**
427
+ * Clone the HashTable as well as cloning each of the keys and values if you
428
+ * want to do a deep clone. To do a deep clone you will need to pass a
429
+ * clone_key function and/or a clone_value function.
430
+ *
431
+ * @param self the HashTable to clone
432
+ * @param clone_key the function to clone the key with
433
+ * @param clone_value the function to clone the value with
434
+ * @return a clone of the original HashTable
435
+ */
436
+ extern HashTable *h_clone(HashTable *self,
437
+ h_clone_func_t clone_key,
438
+ h_clone_func_t clone_value);
439
+
440
+ /*
441
+ * The following functions should only be used in static HashTable
442
+ * declarations
443
+ */
444
+ /**
445
+ * This is the lookup function for a hash table keyed with strings. Since it
446
+ * is so common for hash tables to be keyed with strings it gets it's own
447
+ * lookup function. This method will always return a HashEntry. If there is no
448
+ * entry with the given key then an empty entry will be returned with the key
449
+ * set to the key that was passed.
450
+ *
451
+ * @param ht the hash table to look in
452
+ * @param key the key to lookup
453
+ * @return the HashEntry that was found
454
+ */
455
+ extern HashEntry *h_lookup_str(HashTable *ht, register const char *key);
456
+
457
+ /**
458
+ * This is the lookup function for a hash table with non-string keys. The
459
+ * hash() and eq() methods used are stored in the hash table. This method will
460
+ * always return a HashEntry. If there is no entry with the given key then an
461
+ * empty entry will be returned with the key set to the key that was passed.
462
+ *
463
+ * @param ht the hash table to look in
464
+ * @param key the key to lookup
465
+ * @return the HashEntry that was found
466
+ */
467
+ extern HashEntry *h_lookup(HashTable *ht, register const void *key);
468
+
469
+ typedef HashEntry *(*h_lookup_ft)(HashTable *ht, register const void *key);
470
+
471
+ extern void h_str_print_keys(HashTable *ht);
472
+
473
+ #endif