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 #include "ucl_internal.h" 25 #include "ucl_hash.h" 26 #include "utlist.h" 27 28 ucl_hash_t* 29 ucl_hash_create (void) 30 { 31 ucl_hash_t *new; 32 33 new = UCL_ALLOC (sizeof (ucl_hash_t)); 34 if (new != NULL) { 35 new->buckets = NULL; 36 } 37 return new; 38 } 39 40 void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func) 41 { 42 ucl_hash_node_t *elt, *tmp; 43 const ucl_object_t *cur, *otmp; 44 45 HASH_ITER (hh, hashlin->buckets, elt, tmp) { 46 HASH_DELETE (hh, hashlin->buckets, elt); 47 if (func) { 48 DL_FOREACH_SAFE (elt->data, cur, otmp) { 49 /* Need to deconst here */ 50 func (__DECONST (ucl_object_t *, cur)); 51 } 52 } 53 UCL_FREE (sizeof (ucl_hash_node_t), elt); 54 } 55 UCL_FREE (sizeof (ucl_hash_t), hashlin); 56 } 57 58 void 59 ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, 60 const char *key, unsigned keylen) 61 { 62 ucl_hash_node_t *node; 63 64 node = UCL_ALLOC (sizeof (ucl_hash_node_t)); 65 node->data = obj; 66 HASH_ADD_KEYPTR (hh, hashlin->buckets, key, keylen, node); 67 } 68 69 const void* 70 ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter) 71 { 72 ucl_hash_node_t *elt = *iter; 73 74 if (elt == NULL) { 75 if (hashlin == NULL || hashlin->buckets == NULL) { 76 return NULL; 77 } 78 elt = hashlin->buckets; 79 if (elt == NULL) { 80 return NULL; 81 } 82 } 83 else if (elt == hashlin->buckets) { 84 return NULL; 85 } 86 87 *iter = elt->hh.next ? elt->hh.next : hashlin->buckets; 88 return elt->data; 89 } 90 91 bool 92 ucl_hash_iter_has_next (ucl_hash_iter_t iter) 93 { 94 ucl_hash_node_t *elt = iter; 95 96 return (elt == NULL || elt->hh.prev != NULL); 97 } 98 99 100 const ucl_object_t* 101 ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen) 102 { 103 ucl_hash_node_t *found; 104 105 if (hashlin == NULL) { 106 return NULL; 107 } 108 HASH_FIND (hh, hashlin->buckets, key, keylen, found); 109 110 if (found) { 111 return found->data; 112 } 113 return NULL; 114 } 115 116 void 117 ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj) 118 { 119 ucl_hash_node_t *found; 120 121 HASH_FIND (hh, hashlin->buckets, obj->key, obj->keylen, found); 122 123 if (found) { 124 HASH_DELETE (hh, hashlin->buckets, found); 125 } 126 } 127