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. */
11 #include "dict_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
14 "Dictionaries provide the same functionnalities than hash tables");
15 /*####[ Private prototypes ]#################################################*/
17 /*####[ Code ]###############################################################*/
21 * @return pointer to the destination
23 * Creates and initialize a new dictionnary
27 xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
33 * @param dict the dictionnary to be freed
35 * Frees a cache structure with all its childs.
38 xbt_dict_free(xbt_dict_t *dict) {
41 xbt_dictelm_free( &( (*dict)->head ) );
50 * \brief Add data to the dict (arbitrary key)
51 * \param dict the container
52 * \param key the key to set the new data
53 * \param key_len the size of the \a key
54 * \param data the data to add in the dict
55 * \param free_ctn function to call with (\a key as argument) when
56 * \a key is removed from the dictionnary
58 * set the \a data in the structure under the \a key, which can be any kind
59 * of data, as long as its length is provided in \a key_len.
62 xbt_dict_set_ext(xbt_dict_t dict,
66 void_f_pvoid_t *free_ctn) {
70 xbt_dictelm_set_ext(&(dict->head),
71 key, key_len, data, free_ctn);
75 * \brief Add data to the dict (null-terminated key)
77 * \param dict the head of the dict
78 * \param key the key to set the new data
79 * \param data the data to add in the dict
80 * \param free_ctn function to call with (\a key as argument) when
81 * \a key is removed from the dictionnary
83 * set the \a data in the structure under the \a key, which is a
84 * null terminated string.
87 xbt_dict_set(xbt_dict_t dict,
90 void_f_pvoid_t *free_ctn) {
94 xbt_dictelm_set(&(dict->head), key, data, free_ctn);
98 * \brief Retrieve data from the dict (arbitrary key)
100 * \param dict the dealer of data
101 * \param key the key to find data
102 * \param key_len the size of the \a key
103 * \returns the data that we are looking for
105 * Search the given \a key. throws not_found_error when not found.
108 xbt_dict_get_ext(xbt_dict_t dict,
114 return xbt_dictelm_get_ext(dict->head, key, key_len);
118 * \brief Retrieve data from the dict (null-terminated key)
120 * \param dict the dealer of data
121 * \param key the key to find data
122 * \returns the data that we are looking for
124 * Search the given \a key. THROWs mismatch_error when not found.
125 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
129 xbt_dict_get(xbt_dict_t dict,
133 return xbt_dictelm_get(dict->head, key);
137 * \brief like xbt_dict_get(), but returning NULL when not found
140 xbt_dict_get_or_null(xbt_dict_t dict,
145 res = xbt_dictelm_get(dict->head, key);
147 if (e.category != not_found_error)
157 * \brief Remove data from the dict (arbitrary key)
159 * \param dict the trash can
160 * \param key the key of the data to be removed
161 * \param key_len the size of the \a key
164 * Remove the entry associated with the given \a key (throws not_found)
167 xbt_dict_remove_ext(xbt_dict_t dict,
172 xbt_dictelm_remove_ext(dict->head, key, key_len);
176 * \brief Remove data from the dict (null-terminated key)
178 * \param dict the head of the dict
179 * \param key the key of the data to be removed
181 * Remove the entry associated with the given \a key
184 xbt_dict_remove(xbt_dict_t dict,
187 THROW1(arg_error,0,"Asked to remove key %s from NULL dict",key);
189 xbt_dictelm_remove(dict->head, key);
194 * \brief Outputs the content of the structure (debuging purpose)
196 * \param dict the exibitionist
197 * \param output a function to dump each data in the tree
199 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
200 * function to output the data. If NULL, data won't be displayed, just the tree structure.
204 xbt_dict_dump(xbt_dict_t dict,
205 void_f_pvoid_t *output) {
207 printf("Dict %p:\n", (void*)dict);
208 xbt_dictelm_dump(dict ? dict->head: NULL, output);