xref: /freebsd/contrib/libucl/src/ucl_hash.h (revision 0e97acdf58fe27b09c4824a474b0344daf997c5f)
1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *       * Redistributions of source code must retain the above copyright
7  *         notice, this list of conditions and the following disclaimer.
8  *       * Redistributions in binary form must reproduce the above copyright
9  *         notice, this list of conditions and the following disclaimer in the
10  *         documentation and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #ifndef __UCL_HASH_H
25 #define __UCL_HASH_H
26 
27 #include "ucl.h"
28 #include "uthash.h"
29 
30 /******************************************************************************/
31 
32 typedef struct ucl_hash_node_s
33 {
34 	const ucl_object_t *data;
35 	UT_hash_handle hh;
36 } ucl_hash_node_t;
37 
38 typedef int ucl_hash_cmp_func (const void* void_a, const void* void_b);
39 typedef void ucl_hash_free_func (void *ptr);
40 typedef void* ucl_hash_iter_t;
41 
42 
43 /**
44  * Linear chained hashtable.
45  */
46 typedef struct ucl_hash_struct
47 {
48 	ucl_hash_node_t *buckets; /**< array of hash buckets. One list for each hash modulus. */
49 } ucl_hash_t;
50 
51 
52 /**
53  * Initializes the hashtable.
54  */
55 ucl_hash_t* ucl_hash_create (void);
56 
57 /**
58  * Deinitializes the hashtable.
59  */
60 void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func);
61 
62 /**
63  * Inserts an element in the the hashtable.
64  */
65 void ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, const char *key,
66 		unsigned keylen);
67 
68 /**
69  * Delete an element from the the hashtable.
70  */
71 void ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj);
72 
73 /**
74  * Searches an element in the hashtable.
75  */
76 const ucl_object_t* ucl_hash_search (ucl_hash_t* hashlin, const char *key,
77 		unsigned keylen);
78 
79 
80 /**
81  * Iterate over hash table
82  * @param hashlin hash
83  * @param iter iterator (must be NULL on first iteration)
84  * @return the next object
85  */
86 const void* ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter);
87 
88 /**
89  * Check whether an iterator has next element
90  */
91 bool ucl_hash_iter_has_next (ucl_hash_iter_t iter);
92 
93 #endif
94