Logo AND Algorithmique Numérique Distribuée

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