Logo AND Algorithmique Numérique Distribuée

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