1 /* set - data container consisting in dict+dynar */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
10 #include "xbt/sysdep.h"
13 #include "xbt/dynar.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_set, xbt,
19 "set: data container consisting in dict+dynar");
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 */
28 /*####[ Memory ]############################################################*/
30 static int _xbt_set_get_id(xbt_set_t set);
32 /** @brief Constructor */
33 xbt_set_t xbt_set_new(void)
35 xbt_set_t res = xbt_new(s_xbt_set_t, 1);
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);
44 /** @brief Destructor */
45 void xbt_set_free(xbt_set_t * set)
48 xbt_dict_free(&((*set)->dict));
49 xbt_dynar_free(&((*set)->dynar));
50 xbt_dynar_free(&((*set)->available_ids));
56 /* Compute an ID in order to add an element into the set. */
57 static int _xbt_set_get_id(xbt_set_t set)
60 if (xbt_dynar_length(set->available_ids) > 0) {
61 /* if there are some available ids */
62 xbt_dynar_pop(set->available_ids, &id);
64 /* otherwise we will add the element at the dynar end */
65 id = xbt_dynar_length(set->dynar);
70 /** @brief Add an element to a set.
72 * \param set set to populate
73 * \param elm element to add.
74 * \param free_func How to add the data
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.
80 void xbt_set_add(xbt_set_t set, xbt_set_elm_t elm, void_f_pvoid_t free_func)
84 xbt_set_elm_t found_in_dict = NULL;
87 VERB1("add %s to the set", elm->name);
89 if (elm->name_len <= 0) {
90 elm->name_len = strlen(elm->name);
94 found_in_dict = xbt_dict_get_ext(set->dict, elm->name, elm->name_len);
97 if (e.category != not_found_error)
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);
108 if (elm == found_in_dict) {
110 ("Ignoring request to insert the same element twice (key %s ; id %d)",
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);
123 /** @brief Remove an element from a set.
126 * \param elm element to remove
128 void xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm)
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);
134 xbt_dynar_set(set->dynar, id, &elm);
137 /** @brief Remove an element from a set providing its name.
140 * \param key name of the element to remove
142 void xbt_set_remove_by_name(xbt_set_t set, const char *key)
144 xbt_set_elm_t elm = xbt_set_get_by_name(set, key);
145 xbt_set_remove(set, elm);
148 /** @brief Remove an element from a set providing its name
149 * and the length of the name.
152 * \param key name of the element to remove
153 * \param key_len length of \a name
155 void xbt_set_remove_by_name_ext(xbt_set_t set, const char *key, int key_len)
157 xbt_set_elm_t elm = xbt_set_get_by_name_ext(set, key, key_len);
158 xbt_set_remove(set, elm);
161 /** @brief Remove an element from a set providing its id.
164 * \param id id of the element to remove
166 void xbt_set_remove_by_id(xbt_set_t set, int id)
168 xbt_set_elm_t elm = xbt_set_get_by_id(set, id);
169 xbt_set_remove(set, elm);
172 /** @brief Retrieve data by providing its name.
175 * \param name Name of the searched cell
176 * \returns the data you're looking for
178 xbt_set_elm_t xbt_set_get_by_name(xbt_set_t set, const char *name)
180 DEBUG1("Lookup key %s", name);
181 return xbt_dict_get(set->dict, name);
184 /** @brief Retrieve data by providing its name.
187 * \param name Name of the searched cell
188 * \returns the data you're looking for, returns NULL if not found
190 xbt_set_elm_t xbt_set_get_by_name_or_null(xbt_set_t set, const char *name)
192 DEBUG1("Lookup key %s", name);
193 return xbt_dict_get_or_null(set->dict, name);
196 /** @brief Retrieve data by providing its name and the length of the name
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
203 * This is useful when strlen cannot be trusted because you don't use a char*
204 * as name, you weirdo.
206 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
207 const char *name, int name_len)
210 return xbt_dict_get_ext(set->dict, name, name_len);
213 /** @brief Retrieve data by providing its ID
216 * \param id what you're looking for
217 * \returns the data you're looking for
219 * @warning, if the ID does not exists, you're getting into trouble
221 xbt_set_elm_t xbt_set_get_by_id(xbt_set_t set, int id)
225 /* Don't bother checking the bounds, the dynar does so */
227 res = xbt_dynar_get_as(set->dynar, id, xbt_set_elm_t);
229 THROW1(not_found_error, 0, "Invalid id: %d", id);
231 DEBUG3("Lookup type of id %d (of %lu): %s",
232 id, xbt_dynar_length(set->dynar), res->name);
238 * \brief Returns the number of elements in the set
240 * \return the number of elements in the set
242 unsigned long xbt_set_length(const xbt_set_t set)
244 return xbt_dynar_length(set->dynar);
250 typedef struct xbt_set_cursor_ {
253 } s_xbt_set_cursor_t;
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)
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");
266 (*cursor)->set = set;
268 /* place the cursor on the first element */
271 while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
280 /** @brief Move to the next element. */
281 void xbt_set_cursor_step(xbt_set_cursor_t cursor)
283 xbt_dynar_t dynar = cursor->set->dynar;
287 while (cursor->val < xbt_dynar_length(dynar) &&
288 xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
291 /** @brief Get current data
293 * \return true if it's ok, false if there is no more data
295 int xbt_set_cursor_get_or_free(xbt_set_cursor_t * curs, xbt_set_elm_t * elm)
297 xbt_set_cursor_t cursor;
299 if (!curs || !(*curs))
304 if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
310 xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
318 XBT_TEST_SUITE("set", "Set data container");
324 unsigned int name_len;
328 } s_my_elem_t, *my_elem_t;
331 static void my_elem_free(void *e)
333 my_elem_t elm = (my_elem_t) e;
342 static void debuged_add(xbt_set_t set, const char *name, const char *data)
346 elm = xbt_new(s_my_elem_t, 1);
347 elm->name = xbt_strdup(name);
350 elm->data = xbt_strdup(data);
352 xbt_test_log2("Add %s (->%s)", name, data);
353 xbt_set_add(set, (xbt_set_elm_t) elm, &my_elem_free);
356 static void fill(xbt_set_t * set)
358 xbt_test_add0("Fill in the data set");
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");
372 static void search_name(xbt_set_t head, const char *key)
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)",
383 if (strcmp(elm->name, elm->data))
384 THROW2(mismatch_error, 0, "The name (%s) != data (%s)", key, elm->name);
388 static void search_id(xbt_set_t head, int id, const char *key)
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)");
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)",
402 if (strcmp(elm->name, elm->data))
403 THROW2(mismatch_error, 0, "The name (%s) != data (%s)",
404 elm->name, elm->data);
408 static void traverse(xbt_set_t set)
410 xbt_set_cursor_t cursor = NULL;
411 my_elem_t elm = NULL;
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);
421 static void search_not_found(xbt_set_t set, const char *data)
425 xbt_test_add1("Search %s (expected not to be found)", data);
427 xbt_set_get_by_name(set, data);
428 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
431 if (e.category != not_found_error)
432 xbt_test_exception(e);
437 xbt_set_t set = NULL;
440 XBT_TEST_UNIT("basic", test_set_basic, "Basic usage")
444 xbt_test_add0("Traverse the empty set");
447 xbt_test_add0("Free a data set");
451 xbt_test_add0("Free the NULL data set");
456 XBT_TEST_UNIT("change", test_set_change, "Changing some values")
460 xbt_test_add0("Change 123 to 'Changed 123'");
461 debuged_add(set, "123", "Changed 123");
463 xbt_test_add0("Change 123 back to '123'");
464 debuged_add(set, "123", "123");
466 xbt_test_add0("Change 12a to 'Dummy 12a'");
467 debuged_add(set, "12a", "Dummy 12a");
469 xbt_test_add0("Change 12a to '12a'");
470 debuged_add(set, "12a", "12a");
472 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
473 xbt_test_add0("Traverse the resulting data set");
477 XBT_TEST_UNIT("retrieve", test_set_retrieve, "Retrieving some values")
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));
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");
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");
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");
506 xbt_test_add0("Traverse the resulting data set");
509 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
511 xbt_test_add0("Free the data set (twice)");
515 xbt_test_add0("Traverse the resulting data set");
519 XBT_TEST_UNIT("remove", test_set_remove, "Removing some values")
526 xbt_set_remove_by_name(set, "12a");
527 search_not_found(set, "12a");
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");
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");
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);
548 #endif /* SIMGRID_TEST */