3 /* dict_multi - dictionnaries of dictionnaries of ... of data */
5 /* Copyright (c) 2003-2005 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "dict_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_multi,dict, "Dictionaries of multiple keys");
14 static void _free_dict(void*d) {
15 xbt_dict_t dict=*(xbt_dict_t*)d;
19 /** \brief Insert \e data under all the keys contained in \e keys, providing their sizes in \e lens.
21 * \arg mdict: the multi-dict
22 * \arg keys: dynar of (char *) containing all the keys
23 * \arg lens: length of each element of \e keys
24 * \arg data: what to store in the structure
25 * \arg free_ctn: function to use to free the pushed content on need
27 * Dynars are not modified during the operation.
31 xbt_multidict_set_ext(xbt_dict_t mdict,
32 xbt_dynar_t keys, xbt_dynar_t lens,
33 void *data, void_f_pvoid_t *free_ctn) {
36 xbt_dict_t thislevel,nextlevel;
39 unsigned long int thislen;
41 int keys_len=xbt_dynar_length(keys);
43 xbt_assert(xbt_dynar_length(keys) == xbt_dynar_length(lens));
44 xbt_assert0(keys_len, "Can't set a zero-long key set in a multidict");
46 DEBUG2("xbt_multidict_set(%p,%d)", mdict, keys_len);
48 for (i=0 , thislevel = mdict ;
50 i++ , thislevel = nextlevel) {
52 xbt_dynar_get_cpy(keys, i, &thiskey);
53 xbt_dynar_get_cpy(lens, i, &thislen);
55 DEBUG5("multi_set: at level %d, len=%ld, key=%p |%*s|", i, thislen, thiskey, (int)thislen,thiskey);
57 /* search the dict of next level */
58 TRYCATCH(xbt_dict_get_ext(thislevel, thiskey, thislen, (void*)&nextlevel), mismatch_error);
60 /* make sure the dict of next level exists */
61 if (errcode == mismatch_error) {
62 nextlevel=xbt_dict_new();
63 xbt_dict_set_ext(thislevel, thiskey, thislen, &nextlevel, _free_dict);
67 xbt_dynar_get_cpy(keys, i, &thiskey);
68 xbt_dynar_get_cpy(lens, i, &thislen);
70 xbt_dict_set_ext(thislevel, thiskey, thislen, data, free_ctn);
75 /** \brief Insert \e data under all the keys contained in \e keys
77 * \arg head: the head of dict
78 * \arg keys: dynar of null-terminated strings containing all the keys
79 * \arg data: what to store in the structure
80 * \arg free_ctn: function to use to free the pushed content on need
83 xbt_multidict_set(xbt_dict_t mdict,
85 void *data, void_f_pvoid_t *free_ctn) {
87 xbt_dynar_t lens = xbt_dynar_new(sizeof(unsigned long int),NULL);
90 for (i = 0; i < xbt_dynar_length(keys); i++) {
91 char *thiskey = xbt_dynar_get_as(keys, i, char*);
92 unsigned long int thislen = (unsigned long int) strlen(thiskey);
93 DEBUG2("Push %ld as level %d length",thislen, i);
94 xbt_dynar_push(lens,&thislen);
97 errcode = xbt_multidict_set_ext(mdict, keys, lens, data, free_ctn);
98 xbt_dynar_free(&lens);
102 /** \brief Insert \e data under all the keys contained in \e keys, providing their sizes in \e lens.
104 * \arg mdict: the multi-dict
105 * \arg keys: dynar of (char *) containing all the keys
106 * \arg lens: length of each element of \e keys
107 * \arg data: where to put what was found in structure
108 * \arg free_ctn: function to use to free the pushed content on need
110 * Dynars are not modified during the operation.
113 xbt_multidict_get_ext(xbt_dict_t mdict,
114 xbt_dynar_t keys, xbt_dynar_t lens,
115 /*OUT*/void **data) {
117 xbt_dict_t thislevel,nextlevel;
120 unsigned long int thislen;
122 int keys_len=xbt_dynar_length(keys);
124 xbt_assert(xbt_dynar_length(keys) == xbt_dynar_length(lens));
125 xbt_assert0(xbt_dynar_length(keys) >= 1, "Can't get a zero-long key set in a multidict");
127 DEBUG2("xbt_multidict_get(%p, %ld)", mdict, xbt_dynar_length(keys));
129 for (i=0 , thislevel=mdict ;
131 i++ , thislevel = nextlevel) {
133 xbt_dynar_get_cpy(keys, i, &thiskey);
134 xbt_dynar_get_cpy(lens, i, &thislen);
136 DEBUG5("multi_get: at level %d, len=%ld, key=%p |%*s|", i, thislen, thiskey, (int)thislen,thiskey);
138 /* search the dict of next level: let mismatch raise if not found */
139 TRY(xbt_dict_get_ext(thislevel, thiskey, thislen, (void*)&nextlevel));
142 xbt_dynar_get_cpy(keys, i, &thiskey);
143 xbt_dynar_get_cpy(lens, i, &thislen);
145 return xbt_dict_get_ext(thislevel, thiskey, thislen, data);
149 xbt_multidict_get(xbt_dict_t mdict, xbt_dynar_t keys, /*OUT*/void **data) {
151 xbt_dynar_t lens = xbt_dynar_new(sizeof(unsigned long int),NULL);
154 for (i = 0; i < xbt_dynar_length(keys); i++) {
155 char *thiskey = xbt_dynar_get_as(keys, i, char*);
156 unsigned long int thislen = (unsigned long int) strlen(thiskey);
157 xbt_dynar_push(lens,&thislen);
160 errcode = xbt_multidict_get_ext(mdict, keys, lens, data),
161 xbt_dynar_free(&lens);
166 /** \brief Remove the entry under all the keys contained in \e keys, providing their sizes in \e lens.
168 * \arg mdict: the multi-dict
169 * \arg keys: dynar of (char *) containing all the keys
170 * \arg lens: length of each element of \e keys
171 * \arg data: what to store in the structure
172 * \arg free_ctn: function to use to free the pushed content on need
174 * Dynars are not modified during the operation.
176 * Removing a non-existant key is ok.
180 xbt_multidict_remove_ext(xbt_dict_t mdict, xbt_dynar_t keys, xbt_dynar_t lens) {
182 xbt_dict_t thislevel,nextlevel;
185 xbt_assert(xbt_dynar_length(keys) == xbt_dynar_length(lens));
186 xbt_assert0(xbt_dynar_length(keys), "Can't remove a zero-long key set in a multidict");
188 unsigned long int thislen;
190 int keys_len=xbt_dynar_length(keys);
192 xbt_assert(xbt_dynar_length(keys) == xbt_dynar_length(lens));
193 xbt_assert0(keys_len, "Can't set a zero-long key set in a multidict");
195 for (i=0 , thislevel=mdict ;
197 i++ , thislevel = nextlevel) {
199 xbt_dynar_get_cpy(keys, i, &thiskey);
200 xbt_dynar_get_cpy(lens, i, &thislen);
202 /* search the dict of next level */
203 TRYCATCH(xbt_dict_get_ext(thislevel, thiskey, thislen, (void*)&nextlevel), mismatch_error);
205 /* If non-existant entry, nothing to do */
206 if (errcode == mismatch_error)
210 xbt_dynar_get_cpy(keys, i, &thiskey);
211 xbt_dynar_get_cpy(lens, i, &thislen);
213 return xbt_dict_remove_ext(thislevel, thiskey, thislen);
217 xbt_multidict_remove(xbt_dict_t mdict, xbt_dynar_t keys) {
220 xbt_dynar_t lens = xbt_dynar_new(sizeof(unsigned long int),NULL);
223 for (i = 0; i < xbt_dynar_length(keys); i++) {
224 char *thiskey = xbt_dynar_get_as(keys, i, char*);
225 unsigned long int thislen = strlen(thiskey);
226 xbt_dynar_push(lens,&thislen);
229 errcode = xbt_multidict_remove_ext(mdict, keys, lens);
230 xbt_dynar_free(&lens);