3 /* dict - a generic dictionnary, variation over the B-tree concept */
5 /* Copyright (c) 2003,2004 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 #include <stdlib.h> /* malloc() */
13 #include <string.h> /* strlen() */
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
18 "Dictionaries provide the same functionnalities than hash tables");
20 /*####[ Private prototypes ]#################################################*/
23 /*####[ Code ]###############################################################*/
28 * @whereto: pointer to the destination
30 * Creates and initialize a new dictionnary
34 xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
40 * @dict: the dictionnary to be freed
42 * Frees a cache structure with all its childs.
45 xbt_dict_free(xbt_dict_t *dict) {
48 xbt_dictelm_free( &( (*dict)->head ) );
59 * @dict: the container
60 * @key: the key to set the new data
61 * @data: the data to add in the dict
63 * set the @data in the structure under the @key, which can be any kind
64 * of data, as long as its length is provided in @key_len.
67 xbt_dict_set_ext(xbt_dict_t dict,
71 void_f_pvoid_t *free_ctn) {
75 xbt_dictelm_set_ext(&(dict->head),
76 key, key_len, data, free_ctn);
82 * @head: the head of the dict
83 * @key: the key to set the new data
84 * @data: the data to add in the dict
86 * set the @data in the structure under the @key, which is a
87 * null terminated string.
90 xbt_dict_set(xbt_dict_t dict,
93 void_f_pvoid_t *free_ctn) {
97 xbt_dictelm_set(&(dict->head), key, data, free_ctn);
103 * @dict: the dealer of data
104 * @key: the key to find data
105 * @data: the data that we are looking for
106 * @Returns: xbt_error
108 * Search the given @key. mismatch_error when not found.
111 xbt_dict_get_ext(xbt_dict_t dict,
114 /* OUT */void **data) {
118 return xbt_dictelm_get_ext(dict->head, key, key_len, data);
124 * @dict: the dealer of data
125 * @key: the key to find data
126 * @data: the data that we are looking for
127 * @Returns: xbt_error
129 * Search the given @key. mismatch_error when not found.
132 xbt_dict_get(xbt_dict_t dict,
134 /* OUT */void **data) {
137 return xbt_dictelm_get(dict->head, key, data);
142 * xbt_dict_remove_ext:
144 * @dict: the trash can
145 * @key: the key of the data to be removed
146 * @Returns: xbt_error_t
148 * Remove the entry associated with the given @key
151 xbt_dict_remove_ext(xbt_dict_t dict,
156 return xbt_dictelm_remove_ext(dict->head, key, key_len);
162 * @head: the head of the dict
163 * @key: the key of the data to be removed
164 * @Returns: xbt_error_t
166 * Remove the entry associated with the given @key
169 xbt_dict_remove(xbt_dict_t dict,
172 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
174 return xbt_dictelm_remove(dict->head, key);
181 * @dict: the exibitionist
182 * @output: a function to dump each data in the tree
183 * @Returns: xbt_error_t
185 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
186 * function to output the data. If NULL, data won't be displayed.
190 xbt_dict_dump(xbt_dict_t dict,
191 void_f_pvoid_t *output) {
193 printf("Dict %p:\n", (void*)dict);
194 xbt_dictelm_dump(dict ? dict->head: NULL, output);