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) {
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) {
47 xbt_dict_free ( &( (*set)->dict ) );
48 xbt_dynar_free( &( (*set)->dynar ) );
49 xbt_dynar_free( &( (*set)->available_ids ) );
55 /* Compute an ID in order to add an element into the set. */
56 static int _xbt_set_get_id(xbt_set_t set) {
58 if (xbt_dynar_length(set->available_ids) > 0) {
59 /* if there are some available ids */
60 xbt_dynar_pop(set->available_ids, &id);
63 /* otherwise we will add the element at the dynar end */
64 id = xbt_dynar_length(set->dynar);
69 /** @brief Add an element to a set.
71 * \param set set to populate
72 * \param elm element to add.
73 * \param free_func How to add the data
75 * elm->name must be set;
76 * if elm->name_len <= 0, it is recomputed. If >0, it's used as is;
77 * elm->ID is attributed automatically.
79 void xbt_set_add (xbt_set_t set,
81 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,
95 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) {
109 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
113 elm->ID=found_in_dict->ID;
114 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
115 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
116 xbt_dynar_set(set->dynar, elm->ID, &elm);
122 /** @brief Remove an element from a set.
125 * \param elm element to remove
127 void xbt_set_remove (xbt_set_t set, xbt_set_elm_t elm) {
129 xbt_dynar_push_as(set->available_ids, int, id); /* this id becomes available now */
130 xbt_dict_remove_ext(set->dict, elm->name, elm->name_len);
132 xbt_dynar_set(set->dynar, id, &elm);
135 /** @brief Remove an element from a set providing its name.
138 * \param key name of the element to remove
140 void xbt_set_remove_by_name (xbt_set_t set, const char *key) {
141 xbt_set_elm_t elm = xbt_set_get_by_name(set, key);
142 xbt_set_remove(set, elm);
145 /** @brief Remove an element from a set providing its name
146 * and the length of the name.
149 * \param key name of the element to remove
150 * \param key_len length of \a name
152 void xbt_set_remove_by_name_ext (xbt_set_t set, const char *key, int key_len) {
153 xbt_set_elm_t elm = xbt_set_get_by_name_ext(set, key, key_len);
154 xbt_set_remove(set, elm);
157 /** @brief Remove an element from a set providing its id.
160 * \param id id of the element to remove
162 void xbt_set_remove_by_id (xbt_set_t set, int id) {
163 xbt_set_elm_t elm = xbt_set_get_by_id(set, id);
164 xbt_set_remove(set, elm);
167 /** @brief Retrieve data by providing its name.
170 * \param name Name of the searched cell
171 * \returns the data you're looking for
173 xbt_set_elm_t xbt_set_get_by_name (xbt_set_t set,
175 DEBUG1("Lookup key %s",name);
176 return xbt_dict_get(set->dict, name);
179 /** @brief Retrieve data by providing its name and the length of the name
182 * \param name Name of the searched cell
183 * \param name_len length of the name, when strlen cannot be trusted
184 * \returns the data you're looking for
186 * This is useful when strlen cannot be trusted because you don't use a char*
187 * as name, you weirdo.
189 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
193 return xbt_dict_get_ext (set->dict, name, name_len);
196 /** @brief Retrieve data by providing its ID
199 * \param id what you're looking for
200 * \returns the data you're looking for
202 * @warning, if the ID does not exists, you're getting into trouble
204 xbt_set_elm_t xbt_set_get_by_id (xbt_set_t set, int id) {
207 /* Don't bother checking the bounds, the dynar does so */
209 res = xbt_dynar_get_as(set->dynar,id, xbt_set_elm_t);
211 THROW1(not_found_error, 0, "Invalid id: %d", id);
213 DEBUG3("Lookup type of id %d (of %lu): %s",
214 id, xbt_dynar_length(set->dynar), res->name);
220 * \brief Returns the number of elements in the set
222 * \return the number of elements in the set
224 unsigned long xbt_set_length (const xbt_set_t set) {
225 return xbt_dynar_length(set->dynar);
231 typedef struct xbt_set_cursor_ {
234 } s_xbt_set_cursor_t;
236 /** @brief Create the cursor if it does not exists, rewind it in any case. */
237 void xbt_set_cursor_first (xbt_set_t set,
238 xbt_set_cursor_t *cursor) {
243 DEBUG0("Create the cursor on first use");
244 *cursor = xbt_new(s_xbt_set_cursor_t,1);
246 "Malloc error during the creation of the cursor");
248 (*cursor)->set = set;
250 /* place the cursor on the first element */
253 while (xbt_dynar_get_ptr(dynar, (*cursor)->val) == NULL) {
262 /** @brief Move to the next element. */
263 void xbt_set_cursor_step (xbt_set_cursor_t cursor) {
264 xbt_dynar_t dynar = cursor->set->dynar;
268 while (cursor->val < xbt_dynar_length(dynar) &&
269 xbt_dynar_get_ptr(dynar, cursor->val) == NULL);
272 /** @brief Get current data
274 * \return true if it's ok, false if there is no more data
276 int xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
277 xbt_set_elm_t *elm) {
278 xbt_set_cursor_t cursor;
280 if (!curs || !(*curs))
285 if (cursor->val >= xbt_dynar_length(cursor->set->dynar)) {
291 xbt_dynar_get_cpy(cursor->set->dynar, cursor->val, elm);
299 XBT_TEST_SUITE("set","Set data container");
305 unsigned int name_len;
309 } s_my_elem_t,*my_elem_t;
312 static void my_elem_free(void *e) {
313 my_elem_t elm=(my_elem_t)e;
322 static void debuged_add(xbt_set_t set,
327 elm = xbt_new(s_my_elem_t,1);
328 elm->name=xbt_strdup(name);
331 elm->data=xbt_strdup(data);
333 xbt_test_log2("Add %s (->%s)",name,data);
334 xbt_set_add(set, (xbt_set_elm_t)elm, &my_elem_free);
337 static void fill(xbt_set_t *set) {
338 xbt_test_add0("Fill in the data set");
341 debuged_add(*set,"12", "12");
342 debuged_add(*set,"12a", "12a");
343 debuged_add(*set,"12b", "12b");
344 debuged_add(*set,"123", "123");
345 debuged_add(*set,"123456", "123456");
346 xbt_test_log0("Child becomes child of what to add");
347 debuged_add(*set,"1234", "1234");
348 xbt_test_log0("Need of common ancestor");
349 debuged_add(*set,"123457", "123457");
352 static void search_name(xbt_set_t head,const char*key) {
355 xbt_test_add1("Search by name %s",key);
356 elm = (my_elem_t)xbt_set_get_by_name(head,key);
357 xbt_test_log2(" Found %s (under ID %d)\n",
358 elm? elm->data:"(null)",
360 if (strcmp(key,elm->name))
361 THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
363 if (strcmp(elm->name,elm->data))
364 THROW2(mismatch_error,0,"The name (%s) != data (%s)",
369 static void search_id(xbt_set_t head,int id,const char*key) {
372 xbt_test_add1("Search by id %d",id);
373 elm = (my_elem_t) xbt_set_get_by_id(head,id);
374 xbt_test_log2("Found %s (data %s)",
375 elm? elm->name:"(null)",
376 elm? elm->data:"(null)");
378 THROW2(mismatch_error,0,"The found ID (%d) is not the one expected (%d)",
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)",
385 elm->name,elm->data);
389 static void traverse(xbt_set_t set) {
390 xbt_set_cursor_t cursor=NULL;
393 xbt_set_foreach(set,cursor,elm) {
394 xbt_test_assert0(elm,"Dude ! Got a null elm during traversal!");
395 xbt_test_log3("Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
396 xbt_test_assert2(!strcmp(elm->name,elm->data),
397 "Key(%s) != value(%s). Abording",
398 elm->name,elm->data);
402 static void search_not_found(xbt_set_t set, const char *data) {
405 xbt_test_add1("Search %s (expected not to be found)",data);
407 xbt_set_get_by_name(set,data);
408 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
410 if (e.category != not_found_error)
411 xbt_test_exception(e);
416 xbt_set_t set = NULL;
419 XBT_TEST_UNIT("basic",test_set_basic,"Basic usage") {
422 xbt_test_add0("Traverse the empty set");
425 xbt_test_add0("Free a data set");
429 xbt_test_add0("Free the NULL data set");
434 XBT_TEST_UNIT("change",test_set_change,"Changing some values") {
437 xbt_test_add0("Change 123 to 'Changed 123'");
438 debuged_add(set,"123","Changed 123");
440 xbt_test_add0("Change 123 back to '123'");
441 debuged_add(set,"123","123");
443 xbt_test_add0("Change 12a to 'Dummy 12a'");
444 debuged_add(set,"12a","Dummy 12a");
446 xbt_test_add0("Change 12a to '12a'");
447 debuged_add(set,"12a","12a");
449 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
450 xbt_test_add0("Traverse the resulting data set");
454 XBT_TEST_UNIT("retrieve",test_set_retrieve,"Retrieving some values") {
457 xbt_test_add0("Search 123");
458 elm = (my_elem_t) xbt_set_get_by_name(set,"123");
459 xbt_test_assert0(elm,"elm must be there");
460 xbt_assert(!strcmp("123",elm->data));
462 search_not_found(set,"Can't be found");
463 search_not_found(set,"123 Can't be found");
464 search_not_found(set,"12345678 NOT");
466 search_name(set,"12");
467 search_name(set,"12a");
468 search_name(set,"12b");
469 search_name(set,"123");
470 search_name(set,"123456");
471 search_name(set,"1234");
472 search_name(set,"123457");
474 search_id(set,0,"12");
475 search_id(set,1,"12a");
476 search_id(set,2,"12b");
477 search_id(set,3,"123");
478 search_id(set,4,"123456");
479 search_id(set,5,"1234");
480 search_id(set,6,"123457");
482 xbt_test_add0("Traverse the resulting data set");
485 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
487 xbt_test_add0("Free the data set (twice)");
491 xbt_test_add0("Traverse the resulting data set");
495 XBT_TEST_UNIT("remove",test_set_remove,"Removing some values") {
501 xbt_set_remove_by_name(set, "12a");
502 search_not_found(set, "12a");
504 search_name(set,"12");
505 search_name(set,"12b");
506 search_name(set,"123");
507 search_name(set,"123456");
508 search_name(set,"1234");
509 search_name(set,"123457");
511 search_id(set,0,"12");
512 search_id(set,2,"12b");
513 search_id(set,3,"123");
514 search_id(set,4,"123456");
515 search_id(set,5,"1234");
516 search_id(set,6,"123457");
518 debuged_add(set, "12anew", "12anew");
519 elm = (my_elem_t) xbt_set_get_by_id(set, 1);
520 xbt_test_assert1(elm->ID == 1, "elm->ID is %d but should be 1", elm->ID);
523 #endif /* SIMGRID_TEST */