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 and the length of the name
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
192 * This is useful when strlen cannot be trusted because you don't use a char*
193 * as name, you weirdo.
195 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
196 const char *name, int name_len)
199 return xbt_dict_get_ext(set->dict, name, name_len);
202 /** @brief Retrieve data by providing its ID
205 * \param id what you're looking for
206 * \returns the data you're looking for
208 * @warning, if the ID does not exists, you're getting into trouble
210 xbt_set_elm_t xbt_set_get_by_id(xbt_set_t set, int id)
214 /* Don't bother checking the bounds, the dynar does so */
216 res = xbt_dynar_get_as(set->dynar, id, xbt_set_elm_t);
218 THROW1(not_found_error, 0, "Invalid id: %d", id);
220 DEBUG3("Lookup type of id %d (of %lu): %s",
221 id, xbt_dynar_length(set->dynar), res->name);
227 * \brief Returns the number of elements in the set
229 * \return the number of elements in the set
231 unsigned long xbt_set_length(const xbt_set_t set)
233 return xbt_dynar_length(set->dynar);
239 typedef struct xbt_set_cursor_ {
242 } s_xbt_set_cursor_t;
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)
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");
255 (*cursor)->set = set;
257 /* place the cursor on the first element */
260 while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
269 /** @brief Move to the next element. */
270 void xbt_set_cursor_step(xbt_set_cursor_t cursor)
272 xbt_dynar_t dynar = cursor->set->dynar;
276 while (cursor->val < xbt_dynar_length(dynar) &&
277 xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
280 /** @brief Get current data
282 * \return true if it's ok, false if there is no more data
284 int xbt_set_cursor_get_or_free(xbt_set_cursor_t * curs, xbt_set_elm_t * elm)
286 xbt_set_cursor_t cursor;
288 if (!curs || !(*curs))
293 if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
299 xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
307 XBT_TEST_SUITE("set", "Set data container");
313 unsigned int name_len;
317 } s_my_elem_t, *my_elem_t;
320 static void my_elem_free(void *e)
322 my_elem_t elm = (my_elem_t) e;
331 static void debuged_add(xbt_set_t set, const char *name, const char *data)
335 elm = xbt_new(s_my_elem_t, 1);
336 elm->name = xbt_strdup(name);
339 elm->data = xbt_strdup(data);
341 xbt_test_log2("Add %s (->%s)", name, data);
342 xbt_set_add(set, (xbt_set_elm_t) elm, &my_elem_free);
345 static void fill(xbt_set_t * set)
347 xbt_test_add0("Fill in the data set");
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");
361 static void search_name(xbt_set_t head, const char *key)
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)",
372 if (strcmp(elm->name, elm->data))
373 THROW2(mismatch_error, 0, "The name (%s) != data (%s)", key, elm->name);
377 static void search_id(xbt_set_t head, int id, const char *key)
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)");
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)",
391 if (strcmp(elm->name, elm->data))
392 THROW2(mismatch_error, 0, "The name (%s) != data (%s)",
393 elm->name, elm->data);
397 static void traverse(xbt_set_t set)
399 xbt_set_cursor_t cursor = NULL;
400 my_elem_t elm = NULL;
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);
410 static void search_not_found(xbt_set_t set, const char *data)
414 xbt_test_add1("Search %s (expected not to be found)", data);
416 xbt_set_get_by_name(set, data);
417 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
420 if (e.category != not_found_error)
421 xbt_test_exception(e);
426 xbt_set_t set = NULL;
429 XBT_TEST_UNIT("basic", test_set_basic, "Basic usage")
433 xbt_test_add0("Traverse the empty set");
436 xbt_test_add0("Free a data set");
440 xbt_test_add0("Free the NULL data set");
445 XBT_TEST_UNIT("change", test_set_change, "Changing some values")
449 xbt_test_add0("Change 123 to 'Changed 123'");
450 debuged_add(set, "123", "Changed 123");
452 xbt_test_add0("Change 123 back to '123'");
453 debuged_add(set, "123", "123");
455 xbt_test_add0("Change 12a to 'Dummy 12a'");
456 debuged_add(set, "12a", "Dummy 12a");
458 xbt_test_add0("Change 12a to '12a'");
459 debuged_add(set, "12a", "12a");
461 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
462 xbt_test_add0("Traverse the resulting data set");
466 XBT_TEST_UNIT("retrieve", test_set_retrieve, "Retrieving some values")
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));
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");
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");
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");
495 xbt_test_add0("Traverse the resulting data set");
498 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
500 xbt_test_add0("Free the data set (twice)");
504 xbt_test_add0("Traverse the resulting data set");
508 XBT_TEST_UNIT("remove", test_set_remove, "Removing some values")
515 xbt_set_remove_by_name(set, "12a");
516 search_not_found(set, "12a");
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");
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");
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);
537 #endif /* SIMGRID_TEST */