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");
19 /*####[ Private prototypes ]#################################################*/
21 /*####[ Code ]###############################################################*/
26 * \return pointer to the destination
28 * Creates and initialize a new dictionnary
32 xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
38 * \param dict the dictionnary to be freed
40 * Frees a cache structure with all its childs.
43 xbt_dict_free(xbt_dict_t *dict) {
46 xbt_dictelm_free( &( (*dict)->head ) );
57 * \param dict the container
58 * \param key the key to set the new data
59 * \param key_len the size of the #key
60 * \param data the data to add in the dict
61 * \param free_ctn function to call with (#key as argument) when
62 * #key is removed from the dictionnary
64 * set the #data in the structure under the #key, which can be any kind
65 * of data, as long as its length is provided in #key_len.
68 xbt_dict_set_ext(xbt_dict_t dict,
72 void_f_pvoid_t *free_ctn) {
76 xbt_dictelm_set_ext(&(dict->head),
77 key, key_len, data, free_ctn);
83 * \param dict the head of the dict
84 * \param key the key to set the new data
85 * \param data the data to add in the dict
86 * \param free_ctn function to call with (#key as argument) when
87 * #key is removed from the dictionnary
89 * set the #data in the structure under the #key, which is a
90 * null terminated string.
93 xbt_dict_set(xbt_dict_t dict,
96 void_f_pvoid_t *free_ctn) {
100 xbt_dictelm_set(&(dict->head), key, data, free_ctn);
106 * \param dict the dealer of data
107 * \param key the key to find data
108 * \param key_len the size of the #key
109 * \param data the data that we are looking for
112 * Search the given #key. mismatch_error when not found.
115 xbt_dict_get_ext(xbt_dict_t dict,
118 /* OUT */void **data) {
122 return xbt_dictelm_get_ext(dict->head, key, key_len, data);
128 * \param dict the dealer of data
129 * \param key the key to find data
130 * \param data the data that we are looking for
133 * Search the given #key. mismatch_error when not found.
136 xbt_dict_get(xbt_dict_t dict,
138 /* OUT */void **data) {
141 return xbt_dictelm_get(dict->head, key, data);
148 * \param dict the trash can
149 * \param key the key of the data to be removed
150 * \param key_len the size of the #key
151 * \return xbt_error_t
153 * Remove the entry associated with the given #key
156 xbt_dict_remove_ext(xbt_dict_t dict,
161 return xbt_dictelm_remove_ext(dict->head, key, key_len);
167 * \param dict the head of the dict
168 * \param key the key of the data to be removed
169 * \return xbt_error_t
171 * Remove the entry associated with the given #key
174 xbt_dict_remove(xbt_dict_t dict,
177 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
179 return xbt_dictelm_remove(dict->head, key);
186 * \param dict the exibitionist
187 * \param output a function to dump each data in the tree
188 * \return xbt_error_t
190 * Ouputs the content of the structure. (for debuging purpose). #ouput is a
191 * function to output the data. If NULL, data won't be displayed.
195 xbt_dict_dump(xbt_dict_t dict,
196 void_f_pvoid_t *output) {
198 printf("Dict %p:\n", (void*)dict);
199 xbt_dictelm_dump(dict ? dict->head: NULL, output);