3 /* set - data container consisting in dict+dynar */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
11 #include "xbt/sysdep.h"
14 #include "xbt/dynar.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_set, xbt,
20 "set: data container consisting in dict+dynar");
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 */
29 /*####[ Memory ]############################################################*/
31 static int _xbt_set_get_id(xbt_set_t set);
33 /** @brief Constructor */
34 xbt_set_t xbt_set_new(void)
36 xbt_set_t res = xbt_new(s_xbt_set_t, 1);
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);
45 /** @brief Destructor */
46 void xbt_set_free(xbt_set_t * set)
49 xbt_dict_free(&((*set)->dict));
50 xbt_dynar_free(&((*set)->dynar));
51 xbt_dynar_free(&((*set)->available_ids));
57 /* Compute an ID in order to add an element into the set. */
58 static int _xbt_set_get_id(xbt_set_t set)
61 if (xbt_dynar_length(set->available_ids) > 0) {
62 /* if there are some available ids */
63 xbt_dynar_pop(set->available_ids, &id);
65 /* otherwise we will add the element at the dynar end */
66 id = xbt_dynar_length(set->dynar);
71 /** @brief Add an element to a set.
73 * \param set set to populate
74 * \param elm element to add.
75 * \param free_func How to add the data
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.
81 void xbt_set_add(xbt_set_t set, xbt_set_elm_t elm, void_f_pvoid_t free_func)
85 xbt_set_elm_t found_in_dict = NULL;
88 VERB1("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 DEBUG2("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 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);
124 /** @brief Remove an element from a set.
127 * \param elm element to remove
129 void xbt_set_remove(xbt_set_t set, xbt_set_elm_t elm)
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);
135 xbt_dynar_set(set->dynar, id, &elm);
138 /** @brief Remove an element from a set providing its name.
141 * \param key name of the element to remove
143 void xbt_set_remove_by_name(xbt_set_t set, const char *key)
145 xbt_set_elm_t elm = xbt_set_get_by_name(set, key);
146 xbt_set_remove(set, elm);
149 /** @brief Remove an element from a set providing its name
150 * and the length of the name.
153 * \param key name of the element to remove
154 * \param key_len length of \a name
156 void xbt_set_remove_by_name_ext(xbt_set_t set, const char *key, int key_len)
158 xbt_set_elm_t elm = xbt_set_get_by_name_ext(set, key, key_len);
159 xbt_set_remove(set, elm);
162 /** @brief Remove an element from a set providing its id.
165 * \param id id of the element to remove
167 void xbt_set_remove_by_id(xbt_set_t set, int id)
169 xbt_set_elm_t elm = xbt_set_get_by_id(set, id);
170 xbt_set_remove(set, elm);
173 /** @brief Retrieve data by providing its name.
176 * \param name Name of the searched cell
177 * \returns the data you're looking for
179 xbt_set_elm_t xbt_set_get_by_name(xbt_set_t set, const char *name)
181 DEBUG1("Lookup key %s", name);
182 return xbt_dict_get(set->dict, name);
185 /** @brief Retrieve data by providing its name.
188 * \param name Name of the searched cell
189 * \returns the data you're looking for, returns NULL if not found
191 xbt_set_elm_t xbt_set_get_by_name_or_null(xbt_set_t set, const char *name)
193 DEBUG1("Lookup key %s", name);
194 return xbt_dict_get_or_null(set->dict, name);
197 /** @brief Retrieve data by providing its name and the length of the name
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
204 * This is useful when strlen cannot be trusted because you don't use a char*
205 * as name, you weirdo.
207 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
208 const char *name, int name_len)
211 return xbt_dict_get_ext(set->dict, name, name_len);
214 /** @brief Retrieve data by providing its ID
217 * \param id what you're looking for
218 * \returns the data you're looking for
220 * @warning, if the ID does not exists, you're getting into trouble
222 xbt_set_elm_t xbt_set_get_by_id(xbt_set_t set, int id)
226 /* Don't bother checking the bounds, the dynar does so */
228 res = xbt_dynar_get_as(set->dynar, id, xbt_set_elm_t);
230 THROW1(not_found_error, 0, "Invalid id: %d", id);
232 DEBUG3("Lookup type of id %d (of %lu): %s",
233 id, xbt_dynar_length(set->dynar), res->name);
239 * \brief Returns the number of elements in the set
241 * \return the number of elements in the set
243 unsigned long xbt_set_length(const xbt_set_t set)
245 return xbt_dynar_length(set->dynar);
251 typedef struct xbt_set_cursor_ {
254 } s_xbt_set_cursor_t;
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)
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");
267 (*cursor)->set = set;
269 /* place the cursor on the first element */
272 while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
281 /** @brief Move to the next element. */
282 void xbt_set_cursor_step(xbt_set_cursor_t cursor)
284 xbt_dynar_t dynar = cursor->set->dynar;
288 while (cursor->val < xbt_dynar_length(dynar) &&
289 xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
292 /** @brief Get current data
294 * \return true if it's ok, false if there is no more data
296 int xbt_set_cursor_get_or_free(xbt_set_cursor_t * curs, xbt_set_elm_t * elm)
298 xbt_set_cursor_t cursor;
300 if (!curs || !(*curs))
305 if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
311 xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
319 XBT_TEST_SUITE("set", "Set data container");
325 unsigned int name_len;
329 } s_my_elem_t, *my_elem_t;
332 static void my_elem_free(void *e)
334 my_elem_t elm = (my_elem_t) e;
343 static void debuged_add(xbt_set_t set, const char *name, const char *data)
347 elm = xbt_new(s_my_elem_t, 1);
348 elm->name = xbt_strdup(name);
351 elm->data = xbt_strdup(data);
353 xbt_test_log2("Add %s (->%s)", name, data);
354 xbt_set_add(set, (xbt_set_elm_t) elm, &my_elem_free);
357 static void fill(xbt_set_t * set)
359 xbt_test_add0("Fill in the data set");
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");
373 static void search_name(xbt_set_t head, const char *key)
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)",
384 if (strcmp(elm->name, elm->data))
385 THROW2(mismatch_error, 0, "The name (%s) != data (%s)", key, elm->name);
389 static void search_id(xbt_set_t head, int id, const char *key)
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)");
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)",
403 if (strcmp(elm->name, elm->data))
404 THROW2(mismatch_error, 0, "The name (%s) != data (%s)",
405 elm->name, elm->data);
409 static void traverse(xbt_set_t set)
411 xbt_set_cursor_t cursor = NULL;
412 my_elem_t elm = NULL;
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);
422 static void search_not_found(xbt_set_t set, const char *data)
426 xbt_test_add1("Search %s (expected not to be found)", data);
428 xbt_set_get_by_name(set, data);
429 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
432 if (e.category != not_found_error)
433 xbt_test_exception(e);
438 xbt_set_t set = NULL;
441 XBT_TEST_UNIT("basic", test_set_basic, "Basic usage")
445 xbt_test_add0("Traverse the empty set");
448 xbt_test_add0("Free a data set");
452 xbt_test_add0("Free the NULL data set");
457 XBT_TEST_UNIT("change", test_set_change, "Changing some values")
461 xbt_test_add0("Change 123 to 'Changed 123'");
462 debuged_add(set, "123", "Changed 123");
464 xbt_test_add0("Change 123 back to '123'");
465 debuged_add(set, "123", "123");
467 xbt_test_add0("Change 12a to 'Dummy 12a'");
468 debuged_add(set, "12a", "Dummy 12a");
470 xbt_test_add0("Change 12a to '12a'");
471 debuged_add(set, "12a", "12a");
473 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
474 xbt_test_add0("Traverse the resulting data set");
478 XBT_TEST_UNIT("retrieve", test_set_retrieve, "Retrieving some values")
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));
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");
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");
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");
507 xbt_test_add0("Traverse the resulting data set");
510 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
512 xbt_test_add0("Free the data set (twice)");
516 xbt_test_add0("Traverse the resulting data set");
520 XBT_TEST_UNIT("remove", test_set_remove, "Removing some values")
527 xbt_set_remove_by_name(set, "12a");
528 search_not_found(set, "12a");
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");
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");
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);
549 #endif /* SIMGRID_TEST */