Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new tracing mask TRACE_VOLUME to trace the msg tasks communication size and group...
[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   xbt_dynar_t available_ids;    /* free places in the dynar */
27 } s_xbt_set_t;
28
29 /*####[ Memory  ]############################################################*/
30
31 static int _xbt_set_get_id(xbt_set_t set);
32
33 /** @brief Constructor */
34 xbt_set_t xbt_set_new(void)
35 {
36   xbt_set_t res = xbt_new(s_xbt_set_t, 1);
37
38   res->dict = xbt_dict_new();
39   res->dynar = xbt_dynar_new(sizeof(void *), NULL);
40   res->available_ids = xbt_dynar_new(sizeof(int), NULL);
41
42   return res;
43 }
44
45 /** @brief Destructor */
46 void xbt_set_free(xbt_set_t * set)
47 {
48   if (*set) {
49     xbt_dict_free(&((*set)->dict));
50     xbt_dynar_free(&((*set)->dynar));
51     xbt_dynar_free(&((*set)->available_ids));
52     free(*set);
53     *set = NULL;
54   }
55 }
56
57 /* Compute an ID in order to add an element into the set. */
58 static int _xbt_set_get_id(xbt_set_t set)
59 {
60   int id;
61   if (xbt_dynar_length(set->available_ids) > 0) {
62     /* if there are some available ids */
63     xbt_dynar_pop(set->available_ids, &id);
64   } else {
65     /* otherwise we will add the element at the dynar end */
66     id = xbt_dynar_length(set->dynar);
67   }
68   return id;
69 }
70
71 /** @brief Add an element to a set.
72  *
73  * \param set set to populate
74  * \param elm element to add.
75  * \param free_func How to add the data
76  *
77  * elm->name must be set;
78  * if elm->name_len <= 0, it is recomputed. If >0, it's used as is;
79  * elm->ID is attributed automatically.
80  */
81 void xbt_set_add(xbt_set_t set, xbt_set_elm_t elm, void_f_pvoid_t free_func)
82 {
83
84   int found = 1;
85   xbt_set_elm_t found_in_dict = NULL;
86   xbt_ex_t e;
87
88   VERB1("add %s to the set", elm->name);
89
90   if (elm->name_len <= 0) {
91     elm->name_len = strlen(elm->name);
92   }
93
94   TRY {
95     found_in_dict = xbt_dict_get_ext(set->dict, elm->name, elm->name_len);
96   }
97   CATCH(e) {
98     if (e.category != not_found_error)
99       RETHROW;
100     found = 0;
101     elm->ID = _xbt_set_get_id(set);
102     xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
103     xbt_dynar_set(set->dynar, elm->ID, &elm);
104     DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
105     xbt_ex_free(e);
106   }
107
108   if (found) {
109     if (elm == found_in_dict) {
110       DEBUG2
111         ("Ignoring request to insert the same element twice (key %s ; id %d)",
112          elm->name, elm->ID);
113       return;
114     } else {
115       elm->ID = found_in_dict->ID;
116       DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
117       xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
118       xbt_dynar_set(set->dynar, elm->ID, &elm);
119       return;
120     }
121   }
122 }
123
124 /** @brief Remove an element from a set.
125  *
126  * \param set a set
127  * \param elm element to remove
128  */
129 void xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm)
130 {
131   int id = elm->ID;
132   xbt_dynar_push_as(set->available_ids, int, id);       /* this id becomes available now */
133   xbt_dict_remove_ext(set->dict, elm->name, elm->name_len);
134   elm = NULL;
135   xbt_dynar_set(set->dynar, id, &elm);
136 }
137
138 /** @brief Remove an element from a set providing its name.
139  *
140  * \param set a set
141  * \param key name of the element to remove
142  */
143 void xbt_set_remove_by_name(xbt_set_t set, const char *key)
144 {
145   xbt_set_elm_t elm = xbt_set_get_by_name(set, key);
146   xbt_set_remove(set, elm);
147 }
148
149 /** @brief Remove an element from a set providing its name
150  * and the length of the name.
151  *
152  * \param set a set
153  * \param key name of the element to remove
154  * \param key_len length of \a name
155  */
156 void xbt_set_remove_by_name_ext(xbt_set_t set, const char *key, int key_len)
157 {
158   xbt_set_elm_t elm = xbt_set_get_by_name_ext(set, key, key_len);
159   xbt_set_remove(set, elm);
160 }
161
162 /** @brief Remove an element from a set providing its id.
163  *
164  * \param set a set
165  * \param id id of the element to remove
166  */
167 void xbt_set_remove_by_id(xbt_set_t set, int id)
168 {
169   xbt_set_elm_t elm = xbt_set_get_by_id(set, id);
170   xbt_set_remove(set, elm);
171 }
172
173 /** @brief Retrieve data by providing its name.
174  *
175  * \param set
176  * \param name Name of the searched cell
177  * \returns the data you're looking for
178  */
179 xbt_set_elm_t xbt_set_get_by_name(xbt_set_t set, const char *name)
180 {
181   DEBUG1("Lookup key %s", name);
182   return xbt_dict_get(set->dict, name);
183 }
184
185 /** @brief Retrieve data by providing its name.
186  *
187  * \param set
188  * \param name Name of the searched cell
189  * \returns the data you're looking for, returns NULL if not found
190  */
191 xbt_set_elm_t xbt_set_get_by_name_or_null(xbt_set_t set, const char *name)
192 {
193   DEBUG1("Lookup key %s", name);
194   return xbt_dict_get_or_null(set->dict, name);
195 }
196
197 /** @brief Retrieve data by providing its name and the length of the name
198  *
199  * \param set
200  * \param name Name of the searched cell
201  * \param name_len length of the name, when strlen cannot be trusted
202  * \returns the data you're looking for
203  *
204  * This is useful when strlen cannot be trusted because you don't use a char*
205  * as name, you weirdo.
206  */
207 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
208                                       const char *name, int name_len)
209 {
210
211   return xbt_dict_get_ext(set->dict, name, name_len);
212 }
213
214 /** @brief Retrieve data by providing its ID
215  *
216  * \param set
217  * \param id what you're looking for
218  * \returns the data you're looking for
219  *
220  * @warning, if the ID does not exists, you're getting into trouble
221  */
222 xbt_set_elm_t xbt_set_get_by_id(xbt_set_t set, int id)
223 {
224   xbt_set_elm_t res;
225
226   /* Don't bother checking the bounds, the dynar does so */
227
228   res = xbt_dynar_get_as(set->dynar, id, xbt_set_elm_t);
229   if (res == NULL) {
230     THROW1(not_found_error, 0, "Invalid id: %d", id);
231   }
232   DEBUG3("Lookup type of id %d (of %lu): %s",
233          id, xbt_dynar_length(set->dynar), res->name);
234
235   return res;
236 }
237
238 /**
239  * \brief Returns the number of elements in the set
240  * \param set a set
241  * \return the number of elements in the set
242  */
243 unsigned long xbt_set_length(const xbt_set_t set)
244 {
245   return xbt_dynar_length(set->dynar);
246 }
247
248 /***
249  *** Cursors
250  ***/
251 typedef struct xbt_set_cursor_ {
252   xbt_set_t set;
253   unsigned int val;
254 } s_xbt_set_cursor_t;
255
256 /** @brief Create the cursor if it does not exists, rewind it in any case. */
257 void xbt_set_cursor_first(xbt_set_t set, xbt_set_cursor_t * cursor)
258 {
259   xbt_dynar_t dynar;
260
261   if (set != NULL) {
262     if (!*cursor) {
263       DEBUG0("Create the cursor on first use");
264       *cursor = xbt_new(s_xbt_set_cursor_t, 1);
265       xbt_assert0(*cursor, "Malloc error during the creation of the cursor");
266     }
267     (*cursor)->set = set;
268
269     /* place the cursor on the first element */
270     dynar = set->dynar;
271     (*cursor)->val = 0;
272     while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
273       (*cursor)->val++;
274     }
275
276   } else {
277     *cursor = NULL;
278   }
279 }
280
281 /** @brief Move to the next element.  */
282 void xbt_set_cursor_step(xbt_set_cursor_t cursor)
283 {
284   xbt_dynar_t dynar = cursor->set->dynar;
285   do {
286     cursor->val++;
287   }
288   while (cursor->val < xbt_dynar_length(dynar) &&
289          xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
290 }
291
292 /** @brief Get current data
293  *
294  * \return true if it's ok, false if there is no more data
295  */
296 int xbt_set_cursor_get_or_free(xbt_set_cursor_t * curs, xbt_set_elm_t * elm)
297 {
298   xbt_set_cursor_t cursor;
299
300   if (!curs || !(*curs))
301     return FALSE;
302
303   cursor = *curs;
304
305   if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
306     free(cursor);
307     *curs = NULL;
308     return FALSE;
309   }
310
311   xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
312   return TRUE;
313 }
314
315 #ifdef SIMGRID_TEST
316 #include "xbt.h"
317 #include "xbt/ex.h"
318
319 XBT_TEST_SUITE("set", "Set data container");
320
321 typedef struct {
322   /* headers */
323   unsigned int ID;
324   char *name;
325   unsigned int name_len;
326
327   /* payload */
328   char *data;
329 } s_my_elem_t, *my_elem_t;
330
331
332 static void my_elem_free(void *e)
333 {
334   my_elem_t elm = (my_elem_t) e;
335
336   if (elm) {
337     free(elm->name);
338     free(elm->data);
339     free(elm);
340   }
341 }
342
343 static void debuged_add(xbt_set_t set, const char *name, const char *data)
344 {
345   my_elem_t elm;
346
347   elm = xbt_new(s_my_elem_t, 1);
348   elm->name = xbt_strdup(name);
349   elm->name_len = 0;
350
351   elm->data = xbt_strdup(data);
352
353   xbt_test_log2("Add %s (->%s)", name, data);
354   xbt_set_add(set, (xbt_set_elm_t) elm, &my_elem_free);
355 }
356
357 static void fill(xbt_set_t * set)
358 {
359   xbt_test_add0("Fill in the data set");
360
361   *set = xbt_set_new();
362   debuged_add(*set, "12", "12");
363   debuged_add(*set, "12a", "12a");
364   debuged_add(*set, "12b", "12b");
365   debuged_add(*set, "123", "123");
366   debuged_add(*set, "123456", "123456");
367   xbt_test_log0("Child becomes child of what to add");
368   debuged_add(*set, "1234", "1234");
369   xbt_test_log0("Need of common ancestor");
370   debuged_add(*set, "123457", "123457");
371 }
372
373 static void search_name(xbt_set_t head, const char *key)
374 {
375   my_elem_t elm;
376
377   xbt_test_add1("Search by name %s", key);
378   elm = (my_elem_t) xbt_set_get_by_name(head, key);
379   xbt_test_log2(" Found %s (under ID %d)\n",
380                 elm ? elm->data : "(null)", elm ? elm->ID : -1);
381   if (strcmp(key, elm->name))
382     THROW2(mismatch_error, 0, "The key (%s) is not the one expected (%s)",
383            key, elm->name);
384   if (strcmp(elm->name, elm->data))
385     THROW2(mismatch_error, 0, "The name (%s) != data (%s)", key, elm->name);
386   fflush(stdout);
387 }
388
389 static void search_id(xbt_set_t head, int id, const char *key)
390 {
391   my_elem_t elm;
392
393   xbt_test_add1("Search by id %d", id);
394   elm = (my_elem_t) xbt_set_get_by_id(head, id);
395   xbt_test_log2("Found %s (data %s)",
396                 elm ? elm->name : "(null)", elm ? elm->data : "(null)");
397   if (id != elm->ID)
398     THROW2(mismatch_error, 0,
399            "The found ID (%d) is not the one expected (%d)", elm->ID, id);
400   if (strcmp(key, elm->name))
401     THROW2(mismatch_error, 0, "The key (%s) is not the one expected (%s)",
402            elm->name, key);
403   if (strcmp(elm->name, elm->data))
404     THROW2(mismatch_error, 0, "The name (%s) != data (%s)",
405            elm->name, elm->data);
406 }
407
408
409 static void traverse(xbt_set_t set)
410 {
411   xbt_set_cursor_t cursor = NULL;
412   my_elem_t elm = NULL;
413
414   xbt_set_foreach(set, cursor, elm) {
415     xbt_test_assert0(elm, "Dude ! Got a null elm during traversal!");
416     xbt_test_log3("Id(%d):  %s->%s\n", elm->ID, elm->name, elm->data);
417     xbt_test_assert2(!strcmp(elm->name, elm->data),
418                      "Key(%s) != value(%s). Abording", elm->name, elm->data);
419   }
420 }
421
422 static void search_not_found(xbt_set_t set, const char *data)
423 {
424   xbt_ex_t e;
425
426   xbt_test_add1("Search %s (expected not to be found)", data);
427   TRY {
428     xbt_set_get_by_name(set, data);
429     THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
430            data);
431   } CATCH(e) {
432     if (e.category != not_found_error)
433       xbt_test_exception(e);
434     xbt_ex_free(e);
435   }
436 }
437
438 xbt_set_t set = NULL;
439
440
441 XBT_TEST_UNIT("basic", test_set_basic, "Basic usage")
442 {
443   set = NULL;
444
445   xbt_test_add0("Traverse the empty set");
446   traverse(set);
447
448   xbt_test_add0("Free a data set");
449   fill(&set);
450   xbt_set_free(&set);
451
452   xbt_test_add0("Free the NULL data set");
453   xbt_set_free(&set);
454
455 }
456
457 XBT_TEST_UNIT("change", test_set_change, "Changing some values")
458 {
459   fill(&set);
460
461   xbt_test_add0("Change 123 to 'Changed 123'");
462   debuged_add(set, "123", "Changed 123");
463
464   xbt_test_add0("Change 123 back to '123'");
465   debuged_add(set, "123", "123");
466
467   xbt_test_add0("Change 12a to 'Dummy 12a'");
468   debuged_add(set, "12a", "Dummy 12a");
469
470   xbt_test_add0("Change 12a to '12a'");
471   debuged_add(set, "12a", "12a");
472
473   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
474   xbt_test_add0("Traverse the resulting data set");
475   traverse(set);
476 }
477
478 XBT_TEST_UNIT("retrieve", test_set_retrieve, "Retrieving some values")
479 {
480   my_elem_t elm;
481
482   xbt_test_add0("Search 123");
483   elm = (my_elem_t) xbt_set_get_by_name(set, "123");
484   xbt_test_assert0(elm, "elm must be there");
485   xbt_assert(!strcmp("123", elm->data));
486
487   search_not_found(set, "Can't be found");
488   search_not_found(set, "123 Can't be found");
489   search_not_found(set, "12345678 NOT");
490
491   search_name(set, "12");
492   search_name(set, "12a");
493   search_name(set, "12b");
494   search_name(set, "123");
495   search_name(set, "123456");
496   search_name(set, "1234");
497   search_name(set, "123457");
498
499   search_id(set, 0, "12");
500   search_id(set, 1, "12a");
501   search_id(set, 2, "12b");
502   search_id(set, 3, "123");
503   search_id(set, 4, "123456");
504   search_id(set, 5, "1234");
505   search_id(set, 6, "123457");
506
507   xbt_test_add0("Traverse the resulting data set");
508   traverse(set);
509
510   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
511
512   xbt_test_add0("Free the data set (twice)");
513   xbt_set_free(&set);
514   xbt_set_free(&set);
515
516   xbt_test_add0("Traverse the resulting data set");
517   traverse(set);
518 }
519
520 XBT_TEST_UNIT("remove", test_set_remove, "Removing some values")
521 {
522   my_elem_t elm;
523
524   xbt_set_free(&set);
525   fill(&set);
526
527   xbt_set_remove_by_name(set, "12a");
528   search_not_found(set, "12a");
529
530   search_name(set, "12");
531   search_name(set, "12b");
532   search_name(set, "123");
533   search_name(set, "123456");
534   search_name(set, "1234");
535   search_name(set, "123457");
536
537   search_id(set, 0, "12");
538   search_id(set, 2, "12b");
539   search_id(set, 3, "123");
540   search_id(set, 4, "123456");
541   search_id(set, 5, "1234");
542   search_id(set, 6, "123457");
543
544   debuged_add(set, "12anew", "12anew");
545   elm = (my_elem_t) xbt_set_get_by_id(set, 1);
546   xbt_test_assert1(elm->ID == 1, "elm->ID is %d but should be 1", elm->ID);
547 }
548
549 #endif /* SIMGRID_TEST */