Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add Doxygen documentation for mallocators
[simgrid.git] / src / xbt / set.c
1 /* $Id$ */
2
3 /* set - data container consisting in dict+dynar                            */
4
5 /* Copyright (c) 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 "xbt/misc.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/ex.h"
14 #include "xbt/dynar.h"
15 #include "xbt/dict.h"
16
17 #include "xbt/set.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_set,xbt,
20              "set: data container consisting in dict+dynar");
21
22 /*####[ Type definition ]####################################################*/
23 typedef struct xbt_set_ {
24   xbt_dict_t  dict;  /* data stored by name */
25   xbt_dynar_t dynar; /* data stored by ID   */
26 } s_xbt_set_t;
27
28 /*####[ Memory  ]############################################################*/
29 /** @brief Constructor */
30 xbt_set_t xbt_set_new (void) {
31   xbt_set_t res=xbt_new(s_xbt_set_t,1);
32
33   res->dict=xbt_dict_new ();
34   res->dynar=xbt_dynar_new(sizeof(void*),NULL);
35
36   return res;
37 }
38
39 /** @brief Destructor */
40 void  xbt_set_free(xbt_set_t *set) {
41   if (*set) {
42     xbt_dict_free ( &( (*set)->dict  ) );
43     xbt_dynar_free( &( (*set)->dynar ) );
44     free(*set);
45     *set = NULL;
46   }
47 }
48
49 /** @brief Add an element to a set. 
50  *
51  * \param set set to populate
52  * \param elm element to add. 
53  * \param free_func How to add the data 
54  *
55  * elm->name must be set;
56  * if elm->name_len <= 0, it is recomputed. If >0, it's used as is;
57  * elm->ID is attributed automatically.
58  */
59 void xbt_set_add    (xbt_set_t      set,
60                       xbt_set_elm_t  elm,
61                       void_f_pvoid_t *free_func) {
62
63   int found = 1;
64   xbt_set_elm_t found_in_dict = NULL;
65   xbt_ex_t e;
66
67   VERB1("add %s to the set",elm->name);
68    
69   if (elm->name_len <= 0) {
70     elm->name_len = strlen(elm->name);
71   }
72
73   TRY {
74     found_in_dict = xbt_dict_get_ext (set->dict, 
75                                       elm->name, elm->name_len);
76   } CATCH(e) {
77     if (e.category != not_found_error) 
78       RETHROW;
79     found = 0;
80     elm->ID = xbt_dynar_length( set->dynar );
81     xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
82     xbt_dynar_set(set->dynar, elm->ID, &elm);
83     DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
84     xbt_ex_free(e);
85   }
86   
87   if (found) {
88     if (elm == found_in_dict) {
89       DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
90              elm->name, elm->ID);
91       return;
92     } else {
93       elm->ID=found_in_dict->ID;
94       DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
95       xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
96       xbt_dynar_set(set->dynar, elm->ID, &elm);
97       return;
98     }
99   }
100 }
101
102 /** @brief Retrive data by providing its name.
103  * 
104  * \param set
105  * \param name Name of the searched cell
106  * \returns the data you're looking for
107  */
108 xbt_set_elm_t xbt_set_get_by_name    (xbt_set_t     set,
109                                       const char     *name) {
110   DEBUG1("Lookup key %s",name);
111   return xbt_dict_get_ext(set->dict, name, strlen(name));
112 }
113
114 /** @brief Retrive data by providing its name and the length of the name
115  *
116  * \param set
117  * \param name Name of the searched cell
118  * \param name_len length of the name, when strlen cannot be trusted
119  * \returns the data you're looking for
120  *
121  * This is useful when strlen cannot be trusted because you don't use a char*
122  * as name, you weirdo.
123  */
124 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t      set,
125                                       const char     *name,
126                                       int             name_len) {
127
128   return xbt_dict_get_ext (set->dict, name, name_len);
129 }
130
131 /** @brief Retrive data by providing its ID
132  *
133  * \param set
134  * \param id what you're looking for
135  * \returns the data you're looking for
136  *
137  * @warning, if the ID does not exists, you're getting into trouble
138  */
139 xbt_set_elm_t xbt_set_get_by_id (xbt_set_t set, int id) {
140   xbt_set_elm_t res;
141   
142   /* Don't bother checking the bounds, the dynar does so */
143
144   res = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
145   DEBUG3("Lookup type of id %d (of %lu): %s", 
146          id, xbt_dynar_length(set->dynar), res->name);
147   
148   return res;
149 }
150
151 /** @brief Constructor */
152 unsigned long xbt_set_length (const xbt_set_t set) {
153    return xbt_dynar_length(set->dynar);
154 }
155
156 /***
157  *** Cursors
158  ***/
159 typedef struct xbt_set_cursor_ {
160   xbt_set_t set;
161   int val;
162 } s_xbt_set_cursor_t;
163
164 /** @brief Create the cursor if it does not exists, rewind it in any case. */
165 void         xbt_set_cursor_first       (xbt_set_t         set,
166                                           xbt_set_cursor_t *cursor) {
167
168   if (set != NULL) {
169     if (!*cursor) {
170       DEBUG0("Create the cursor on first use");
171       *cursor = xbt_new(s_xbt_set_cursor_t,1);
172       xbt_assert0(*cursor,
173                    "Malloc error during the creation of the cursor");
174     }
175     (*cursor)->set = set;
176     xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
177   } else {
178     *cursor = NULL;
179   }
180 }
181
182 /** @brief Move to the next element.  */
183 void         xbt_set_cursor_step        (xbt_set_cursor_t cursor) {
184   xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
185 }
186
187 /** @brief Get current data
188  * 
189  * \return true if it's ok, false if there is no more data
190  */
191 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
192                                           xbt_set_elm_t    *elm) {
193   xbt_set_cursor_t cursor;
194
195   if (!curs || !(*curs))
196     return FALSE;
197
198   cursor=*curs;
199
200   if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
201     free(cursor);
202     *curs=NULL;
203     return FALSE;    
204   } 
205   return TRUE;
206 }
207
208 #ifdef SIMGRID_TEST
209 #include "xbt.h"
210 #include "xbt/ex.h"
211
212 XBT_TEST_SUITE("set","Set data container");
213
214 typedef struct  {
215   /* headers */
216   unsigned int ID;
217   char        *name;
218   unsigned int name_len;
219
220   /* payload */
221   char         *data;
222 } s_my_elem_t,*my_elem_t;
223
224
225 static void my_elem_free(void *e) {
226   my_elem_t elm=(my_elem_t)e;
227
228   if (elm) {
229     free(elm->name);
230     free(elm->data);
231     free(elm);
232   }
233 }
234
235 static void debuged_add(xbt_set_t  set,
236                         const char *name,
237                         const char *data) {
238   my_elem_t    elm;
239
240   elm = xbt_new(s_my_elem_t,1);
241   elm->name=xbt_strdup(name);
242   elm->name_len=0;
243
244   elm->data=xbt_strdup(data);
245
246   xbt_test_log2("Add %s (->%s)",name,data);
247   xbt_set_add(set, (xbt_set_elm_t)elm, &my_elem_free);
248 }
249
250 static void fill(xbt_set_t *set) {
251   xbt_test_add0("Fill in the data set");
252
253   *set=xbt_set_new();
254   debuged_add(*set,"12",     "12");
255   debuged_add(*set,"12a",    "12a");
256   debuged_add(*set,"12b",    "12b");
257   debuged_add(*set,"123",    "123");
258   debuged_add(*set,"123456", "123456");
259   xbt_test_log0("Child becomes child of what to add");
260   debuged_add(*set,"1234",   "1234");
261   xbt_test_log0("Need of common ancestor");
262   debuged_add(*set,"123457", "123457");
263 }
264
265 static void search_name(xbt_set_t head,const char*key) {
266   my_elem_t elm;
267
268   xbt_test_add1("Search by name %s",key);
269   elm = (my_elem_t)xbt_set_get_by_name(head,key);
270   xbt_test_log2(" Found %s (under ID %d)\n",
271                 elm? elm->data:"(null)",
272                 elm? elm->ID:-1);
273   if (strcmp(key,elm->name))
274     THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
275            key,elm->name);
276   if (strcmp(elm->name,elm->data))
277     THROW2(mismatch_error,0,"The name (%s) != data (%s)",
278            key,elm->name);
279   fflush(stdout);
280 }
281
282 static void search_id(xbt_set_t head,int id,const char*key) {
283   my_elem_t elm;
284
285   xbt_test_add1("Search by id %d",id);
286   elm = (my_elem_t) xbt_set_get_by_id(head,id);
287   xbt_test_log2("Found %s (data %s)",
288                 elm? elm->name:"(null)",
289                 elm? elm->data:"(null)");
290   if (id != elm->ID)
291     THROW2(mismatch_error,0,"The found ID (%d) is not the one expected (%d)",
292            elm->ID,id);
293   if (strcmp(key,elm->name))
294     THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
295            elm->name,key);
296   if (strcmp(elm->name,elm->data))
297     THROW2(mismatch_error,0,"The name (%s) != data (%s)",
298            elm->name,elm->data);
299 }
300
301
302 static void traverse(xbt_set_t set) {
303   xbt_set_cursor_t cursor=NULL;
304   my_elem_t         elm=NULL;
305  
306   xbt_set_foreach(set,cursor,elm) {
307     xbt_test_assert0(elm,"Dude ! Got a null elm during traversal!");
308     xbt_test_log3("Id(%d):  %s->%s\n",elm->ID,elm->name,elm->data);
309     xbt_test_assert2(!strcmp(elm->name,elm->data),
310                      "Key(%s) != value(%s). Abording",
311                      elm->name,elm->data);
312   }
313 }
314
315 static void search_not_found(xbt_set_t set, const char *data) {
316   xbt_ex_t e;
317
318   xbt_test_add1("Search %s (expected not to be found)",data);
319   TRY {
320     xbt_set_get_by_name(set,data);
321     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
322   } CATCH(e) {
323     if (e.category != not_found_error) 
324       xbt_test_exception(e);
325     xbt_ex_free(e);  
326   }
327 }
328
329 xbt_set_t set = NULL;
330
331
332 XBT_TEST_UNIT("basic",test_set_basic,"Basic usage") {
333   set=NULL;
334
335   xbt_test_add0("Traverse the empty set");
336   traverse(set);
337   
338   xbt_test_add0("Free a data set");
339   fill(&set);
340   xbt_set_free(&set);
341
342   xbt_test_add0("Free the NULL data set");
343   xbt_set_free(&set);
344
345 }
346
347 XBT_TEST_UNIT("change",test_set_change,"Changing some values") {
348   fill(&set);
349
350   xbt_test_add0("Change 123 to 'Changed 123'");
351   debuged_add(set,"123","Changed 123");
352
353   xbt_test_add0("Change 123 back to '123'");
354   debuged_add(set,"123","123");
355
356   xbt_test_add0("Change 12a to 'Dummy 12a'");
357   debuged_add(set,"12a","Dummy 12a");
358
359   xbt_test_add0("Change 12a to '12a'");
360   debuged_add(set,"12a","12a");
361
362   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
363   xbt_test_add0("Traverse the resulting data set");
364   traverse(set);
365 }
366
367 XBT_TEST_UNIT("retrieve",test_set_retrieve,"Retrieving some values") {
368   my_elem_t elm;
369
370   xbt_test_add0("Search 123");
371   elm = (my_elem_t) xbt_set_get_by_name(set,"123");
372   xbt_test_assert0(elm,"elm must be there");
373   xbt_assert(!strcmp("123",elm->data));
374
375   search_not_found(set,"Can't be found");
376   search_not_found(set,"123 Can't be found");
377   search_not_found(set,"12345678 NOT");
378
379   search_name(set,"12");
380   search_name(set,"12a");
381   search_name(set,"12b");
382   search_name(set,"123");
383   search_name(set,"123456");
384   search_name(set,"1234");
385   search_name(set,"123457");
386
387   search_id(set,0,"12");
388   search_id(set,1,"12a");
389   search_id(set,2,"12b");
390   search_id(set,3,"123");
391   search_id(set,4,"123456");
392   search_id(set,5,"1234");
393   search_id(set,6,"123457");
394
395   xbt_test_add0("Traverse the resulting data set");
396   traverse(set);
397
398   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
399
400   xbt_test_add0("Free the data set (twice)");
401   xbt_set_free(&set);
402   xbt_set_free(&set);
403
404   xbt_test_add0("Traverse the resulting data set");
405   traverse(set);
406 }
407 #endif /* SIMGRID_TEST */