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() */
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
19 "Dictionaries provide the same functionnalities than hash tables");
20 /*####[ Private prototypes ]#################################################*/
22 /*####[ 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 ) );
55 * \brief Add data to the dict (arbitrary key)
56 * \param dict the container
57 * \param key the key to set the new data
58 * \param key_len the size of the \a key
59 * \param data the data to add in the dict
60 * \param free_ctn function to call with (\a key as argument) when
61 * \a key is removed from the dictionnary
63 * set the \a data in the structure under the \a key, which can be any kind
64 * of data, as long as its length is provided in \a 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);
80 * \brief Add data to the dict (null-terminated key)
82 * \param dict the head of the dict
83 * \param key the key to set the new data
84 * \param data the data to add in the dict
85 * \param free_ctn function to call with (\a key as argument) when
86 * \a key is removed from the dictionnary
88 * set the \a data in the structure under the \a key, which is a
89 * null terminated string.
92 xbt_dict_set(xbt_dict_t dict,
95 void_f_pvoid_t *free_ctn) {
99 xbt_dictelm_set(&(dict->head), key, data, free_ctn);
103 * \brief Retrieve data from the dict (arbitrary key)
105 * \param dict the dealer of data
106 * \param key the key to find data
107 * \param key_len the size of the \a key
108 * \param data the data that we are looking for
111 * Search the given \a key. mismatch_error when not found.
114 xbt_dict_get_ext(xbt_dict_t dict,
117 /* OUT */void **data) {
121 return xbt_dictelm_get_ext(dict->head, key, key_len, data);
125 * \brief Retrieve data from the dict (null-terminated key)
127 * \param dict the dealer of data
128 * \param key the key to find data
129 * \param data the data that we are looking for
132 * Search the given \a key. mismatch_error when not found.
135 xbt_dict_get(xbt_dict_t dict,
137 /* OUT */void **data) {
140 return xbt_dictelm_get(dict->head, key, data);
145 * \brief Remove data from the dict (arbitrary key)
147 * \param dict the trash can
148 * \param key the key of the data to be removed
149 * \param key_len the size of the \a key
150 * \return xbt_error_t
152 * Remove the entry associated with the given \a key
155 xbt_dict_remove_ext(xbt_dict_t dict,
160 return xbt_dictelm_remove_ext(dict->head, key, key_len);
164 * \brief Remove data from the dict (null-terminated key)
166 * \param dict the head of the dict
167 * \param key the key of the data to be removed
169 * Remove the entry associated with the given \a key
172 xbt_dict_remove(xbt_dict_t dict,
175 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
177 return xbt_dictelm_remove(dict->head, key);
182 * \brief Outputs the content of the structure (debuging purpose)
184 * \param dict the exibitionist
185 * \param output a function to dump each data in the tree
187 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
188 * function to output the data. If NULL, data won't be displayed, just the tree structure.
192 xbt_dict_dump(xbt_dict_t dict,
193 void_f_pvoid_t *output) {
195 printf("Dict %p:\n", (void*)dict);
196 xbt_dictelm_dump(dict ? dict->head: NULL, output);