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 /** \defgroup XBT_dict A generic dictionnary
18 * \brief This section describes the API to a dictionnary structure
19 * that associates as string to a void* key. It is not a hash table
20 * and the internal data-structure rather looks like a tree.
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
24 "Dictionaries provide the same functionnalities than hash tables");
25 /*####[ Private prototypes ]#################################################*/
27 /*####[ Code ]###############################################################*/
32 * \return pointer to the destination
34 * Creates and initialize a new dictionnary
38 xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
44 * \param dict the dictionnary to be freed
46 * Frees a cache structure with all its childs.
49 xbt_dict_free(xbt_dict_t *dict) {
52 xbt_dictelm_free( &( (*dict)->head ) );
63 * \param dict the container
64 * \param key the key to set the new data
65 * \param key_len the size of the #key
66 * \param data the data to add in the dict
67 * \param free_ctn function to call with (#key as argument) when
68 * #key is removed from the dictionnary
70 * set the #data in the structure under the #key, which can be any kind
71 * of data, as long as its length is provided in #key_len.
74 xbt_dict_set_ext(xbt_dict_t dict,
78 void_f_pvoid_t *free_ctn) {
82 xbt_dictelm_set_ext(&(dict->head),
83 key, key_len, data, free_ctn);
89 * \param dict the head of the dict
90 * \param key the key to set the new data
91 * \param data the data to add in the dict
92 * \param free_ctn function to call with (#key as argument) when
93 * #key is removed from the dictionnary
95 * set the #data in the structure under the #key, which is a
96 * null terminated string.
99 xbt_dict_set(xbt_dict_t dict,
102 void_f_pvoid_t *free_ctn) {
106 xbt_dictelm_set(&(dict->head), key, data, free_ctn);
112 * \param dict the dealer of data
113 * \param key the key to find data
114 * \param key_len the size of the #key
115 * \param data the data that we are looking for
118 * Search the given #key. mismatch_error when not found.
121 xbt_dict_get_ext(xbt_dict_t dict,
124 /* OUT */void **data) {
128 return xbt_dictelm_get_ext(dict->head, key, key_len, data);
134 * \param dict the dealer of data
135 * \param key the key to find data
136 * \param data the data that we are looking for
139 * Search the given #key. mismatch_error when not found.
142 xbt_dict_get(xbt_dict_t dict,
144 /* OUT */void **data) {
147 return xbt_dictelm_get(dict->head, key, data);
154 * \param dict the trash can
155 * \param key the key of the data to be removed
156 * \param key_len the size of the #key
157 * \return xbt_error_t
159 * Remove the entry associated with the given #key
162 xbt_dict_remove_ext(xbt_dict_t dict,
167 return xbt_dictelm_remove_ext(dict->head, key, key_len);
173 * \param dict the head of the dict
174 * \param key the key of the data to be removed
175 * \return xbt_error_t
177 * Remove the entry associated with the given #key
180 xbt_dict_remove(xbt_dict_t dict,
183 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
185 return xbt_dictelm_remove(dict->head, key);
192 * \param dict the exibitionist
193 * \param output a function to dump each data in the tree
194 * \return xbt_error_t
196 * Ouputs the content of the structure. (for debuging purpose). #ouput is a
197 * function to output the data. If NULL, data won't be displayed.
201 xbt_dict_dump(xbt_dict_t dict,
202 void_f_pvoid_t *output) {
204 printf("Dict %p:\n", (void*)dict);
205 xbt_dictelm_dump(dict ? dict->head: NULL, output);