ucl_hash.c (1709ccf9d38a5753192420ce5fccd93b04ce4d07) ucl_hash.c (b04a7a0baf6523245034b8ccd06cd0176b8a18cf)
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

--- 26 unchanged lines hidden (view full) ---

35 new->buckets = NULL;
36 }
37 return new;
38}
39
40void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func *func)
41{
42 ucl_hash_node_t *elt, *tmp;
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

--- 26 unchanged lines hidden (view full) ---

35 new->buckets = NULL;
36 }
37 return new;
38}
39
40void 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;
43
44 HASH_ITER (hh, hashlin->buckets, elt, tmp) {
45 HASH_DELETE (hh, hashlin->buckets, elt);
46 if (func) {
44
45 HASH_ITER (hh, hashlin->buckets, elt, tmp) {
46 HASH_DELETE (hh, hashlin->buckets, elt);
47 if (func) {
47 func (elt->data);
48 DL_FOREACH_SAFE (elt->data, cur, otmp) {
49 /* Need to deconst here */
50 func (__DECONST (ucl_object_t *, cur));
51 }
48 }
49 UCL_FREE (sizeof (ucl_hash_node_t), elt);
50 }
51 UCL_FREE (sizeof (ucl_hash_t), hashlin);
52}
53
54void
52 }
53 UCL_FREE (sizeof (ucl_hash_node_t), elt);
54 }
55 UCL_FREE (sizeof (ucl_hash_t), hashlin);
56}
57
58void
55ucl_hash_insert (ucl_hash_t* hashlin, ucl_object_t *obj, const char *key, unsigned keylen)
59ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj,
60 const char *key, unsigned keylen)
56{
57 ucl_hash_node_t *node;
58
59 node = UCL_ALLOC (sizeof (ucl_hash_node_t));
60 node->data = obj;
61 HASH_ADD_KEYPTR (hh, hashlin->buckets, key, keylen, node);
62}
63
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
64void*
69const void*
65ucl_hash_iterate (ucl_hash_t *hashlin, ucl_hash_iter_t *iter)
66{
67 ucl_hash_node_t *elt = *iter;
68
69 if (elt == NULL) {
70 if (hashlin == NULL || hashlin->buckets == NULL) {
71 return NULL;
72 }

--- 14 unchanged lines hidden (view full) ---

87ucl_hash_iter_has_next (ucl_hash_iter_t iter)
88{
89 ucl_hash_node_t *elt = iter;
90
91 return (elt == NULL || elt->hh.prev != NULL);
92}
93
94
70ucl_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 }

--- 14 unchanged lines hidden (view full) ---

92ucl_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
95ucl_object_t*
100const ucl_object_t*
96ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen)
97{
98 ucl_hash_node_t *found;
99
100 if (hashlin == NULL) {
101 return NULL;
102 }
103 HASH_FIND (hh, hashlin->buckets, key, keylen, found);
104
105 if (found) {
106 return found->data;
107 }
108 return NULL;
109}
110
111void
101ucl_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
116void
112ucl_hash_delete (ucl_hash_t* hashlin, ucl_object_t *obj)
117ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj)
113{
114 ucl_hash_node_t *found;
115
116 HASH_FIND (hh, hashlin->buckets, obj->key, obj->keylen, found);
117
118 if (found) {
119 HASH_DELETE (hh, hashlin->buckets, found);
120 }
121}
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}