3 /* dict - a generic dictionnary, variation over the B-tree concept */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "dict_private.h"
13 #include <stdlib.h> /* malloc() */
14 #include <string.h> /* strlen() */
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
19 "Dictionaries provide the same functionnalities than hash tables");
21 /*####[ Private prototypes ]#################################################*/
24 /*####[ Code ]###############################################################*/
29 * @whereto: pointer to the destination
31 * Creates and initialize a new dictionnary
35 xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
41 * @dict: the dictionnary to be freed
43 * Frees a cache structure with all its childs.
46 xbt_dict_free(xbt_dict_t *dict) {
49 xbt_dictelm_free( &( (*dict)->head ) );
60 * @dict: the container
61 * @key: the key to set the new data
62 * @data: the data to add in the dict
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 * @head: the head of the dict
84 * @key: the key to set the new data
85 * @data: the data to add in the dict
87 * set the @data in the structure under the @key, which is a
88 * null terminated string.
91 xbt_dict_set(xbt_dict_t dict,
94 void_f_pvoid_t *free_ctn) {
98 xbt_dictelm_set(&(dict->head), key, data, free_ctn);
104 * @dict: the dealer of data
105 * @key: the key to find data
106 * @data: the data that we are looking for
107 * @Returns: xbt_error
109 * Search the given @key. mismatch_error when not found.
112 xbt_dict_get_ext(xbt_dict_t dict,
115 /* OUT */void **data) {
119 return xbt_dictelm_get_ext(dict->head, key, key_len, data);
125 * @dict: the dealer of data
126 * @key: the key to find data
127 * @data: the data that we are looking for
128 * @Returns: xbt_error
130 * Search the given @key. mismatch_error when not found.
133 xbt_dict_get(xbt_dict_t dict,
135 /* OUT */void **data) {
138 return xbt_dictelm_get(dict->head, key, data);
143 * xbt_dict_remove_ext:
145 * @dict: the trash can
146 * @key: the key of the data to be removed
147 * @Returns: xbt_error_t
149 * Remove the entry associated with the given @key
152 xbt_dict_remove_ext(xbt_dict_t dict,
157 return xbt_dictelm_remove_ext(dict->head, key, key_len);
163 * @head: the head of the dict
164 * @key: the key of the data to be removed
165 * @Returns: xbt_error_t
167 * Remove the entry associated with the given @key
170 xbt_dict_remove(xbt_dict_t dict,
173 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
175 return xbt_dictelm_remove(dict->head, key);
182 * @dict: the exibitionist
183 * @output: a function to dump each data in the tree
184 * @Returns: xbt_error_t
186 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
187 * function to output the data. If NULL, data won't be displayed.
191 xbt_dict_dump(xbt_dict_t dict,
192 void_f_pvoid_t *output) {
194 printf("Dict %p:\n", (void*)dict);
195 xbt_dictelm_dump(dict ? dict->head: NULL, output);