Logo AND Algorithmique Numérique Distribuée

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