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,
81 void_f_pvoid_t free_func)
85 xbt_set_elm_t found_in_dict = NULL;
88 XBT_VERB("add %s to the set", elm->name);
90 if (elm->name_len <= 0) {
91 elm->name_len = strlen(elm->name);
95 found_in_dict = xbt_dict_get_ext(set->dict, elm->name, elm->name_len);
98 if (e.category != not_found_error)
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 XBT_DEBUG("Insertion of key '%s' (id %d)", elm->name, elm->ID);
109 if (elm == found_in_dict) {
111 ("Ignoring request to insert the same element twice (key %s ; id %d)",
115 elm->ID = found_in_dict->ID;
116 XBT_DEBUG("Reinsertion of key %s (id %d)", elm->name, elm->ID);
117 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm,
119 xbt_dynar_set(set->dynar, elm->ID, &elm);
125 /** @brief Remove an element from a set.
128 * \param elm element to remove
130 void xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm)
133 xbt_dynar_push_as(set->available_ids, int, id); /* this id becomes available now */
134 xbt_dict_remove_ext(set->dict, elm->name, elm->name_len);
136 xbt_dynar_set(set->dynar, id, &elm);
139 /** @brief Remove an element from a set providing its name.
142 * \param key name of the element to remove
144 void xbt_set_remove_by_name(xbt_set_t set, const char *key)
146 xbt_set_elm_t elm = xbt_set_get_by_name(set, key);
147 xbt_set_remove(set, elm);
150 /** @brief Remove an element from a set providing its name
151 * and the length of the name.
154 * \param key name of the element to remove
155 * \param key_len length of \a name
157 void xbt_set_remove_by_name_ext(xbt_set_t set, const char *key,
160 xbt_set_elm_t elm = xbt_set_get_by_name_ext(set, key, key_len);
161 xbt_set_remove(set, elm);
164 /** @brief Remove an element from a set providing its id.
167 * \param id id of the element to remove
169 void xbt_set_remove_by_id(xbt_set_t set, int id)
171 xbt_set_elm_t elm = xbt_set_get_by_id(set, id);
172 xbt_set_remove(set, elm);
175 /** @brief Retrieve data by providing its name.
178 * \param name Name of the searched cell
179 * \returns the data you're looking for
181 xbt_set_elm_t xbt_set_get_by_name(xbt_set_t set, const char *name)
183 XBT_DEBUG("Lookup key %s", name);
184 return xbt_dict_get(set->dict, name);
187 /** @brief Retrieve data by providing its name.
190 * \param name Name of the searched cell
191 * \returns the data you're looking for, returns NULL if not found
193 xbt_set_elm_t xbt_set_get_by_name_or_null(xbt_set_t set, const char *name)
195 XBT_DEBUG("Lookup key %s", name);
196 return xbt_dict_get_or_null(set->dict, name);
199 /** @brief Retrieve data by providing its name and the length of the name
202 * \param name Name of the searched cell
203 * \param name_len length of the name, when strlen cannot be trusted
204 * \returns the data you're looking for
206 * This is useful when strlen cannot be trusted because you don't use a char*
207 * as name, you weirdo.
209 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
210 const char *name, int name_len)
213 return xbt_dict_get_ext(set->dict, name, name_len);
216 /** @brief Retrieve data by providing its ID
219 * \param id what you're looking for
220 * \returns the data you're looking for
222 * @warning, if the ID does not exists, you're getting into trouble
224 xbt_set_elm_t xbt_set_get_by_id(xbt_set_t set, int id)
228 /* Don't bother checking the bounds, the dynar does so */
230 res = xbt_dynar_get_as(set->dynar, id, xbt_set_elm_t);
232 THROWF(not_found_error, 0, "Invalid id: %d", id);
234 XBT_DEBUG("Lookup type of id %d (of %lu): %s",
235 id, xbt_dynar_length(set->dynar), res->name);
241 * \brief Returns the number of elements in the set
243 * \return the number of elements in the set
245 unsigned long xbt_set_length(const xbt_set_t set)
247 return xbt_dynar_length(set->dynar);
253 typedef struct xbt_set_cursor_ {
256 } s_xbt_set_cursor_t;
258 /** @brief Create the cursor if it does not exists, rewind it in any case. */
259 void xbt_set_cursor_first(xbt_set_t set, xbt_set_cursor_t * cursor)
265 XBT_DEBUG("Create the cursor on first use");
266 *cursor = xbt_new(s_xbt_set_cursor_t, 1);
268 "Malloc error during the creation of the cursor");
270 (*cursor)->set = set;
272 /* place the cursor on the first element */
275 while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
284 /** @brief Move to the next element. */
285 void xbt_set_cursor_step(xbt_set_cursor_t cursor)
287 xbt_dynar_t dynar = cursor->set->dynar;
291 while (cursor->val < xbt_dynar_length(dynar) &&
292 xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
295 /** @brief Get current data
297 * \return true if it's ok, false if there is no more data
299 int xbt_set_cursor_get_or_free(xbt_set_cursor_t * curs,
302 xbt_set_cursor_t cursor;
304 if (!curs || !(*curs))
309 if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
315 xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
323 XBT_TEST_SUITE("set", "Set data container");
329 unsigned int name_len;
333 } s_my_elem_t, *my_elem_t;
336 static void my_elem_free(void *e)
338 my_elem_t elm = (my_elem_t) e;
347 static void debuged_add(xbt_set_t set, const char *name, const char *data)
351 elm = xbt_new(s_my_elem_t, 1);
352 elm->name = xbt_strdup(name);
355 elm->data = xbt_strdup(data);
357 xbt_test_log("Add %s (->%s)", name, data);
358 xbt_set_add(set, (xbt_set_elm_t) elm, &my_elem_free);
361 static void fill(xbt_set_t * set)
363 xbt_test_add("Fill in the data set");
365 *set = xbt_set_new();
366 debuged_add(*set, "12", "12");
367 debuged_add(*set, "12a", "12a");
368 debuged_add(*set, "12b", "12b");
369 debuged_add(*set, "123", "123");
370 debuged_add(*set, "123456", "123456");
371 xbt_test_log("Child becomes child of what to add");
372 debuged_add(*set, "1234", "1234");
373 xbt_test_log("Need of common ancestor");
374 debuged_add(*set, "123457", "123457");
377 static void search_name(xbt_set_t head, const char *key)
381 xbt_test_add("Search by name %s", key);
382 elm = (my_elem_t) xbt_set_get_by_name(head, key);
383 xbt_test_log(" Found %s (under ID %d)\n",
384 elm ? elm->data : "(null)", elm ? elm->ID : -1);
385 if (strcmp(key, elm->name))
386 THROWF(mismatch_error, 0, "The key (%s) is not the one expected (%s)",
388 if (strcmp(elm->name, elm->data))
389 THROWF(mismatch_error, 0, "The name (%s) != data (%s)", key,
394 static void search_id(xbt_set_t head, int id, const char *key)
398 xbt_test_add("Search by id %d", id);
399 elm = (my_elem_t) xbt_set_get_by_id(head, id);
400 xbt_test_log("Found %s (data %s)",
401 elm ? elm->name : "(null)", elm ? elm->data : "(null)");
403 THROWF(mismatch_error, 0,
404 "The found ID (%d) is not the one expected (%d)", elm->ID, id);
405 if (strcmp(key, elm->name))
406 THROWF(mismatch_error, 0, "The key (%s) is not the one expected (%s)",
408 if (strcmp(elm->name, elm->data))
409 THROWF(mismatch_error, 0, "The name (%s) != data (%s)",
410 elm->name, elm->data);
414 static void traverse(xbt_set_t set)
416 xbt_set_cursor_t cursor = NULL;
417 my_elem_t elm = NULL;
419 xbt_set_foreach(set, cursor, elm) {
420 xbt_test_assert(elm, "Dude ! Got a null elm during traversal!");
421 xbt_test_log("Id(%d): %s->%s\n", elm->ID, elm->name, elm->data);
422 xbt_test_assert(!strcmp(elm->name, elm->data),
423 "Key(%s) != value(%s). Abording", elm->name,
428 static void search_not_found(xbt_set_t set, const char *data)
432 xbt_test_add("Search %s (expected not to be found)", data);
434 xbt_set_get_by_name(set, data);
435 THROWF(unknown_error, 0,
436 "Found something which shouldn't be there (%s)", data);
439 if (e.category != not_found_error)
440 xbt_test_exception(e);
445 xbt_set_t set = NULL;
448 XBT_TEST_UNIT("basic", test_set_basic, "Basic usage")
452 xbt_test_add("Traverse the empty set");
455 xbt_test_add("Free a data set");
459 xbt_test_add("Free the NULL data set");
464 XBT_TEST_UNIT("change", test_set_change, "Changing some values")
468 xbt_test_add("Change 123 to 'Changed 123'");
469 debuged_add(set, "123", "Changed 123");
471 xbt_test_add("Change 123 back to '123'");
472 debuged_add(set, "123", "123");
474 xbt_test_add("Change 12a to 'Dummy 12a'");
475 debuged_add(set, "12a", "Dummy 12a");
477 xbt_test_add("Change 12a to '12a'");
478 debuged_add(set, "12a", "12a");
480 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
481 xbt_test_add("Traverse the resulting data set");
485 XBT_TEST_UNIT("retrieve", test_set_retrieve, "Retrieving some values")
489 xbt_test_add("Search 123");
490 elm = (my_elem_t) xbt_set_get_by_name(set, "123");
491 xbt_test_assert(elm, "elm must be there");
492 xbt_assert(!strcmp("123", elm->data));
494 search_not_found(set, "Can't be found");
495 search_not_found(set, "123 Can't be found");
496 search_not_found(set, "12345678 NOT");
498 search_name(set, "12");
499 search_name(set, "12a");
500 search_name(set, "12b");
501 search_name(set, "123");
502 search_name(set, "123456");
503 search_name(set, "1234");
504 search_name(set, "123457");
506 search_id(set, 0, "12");
507 search_id(set, 1, "12a");
508 search_id(set, 2, "12b");
509 search_id(set, 3, "123");
510 search_id(set, 4, "123456");
511 search_id(set, 5, "1234");
512 search_id(set, 6, "123457");
514 xbt_test_add("Traverse the resulting data set");
517 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
519 xbt_test_add("Free the data set (twice)");
523 xbt_test_add("Traverse the resulting data set");
527 XBT_TEST_UNIT("remove", test_set_remove, "Removing some values")
533 xbt_set_remove_by_name(set, "12a");
534 search_not_found(set, "12a");
536 search_name(set, "12");
537 search_name(set, "12b");
538 search_name(set, "123");
539 search_name(set, "123456");
540 search_name(set, "1234");
541 search_name(set, "123457");
543 search_id(set, 0, "12");
544 search_id(set, 2, "12b");
545 search_id(set, 3, "123");
546 search_id(set, 4, "123456");
547 search_id(set, 5, "1234");
548 search_id(set, 6, "123457");
550 debuged_add(set, "12anew", "12anew");
551 elm = (my_elem_t) xbt_set_get_by_id(set, 1);
552 xbt_test_assert(elm->ID == 1, "elm->ID is %d but should be 1", elm->ID);
557 #endif /* SIMGRID_TEST */