1 /* Copyright (c) 2013, Vsevolod Stakhov
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.
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.
24 #include "ucl_internal.h"
29 ucl_hash_create (void)
33 new = UCL_ALLOC (sizeof (ucl_hash_t));
40 void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func)
42 ucl_hash_node_t *elt, *tmp;
43 const ucl_object_t *cur, *otmp;
45 HASH_ITER (hh, hashlin->buckets, elt, tmp) {
46 HASH_DELETE (hh, hashlin->buckets, elt);
48 DL_FOREACH_SAFE (elt->data, cur, otmp) {
49 /* Need to deconst here */
50 func (__DECONST (ucl_object_t *, cur));
53 UCL_FREE (sizeof (ucl_hash_node_t), elt);
55 UCL_FREE (sizeof (ucl_hash_t), hashlin);
59 ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj,
60 const char *key, unsigned keylen)
62 ucl_hash_node_t *node;
64 node = UCL_ALLOC (sizeof (ucl_hash_node_t));
66 HASH_ADD_KEYPTR (hh, hashlin->buckets, key, keylen, node);
69 void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old,
70 const ucl_object_t *new)
72 ucl_hash_node_t *node;
74 HASH_FIND (hh, hashlin->buckets, old->key, old->keylen, node);
76 /* Direct replacement */
78 node->hh.key = new->key;
79 node->hh.keylen = new->keylen;
84 ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter)
86 ucl_hash_node_t *elt = *iter;
89 if (hashlin == NULL || hashlin->buckets == NULL) {
92 elt = hashlin->buckets;
97 else if (elt == hashlin->buckets) {
101 *iter = elt->hh.next ? elt->hh.next : hashlin->buckets;
106 ucl_hash_iter_has_next (ucl_hash_iter_t iter)
108 ucl_hash_node_t *elt = iter;
110 return (elt == NULL || elt->hh.prev != NULL);
115 ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen)
117 ucl_hash_node_t *found;
119 if (hashlin == NULL) {
122 HASH_FIND (hh, hashlin->buckets, key, keylen, found);
131 ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj)
133 ucl_hash_node_t *found;
135 HASH_FIND (hh, hashlin->buckets, obj->key, obj->keylen, found);
138 HASH_DELETE (hh, hashlin->buckets, found);
139 UCL_FREE (sizeof (ucl_hash_node_t), found);