Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: Change the reference speed to a command line option
[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 and the length of the name
186  *
187  * \param set
188  * \param name Name of the searched cell
189  * \param name_len length of the name, when strlen cannot be trusted
190  * \returns the data you're looking for
191  *
192  * This is useful when strlen cannot be trusted because you don't use a char*
193  * as name, you weirdo.
194  */
195 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
196                                       const char *name, int name_len)
197 {
198
199   return xbt_dict_get_ext(set->dict, name, name_len);
200 }
201
202 /** @brief Retrieve data by providing its ID
203  *
204  * \param set
205  * \param id what you're looking for
206  * \returns the data you're looking for
207  *
208  * @warning, if the ID does not exists, you're getting into trouble
209  */
210 xbt_set_elm_t xbt_set_get_by_id(xbt_set_t set, int id)
211 {
212   xbt_set_elm_t res;
213
214   /* Don't bother checking the bounds, the dynar does so */
215
216   res = xbt_dynar_get_as(set->dynar, id, xbt_set_elm_t);
217   if (res == NULL) {
218     THROW1(not_found_error, 0, "Invalid id: %d", id);
219   }
220   DEBUG3("Lookup type of id %d (of %lu): %s",
221          id, xbt_dynar_length(set->dynar), res->name);
222
223   return res;
224 }
225
226 /**
227  * \brief Returns the number of elements in the set
228  * \param set a set
229  * \return the number of elements in the set
230  */
231 unsigned long xbt_set_length(const xbt_set_t set)
232 {
233   return xbt_dynar_length(set->dynar);
234 }
235
236 /***
237  *** Cursors
238  ***/
239 typedef struct xbt_set_cursor_ {
240   xbt_set_t set;
241   unsigned int val;
242 } s_xbt_set_cursor_t;
243
244 /** @brief Create the cursor if it does not exists, rewind it in any case. */
245 void xbt_set_cursor_first(xbt_set_t set, xbt_set_cursor_t * cursor)
246 {
247   xbt_dynar_t dynar;
248
249   if (set != NULL) {
250     if (!*cursor) {
251       DEBUG0("Create the cursor on first use");
252       *cursor = xbt_new(s_xbt_set_cursor_t, 1);
253       xbt_assert0(*cursor, "Malloc error during the creation of the cursor");
254     }
255     (*cursor)->set = set;
256
257     /* place the cursor on the first element */
258     dynar = set->dynar;
259     (*cursor)->val = 0;
260     while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
261       (*cursor)->val++;
262     }
263
264   } else {
265     *cursor = NULL;
266   }
267 }
268
269 /** @brief Move to the next element.  */
270 void xbt_set_cursor_step(xbt_set_cursor_t cursor)
271 {
272   xbt_dynar_t dynar = cursor->set->dynar;
273   do {
274     cursor->val++;
275   }
276   while (cursor->val < xbt_dynar_length(dynar) &&
277          xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
278 }
279
280 /** @brief Get current data
281  *
282  * \return true if it's ok, false if there is no more data
283  */
284 int xbt_set_cursor_get_or_free(xbt_set_cursor_t * curs, xbt_set_elm_t * elm)
285 {
286   xbt_set_cursor_t cursor;
287
288   if (!curs || !(*curs))
289     return FALSE;
290
291   cursor = *curs;
292
293   if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
294     free(cursor);
295     *curs = NULL;
296     return FALSE;
297   }
298
299   xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
300   return TRUE;
301 }
302
303 #ifdef SIMGRID_TEST
304 #include "xbt.h"
305 #include "xbt/ex.h"
306
307 XBT_TEST_SUITE("set", "Set data container");
308
309 typedef struct {
310   /* headers */
311   unsigned int ID;
312   char *name;
313   unsigned int name_len;
314
315   /* payload */
316   char *data;
317 } s_my_elem_t, *my_elem_t;
318
319
320 static void my_elem_free(void *e)
321 {
322   my_elem_t elm = (my_elem_t) e;
323
324   if (elm) {
325     free(elm->name);
326     free(elm->data);
327     free(elm);
328   }
329 }
330
331 static void debuged_add(xbt_set_t set, const char *name, const char *data)
332 {
333   my_elem_t elm;
334
335   elm = xbt_new(s_my_elem_t, 1);
336   elm->name = xbt_strdup(name);
337   elm->name_len = 0;
338
339   elm->data = xbt_strdup(data);
340
341   xbt_test_log2("Add %s (->%s)", name, data);
342   xbt_set_add(set, (xbt_set_elm_t) elm, &my_elem_free);
343 }
344
345 static void fill(xbt_set_t * set)
346 {
347   xbt_test_add0("Fill in the data set");
348
349   *set = xbt_set_new();
350   debuged_add(*set, "12", "12");
351   debuged_add(*set, "12a", "12a");
352   debuged_add(*set, "12b", "12b");
353   debuged_add(*set, "123", "123");
354   debuged_add(*set, "123456", "123456");
355   xbt_test_log0("Child becomes child of what to add");
356   debuged_add(*set, "1234", "1234");
357   xbt_test_log0("Need of common ancestor");
358   debuged_add(*set, "123457", "123457");
359 }
360
361 static void search_name(xbt_set_t head, const char *key)
362 {
363   my_elem_t elm;
364
365   xbt_test_add1("Search by name %s", key);
366   elm = (my_elem_t) xbt_set_get_by_name(head, key);
367   xbt_test_log2(" Found %s (under ID %d)\n",
368                 elm ? elm->data : "(null)", elm ? elm->ID : -1);
369   if (strcmp(key, elm->name))
370     THROW2(mismatch_error, 0, "The key (%s) is not the one expected (%s)",
371            key, elm->name);
372   if (strcmp(elm->name, elm->data))
373     THROW2(mismatch_error, 0, "The name (%s) != data (%s)", key, elm->name);
374   fflush(stdout);
375 }
376
377 static void search_id(xbt_set_t head, int id, const char *key)
378 {
379   my_elem_t elm;
380
381   xbt_test_add1("Search by id %d", id);
382   elm = (my_elem_t) xbt_set_get_by_id(head, id);
383   xbt_test_log2("Found %s (data %s)",
384                 elm ? elm->name : "(null)", elm ? elm->data : "(null)");
385   if (id != elm->ID)
386     THROW2(mismatch_error, 0,
387            "The found ID (%d) is not the one expected (%d)", elm->ID, id);
388   if (strcmp(key, elm->name))
389     THROW2(mismatch_error, 0, "The key (%s) is not the one expected (%s)",
390            elm->name, key);
391   if (strcmp(elm->name, elm->data))
392     THROW2(mismatch_error, 0, "The name (%s) != data (%s)",
393            elm->name, elm->data);
394 }
395
396
397 static void traverse(xbt_set_t set)
398 {
399   xbt_set_cursor_t cursor = NULL;
400   my_elem_t elm = NULL;
401
402   xbt_set_foreach(set, cursor, elm) {
403     xbt_test_assert0(elm, "Dude ! Got a null elm during traversal!");
404     xbt_test_log3("Id(%d):  %s->%s\n", elm->ID, elm->name, elm->data);
405     xbt_test_assert2(!strcmp(elm->name, elm->data),
406                      "Key(%s) != value(%s). Abording", elm->name, elm->data);
407   }
408 }
409
410 static void search_not_found(xbt_set_t set, const char *data)
411 {
412   xbt_ex_t e;
413
414   xbt_test_add1("Search %s (expected not to be found)", data);
415   TRY {
416     xbt_set_get_by_name(set, data);
417     THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
418            data);
419   } CATCH(e) {
420     if (e.category != not_found_error)
421       xbt_test_exception(e);
422     xbt_ex_free(e);
423   }
424 }
425
426 xbt_set_t set = NULL;
427
428
429 XBT_TEST_UNIT("basic", test_set_basic, "Basic usage")
430 {
431   set = NULL;
432
433   xbt_test_add0("Traverse the empty set");
434   traverse(set);
435
436   xbt_test_add0("Free a data set");
437   fill(&set);
438   xbt_set_free(&set);
439
440   xbt_test_add0("Free the NULL data set");
441   xbt_set_free(&set);
442
443 }
444
445 XBT_TEST_UNIT("change", test_set_change, "Changing some values")
446 {
447   fill(&set);
448
449   xbt_test_add0("Change 123 to 'Changed 123'");
450   debuged_add(set, "123", "Changed 123");
451
452   xbt_test_add0("Change 123 back to '123'");
453   debuged_add(set, "123", "123");
454
455   xbt_test_add0("Change 12a to 'Dummy 12a'");
456   debuged_add(set, "12a", "Dummy 12a");
457
458   xbt_test_add0("Change 12a to '12a'");
459   debuged_add(set, "12a", "12a");
460
461   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
462   xbt_test_add0("Traverse the resulting data set");
463   traverse(set);
464 }
465
466 XBT_TEST_UNIT("retrieve", test_set_retrieve, "Retrieving some values")
467 {
468   my_elem_t elm;
469
470   xbt_test_add0("Search 123");
471   elm = (my_elem_t) xbt_set_get_by_name(set, "123");
472   xbt_test_assert0(elm, "elm must be there");
473   xbt_assert(!strcmp("123", elm->data));
474
475   search_not_found(set, "Can't be found");
476   search_not_found(set, "123 Can't be found");
477   search_not_found(set, "12345678 NOT");
478
479   search_name(set, "12");
480   search_name(set, "12a");
481   search_name(set, "12b");
482   search_name(set, "123");
483   search_name(set, "123456");
484   search_name(set, "1234");
485   search_name(set, "123457");
486
487   search_id(set, 0, "12");
488   search_id(set, 1, "12a");
489   search_id(set, 2, "12b");
490   search_id(set, 3, "123");
491   search_id(set, 4, "123456");
492   search_id(set, 5, "1234");
493   search_id(set, 6, "123457");
494
495   xbt_test_add0("Traverse the resulting data set");
496   traverse(set);
497
498   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
499
500   xbt_test_add0("Free the data set (twice)");
501   xbt_set_free(&set);
502   xbt_set_free(&set);
503
504   xbt_test_add0("Traverse the resulting data set");
505   traverse(set);
506 }
507
508 XBT_TEST_UNIT("remove", test_set_remove, "Removing some values")
509 {
510   my_elem_t elm;
511
512   xbt_set_free(&set);
513   fill(&set);
514
515   xbt_set_remove_by_name(set, "12a");
516   search_not_found(set, "12a");
517
518   search_name(set, "12");
519   search_name(set, "12b");
520   search_name(set, "123");
521   search_name(set, "123456");
522   search_name(set, "1234");
523   search_name(set, "123457");
524
525   search_id(set, 0, "12");
526   search_id(set, 2, "12b");
527   search_id(set, 3, "123");
528   search_id(set, 4, "123456");
529   search_id(set, 5, "1234");
530   search_id(set, 6, "123457");
531
532   debuged_add(set, "12anew", "12anew");
533   elm = (my_elem_t) xbt_set_get_by_id(set, 1);
534   xbt_test_assert1(elm->ID == 1, "elm->ID is %d but should be 1", elm->ID);
535 }
536
537 #endif /* SIMGRID_TEST */