Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I probably had been drinking too much the day I have written this command... Sorry...
[simgrid.git] / src / xbt / dict.c
1 /* $Id$ */
2
3 /* dict - a generic dictionnary, variation over the B-tree concept          */
4
5 /* Copyright (c) 2003,2004 Martin Quinson. All rights reserved.             */
6
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. */
9
10 #include "dict_private.h"
11
12 #include <stdlib.h> /* malloc() */
13 #include <string.h> /* strlen() */
14
15 #include <stdio.h>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
18    "Dictionaries provide the same functionnalities than hash tables");
19
20 /*####[ Private prototypes ]#################################################*/
21
22
23 /*####[ Code ]###############################################################*/
24
25 /**
26  * xbt_dict_new:
27  *
28  * @whereto: pointer to the destination
29  *
30  * Creates and initialize a new dictionnary
31  */
32 xbt_dict_t 
33 xbt_dict_new(void) {
34   xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
35   res->head=NULL;
36   return res;
37 }
38 /**
39  * xbt_dict_free:
40  * @dict: the dictionnary to be freed
41  *
42  * Frees a cache structure with all its childs.
43  */
44 void
45 xbt_dict_free(xbt_dict_t *dict)  {
46   if (dict && *dict) {
47     if ((*dict)->head) {
48       xbt_dictelm_free( &( (*dict)->head ) );
49       (*dict)->head = NULL;
50     }
51     xbt_free(*dict);
52     *dict=NULL;
53   }
54 }
55
56 /**
57  * xbt_dict_set_ext:
58  *
59  * @dict: the container
60  * @key: the key to set the new data
61  * @data: the data to add in the dict
62  *
63  * set the @data in the structure under the @key, which can be any kind 
64  * of data, as long as its length is provided in @key_len.
65  */
66 void
67 xbt_dict_set_ext(xbt_dict_t      dict,
68                   const char      *key,
69                   int              key_len,
70                   void            *data,
71                   void_f_pvoid_t  *free_ctn) {
72
73   xbt_assert(dict);
74
75   xbt_dictelm_set_ext(&(dict->head),
76                        key, key_len, data, free_ctn);
77 }
78
79 /**
80  * xbt_dict_set:
81  *
82  * @head: the head of the dict
83  * @key: the key to set the new data
84  * @data: the data to add in the dict
85  *
86  * set the @data in the structure under the @key, which is a 
87  * null terminated string.
88  */
89 void
90 xbt_dict_set(xbt_dict_t     dict,
91               const char     *key,
92               void           *data,
93               void_f_pvoid_t *free_ctn) {
94
95   xbt_assert(dict);
96   
97   xbt_dictelm_set(&(dict->head), key, data, free_ctn);
98 }
99
100 /**
101  * xbt_dict_get_ext:
102  *
103  * @dict: the dealer of data
104  * @key: the key to find data
105  * @data: the data that we are looking for
106  * @Returns: xbt_error
107  *
108  * Search the given @key. mismatch_error when not found.
109  */
110 xbt_error_t
111 xbt_dict_get_ext(xbt_dict_t     dict,
112                   const char     *key,
113                   int             key_len,
114                   /* OUT */void **data) {
115
116   xbt_assert(dict);
117
118   return xbt_dictelm_get_ext(dict->head, key, key_len, data);
119 }
120
121 /**
122  * xbt_dict_get:
123  *
124  * @dict: the dealer of data
125  * @key: the key to find data
126  * @data: the data that we are looking for
127  * @Returns: xbt_error
128  *
129  * Search the given @key. mismatch_error when not found.
130  */
131 xbt_error_t
132 xbt_dict_get(xbt_dict_t     dict,
133               const char     *key,
134               /* OUT */void **data) {
135   xbt_assert(dict);
136
137   return xbt_dictelm_get(dict->head, key, data);
138 }
139
140
141 /**
142  * xbt_dict_remove_ext:
143  *
144  * @dict: the trash can
145  * @key: the key of the data to be removed
146  * @Returns: xbt_error_t
147  *
148  * Remove the entry associated with the given @key
149  */
150 xbt_error_t
151 xbt_dict_remove_ext(xbt_dict_t  dict,
152                      const char  *key,
153                      int          key_len) {
154   xbt_assert(dict);
155   
156   return xbt_dictelm_remove_ext(dict->head, key, key_len);
157 }
158
159 /**
160  * xbt_dict_remove:
161  *
162  * @head: the head of the dict
163  * @key: the key of the data to be removed
164  * @Returns: xbt_error_t
165  *
166  * Remove the entry associated with the given @key
167  */
168 xbt_error_t
169 xbt_dict_remove(xbt_dict_t  dict,
170                  const char  *key) {
171   if (!dict) 
172      RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
173
174   return xbt_dictelm_remove(dict->head, key);
175 }
176
177
178 /**
179  * xbt_dict_dump:
180  *
181  * @dict: the exibitionist
182  * @output: a function to dump each data in the tree
183  * @Returns: xbt_error_t
184  *
185  * Ouputs the content of the structure. (for debuging purpose). @ouput is a
186  * function to output the data. If NULL, data won't be displayed.
187  */
188
189 void
190 xbt_dict_dump(xbt_dict_t     dict,
191                void_f_pvoid_t *output) {
192
193   printf("Dict %p:\n", (void*)dict);
194   xbt_dictelm_dump(dict ? dict->head: NULL, output);
195 }
196