Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3657cb0b315876a8cdb571de4c8ed64ba6577e9f
[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
12 #include "gras_private.h"
13 #include "dict_private.h"
14
15 #include <stdlib.h> /* malloc() */
16 #include <string.h> /* strlen() */
17
18 #include <stdio.h>
19
20 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict,gros,
21    "Dictionaries provide the same functionnalities than hash tables");
22
23 /*####[ Private prototypes ]#################################################*/
24
25
26 /*####[ Code ]###############################################################*/
27
28 /**
29  * gras_dict_new:
30  *
31  * @whereto: pointer to the destination
32  *
33  * Creates and initialize a new dictionnary
34  */
35 gras_error_t
36 gras_dict_new(gras_dict_t **whereto) {
37   gras_dict_t *dict;
38
39   if (!(dict = calloc(1, sizeof(gras_dict_t))))
40     RAISE_MALLOC;
41
42   dict->head=NULL;
43
44   *whereto = dict;
45   
46   return no_error;
47 }
48 /**
49  * gras_dict_free:
50  * @dict: the dictionnary to be freed
51  *
52  * Frees a cache structure with all its childs.
53  */
54 void
55 gras_dict_free(gras_dict_t **dict)  {
56   if (dict && *dict) {
57     if ((*dict)->head) {
58       gras_dictelm_free( &( (*dict)->head ) );
59       (*dict)->head = NULL;
60     }
61     free(*dict);
62     *dict=NULL;
63   }
64 }
65
66 /**
67  * gras_dict_set_ext:
68  *
69  * @p_dict: the container
70  * @key: the key to set the new data
71  * @data: the data to add in the dict
72  * @Returns: a gras_error
73  *
74  * set the @data in the structure under the @key, which can be any kind 
75  * of data, as long as its length is provided in @key_len.
76  */
77 gras_error_t
78 gras_dict_set_ext(gras_dict_t     *p_dict,
79                      const char      *key,
80                      int              key_len,
81                      void            *data,
82                      void_f_pvoid_t  *free_ctn) {
83
84   gras_assert(p_dict);
85
86   return gras_dictelm_set_ext(&(p_dict->head),
87                                  key, key_len, data, free_ctn);
88 }
89
90 /**
91  * gras_dict_set:
92  *
93  * @head: the head of the dict
94  * @key: the key to set the new data
95  * @data: the data to add in the dict
96  * @Returns: a gras_error
97  *
98  * set the @data in the structure under the @key, which is a 
99  * null terminated string.
100  */
101 gras_error_t
102 gras_dict_set(gras_dict_t    *p_dict,
103                  const char     *key,
104                  void           *data,
105                  void_f_pvoid_t *free_ctn) {
106
107   gras_assert(p_dict);
108   
109   return gras_dictelm_set(&(p_dict->head), key, data, free_ctn);
110 }
111
112 /**
113  * gras_dict_get_ext:
114  *
115  * @dict: the dealer of data
116  * @key: the key to find data
117  * @data: the data that we are looking for
118  * @Returns: gras_error
119  *
120  * Search the given @key. mismatch_error when not found.
121  */
122 gras_error_t
123 gras_dict_get_ext(gras_dict_t    *dict,
124                        const char     *key,
125                        int             key_len,
126                        /* OUT */void **data) {
127
128   gras_assert(dict);
129
130   return gras_dictelm_get_ext(dict->head, key, key_len, data);
131 }
132
133 /**
134  * gras_dict_get:
135  *
136  * @dict: the dealer of data
137  * @key: the key to find data
138  * @data: the data that we are looking for
139  * @Returns: gras_error
140  *
141  * Search the given @key. mismatch_error when not found.
142  */
143 gras_error_t
144 gras_dict_get(gras_dict_t    *dict,
145                    const char     *key,
146                    /* OUT */void **data) {
147   gras_assert(dict);
148
149   return gras_dictelm_get(dict->head, key, data);
150 }
151
152
153 /**
154  * gras_dict_remove_ext:
155  *
156  * @dict: the trash can
157  * @key: the key of the data to be removed
158  * @Returns: gras_error_t
159  *
160  * Remove the entry associated with the given @key
161  */
162 gras_error_t
163 gras_dict_remove_ext(gras_dict_t *dict,
164                      const char  *key,
165                      int          key_len) {
166   gras_assert(dict);
167   
168   return gras_dictelm_remove_ext(dict->head, key, key_len);
169 }
170
171 /**
172  * gras_dict_remove:
173  *
174  * @head: the head of the dict
175  * @key: the key of the data to be removed
176  * @Returns: gras_error_t
177  *
178  * Remove the entry associated with the given @key
179  */
180 gras_error_t
181 gras_dict_remove(gras_dict_t *dict,
182                  const char  *key) {
183   if (!dict) 
184      RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
185
186   return gras_dictelm_remove(dict->head, key);
187 }
188
189
190 /**
191  * gras_dict_dump:
192  *
193  * @dict: the exibitionist
194  * @output: a function to dump each data in the tree
195  * @Returns: gras_error_t
196  *
197  * Ouputs the content of the structure. (for debuging purpose). @ouput is a
198  * function to output the data. If NULL, data won't be displayed.
199  */
200
201 gras_error_t
202 gras_dict_dump(gras_dict_t    *dict,
203                void_f_pvoid_t *output) {
204
205   printf("Dict %p:\n", (void*)dict);
206   return gras_dictelm_dump(dict ? dict->head: NULL, output);
207 }
208