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(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 */
28 /*####[ Memory ]############################################################*/
29 /** @brief Constructor */
30 xbt_set_t xbt_set_new (void) {
31 xbt_set_t res=xbt_new(s_xbt_set_t,1);
33 res->dict=xbt_dict_new ();
34 res->dynar=xbt_dynar_new(sizeof(void*),NULL);
39 /** @brief Destructor */
40 void xbt_set_free(xbt_set_t *set) {
42 xbt_dict_free ( &( (*set)->dict ) );
43 xbt_dynar_free( &( (*set)->dynar ) );
49 /** @brief Add an element to a set.
51 * \param set set to populate
52 * \param elm element to add.
53 * \param free_func How to add the data
55 * elm->name must be set;
56 * if elm->name_len <= 0, it is recomputed. If >0, it's used as is;
57 * elm->ID is attributed automatically.
59 void xbt_set_add (xbt_set_t set,
61 void_f_pvoid_t *free_func) {
64 xbt_set_elm_t found_in_dict = NULL;
67 if (elm->name_len <= 0) {
68 elm->name_len = strlen(elm->name);
72 found_in_dict = xbt_dict_get_ext (set->dict,
73 elm->name, elm->name_len);
75 if (e.category != not_found_error)
78 elm->ID = xbt_dynar_length( set->dynar );
79 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
80 xbt_dynar_set(set->dynar, elm->ID, &elm);
81 DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
86 if (elm == found_in_dict) {
87 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
91 elm->ID=found_in_dict->ID;
92 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
93 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
94 xbt_dynar_set(set->dynar, elm->ID, &elm);
100 /** @brief Retrive data by providing its name.
103 * \param name Name of the searched cell
104 * \returns the data you're looking for
106 xbt_set_elm_t xbt_set_get_by_name (xbt_set_t set,
108 DEBUG1("Lookup key %s",name);
109 return xbt_dict_get_ext(set->dict, name, strlen(name));
112 /** @brief Retrive data by providing its name and the length of the name
115 * \param name Name of the searched cell
116 * \param name_len length of the name, when strlen cannot be trusted
117 * \returns the data you're looking for
119 * This is useful when strlen cannot be trusted because you don't use a char*
120 * as name, you weirdo.
122 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t set,
126 return xbt_dict_get_ext (set->dict, name, name_len);
129 /** @brief Retrive data by providing its ID
132 * \param id what you're looking for
133 * \returns the data you're looking for
135 * @warning, if the ID does not exists, you're getting into trouble
137 xbt_set_elm_t xbt_set_get_by_id (xbt_set_t set, int id) {
140 /* Don't bother checking the bounds, the dynar does so */
142 res = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
143 DEBUG3("Lookup type of id %d (of %lu): %s",
144 id, xbt_dynar_length(set->dynar), res->name);
152 typedef struct xbt_set_cursor_ {
155 } s_xbt_set_cursor_t;
157 /** @brief Create the cursor if it does not exists, rewind it in any case. */
158 void xbt_set_cursor_first (xbt_set_t set,
159 xbt_set_cursor_t *cursor) {
163 DEBUG0("Create the cursor on first use");
164 *cursor = xbt_new(s_xbt_set_cursor_t,1);
166 "Malloc error during the creation of the cursor");
168 (*cursor)->set = set;
169 xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
175 /** @brief Move to the next element. */
176 void xbt_set_cursor_step (xbt_set_cursor_t cursor) {
177 xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
180 /** @brief Get current data
182 * \return true if it's ok, false if there is no more data
184 int xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
185 xbt_set_elm_t *elm) {
186 xbt_set_cursor_t cursor;
188 if (!curs || !(*curs))
193 if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
205 XBT_TEST_SUITE("set","Set data container");
211 unsigned int name_len;
215 } s_my_elem_t,*my_elem_t;
218 static void my_elem_free(void *e) {
219 my_elem_t elm=(my_elem_t)e;
228 static void debuged_add(xbt_set_t set,
233 elm = xbt_new(s_my_elem_t,1);
234 elm->name=xbt_strdup(name);
237 elm->data=xbt_strdup(data);
239 xbt_test_log2("Add %s (->%s)",name,data);
240 xbt_set_add(set, (xbt_set_elm_t)elm, &my_elem_free);
243 static void fill(xbt_set_t *set) {
244 xbt_test_add0("Fill in the data set");
247 debuged_add(*set,"12", "12");
248 debuged_add(*set,"12a", "12a");
249 debuged_add(*set,"12b", "12b");
250 debuged_add(*set,"123", "123");
251 debuged_add(*set,"123456", "123456");
252 xbt_test_log0("Child becomes child of what to add");
253 debuged_add(*set,"1234", "1234");
254 xbt_test_log0("Need of common ancestor");
255 debuged_add(*set,"123457", "123457");
258 static void search_name(xbt_set_t head,const char*key) {
261 xbt_test_add1("Search by name %s",key);
262 elm = (my_elem_t)xbt_set_get_by_name(head,key);
263 xbt_test_log2(" Found %s (under ID %d)\n",
264 elm? elm->data:"(null)",
266 if (strcmp(key,elm->name))
267 THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
269 if (strcmp(elm->name,elm->data))
270 THROW2(mismatch_error,0,"The name (%s) != data (%s)",
275 static void search_id(xbt_set_t head,int id,const char*key) {
278 xbt_test_add1("Search by id %d",id);
279 elm = (my_elem_t) xbt_set_get_by_id(head,id);
280 xbt_test_log2("Found %s (data %s)",
281 elm? elm->name:"(null)",
282 elm? elm->data:"(null)");
284 THROW2(mismatch_error,0,"The found ID (%d) is not the one expected (%d)",
286 if (strcmp(key,elm->name))
287 THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
289 if (strcmp(elm->name,elm->data))
290 THROW2(mismatch_error,0,"The name (%s) != data (%s)",
291 elm->name,elm->data);
295 static void traverse(xbt_set_t set) {
296 xbt_set_cursor_t cursor=NULL;
299 xbt_set_foreach(set,cursor,elm) {
300 xbt_test_assert0(elm,"Dude ! Got a null elm during traversal!");
301 xbt_test_log3("Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
302 xbt_test_assert2(!strcmp(elm->name,elm->data),
303 "Key(%s) != value(%s). Abording",
304 elm->name,elm->data);
308 static void search_not_found(xbt_set_t set, const char *data) {
311 xbt_test_add1("Search %s (expected not to be found)",data);
313 xbt_set_get_by_name(set,data);
314 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
316 if (e.category != not_found_error)
317 xbt_test_exception(e);
322 xbt_set_t set = NULL;
325 XBT_TEST_UNIT("basic",test_set_basic,"Basic usage") {
328 xbt_test_add0("Traverse the empty set");
331 xbt_test_add0("Free a data set");
335 xbt_test_add0("Free the NULL data set");
340 XBT_TEST_UNIT("change",test_set_change,"Changing some values") {
343 xbt_test_add0("Change 123 to 'Changed 123'");
344 debuged_add(set,"123","Changed 123");
346 xbt_test_add0("Change 123 back to '123'");
347 debuged_add(set,"123","123");
349 xbt_test_add0("Change 12a to 'Dummy 12a'");
350 debuged_add(set,"12a","Dummy 12a");
352 xbt_test_add0("Change 12a to '12a'");
353 debuged_add(set,"12a","12a");
355 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
356 xbt_test_add0("Traverse the resulting data set");
360 XBT_TEST_UNIT("retrieve",test_set_retrieve,"Retrieving some values") {
363 xbt_test_add0("Search 123");
364 elm = (my_elem_t) xbt_set_get_by_name(set,"123");
365 xbt_test_assert0(elm,"elm must be there");
366 strcmp("123",elm->data);
368 search_not_found(set,"Can't be found");
369 search_not_found(set,"123 Can't be found");
370 search_not_found(set,"12345678 NOT");
372 search_name(set,"12");
373 search_name(set,"12a");
374 search_name(set,"12b");
375 search_name(set,"123");
376 search_name(set,"123456");
377 search_name(set,"1234");
378 search_name(set,"123457");
380 search_id(set,0,"12");
381 search_id(set,1,"12a");
382 search_id(set,2,"12b");
383 search_id(set,3,"123");
384 search_id(set,4,"123456");
385 search_id(set,5,"1234");
386 search_id(set,6,"123457");
388 xbt_test_add0("Traverse the resulting data set");
391 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
393 xbt_test_add0("Free the data set (twice)");
397 xbt_test_add0("Traverse the resulting data set");
400 #endif /* SIMGRID_TEST */