Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some new functions to this. Bloatware, here we come.
[simgrid.git] / src / xbt / set.c
1 /* $Id$ */
2
3 /* set - data container consisting in dict+dynar                            */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
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. */
9
10 #include "xbt/misc.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "xbt/ex.h"
14 #include "xbt/dynar.h"
15 #include "xbt/dict.h"
16
17 #include "xbt/set.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt,
20              "set: data container consisting in dict+dynar");
21
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 } s_xbt_set_t;
27
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);
32
33   res->dict=xbt_dict_new ();
34   res->dynar=xbt_dynar_new(sizeof(void*),NULL);
35
36   return res;
37 }
38
39 /** @brief Destructor */
40 void  xbt_set_free(xbt_set_t *set) {
41   if (*set) {
42     xbt_dict_free ( &( (*set)->dict  ) );
43     xbt_dynar_free( &( (*set)->dynar ) );
44     free(*set);
45     *set = NULL;
46   }
47 }
48
49 /** @brief Add an element to a set. 
50  *
51  * \param set set to populate
52  * \param elm element to add. 
53  * \param free_func How to add the data 
54  *
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.
58  */
59 void xbt_set_add    (xbt_set_t      set,
60                       xbt_set_elm_t  elm,
61                       void_f_pvoid_t *free_func) {
62
63   int found = 1;
64   xbt_set_elm_t found_in_dict = NULL;
65   xbt_ex_t e;
66
67   if (elm->name_len <= 0) {
68     elm->name_len = strlen(elm->name);
69   }
70
71   TRY {
72     found_in_dict = xbt_dict_get_ext (set->dict, 
73                                       elm->name, elm->name_len);
74   } CATCH(e) {
75     if (e.category != not_found_error) 
76       RETHROW;
77     found = 0;
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);
82     xbt_ex_free(e);
83   }
84   
85   if (found) {
86     if (elm == found_in_dict) {
87       DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
88              elm->name, elm->ID);
89       return;
90     } else {
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);
95       return;
96     }
97   }
98 }
99
100 /** @brief Retrive data by providing its name.
101  * 
102  * \param set
103  * \param name Name of the searched cell
104  * \returns the data you're looking for
105  */
106 xbt_set_elm_t xbt_set_get_by_name    (xbt_set_t     set,
107                                       const char     *name) {
108   DEBUG1("Lookup key %s",name);
109   return xbt_dict_get_ext(set->dict, name, strlen(name));
110 }
111
112 /** @brief Retrive data by providing its name and the length of the name
113  *
114  * \param set
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
118  *
119  * This is useful when strlen cannot be trusted because you don't use a char*
120  * as name, you weirdo.
121  */
122 xbt_set_elm_t xbt_set_get_by_name_ext(xbt_set_t      set,
123                                       const char     *name,
124                                       int             name_len) {
125
126   return xbt_dict_get_ext (set->dict, name, name_len);
127 }
128
129 /** @brief Retrive data by providing its ID
130  *
131  * \param set
132  * \param id what you're looking for
133  * \returns the data you're looking for
134  *
135  * @warning, if the ID does not exists, you're getting into trouble
136  */
137 xbt_set_elm_t xbt_set_get_by_id (xbt_set_t set, int id) {
138   xbt_set_elm_t res;
139   
140   /* Don't bother checking the bounds, the dynar does so */
141
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);
145   
146   return res;
147 }
148
149 /** @brief Constructor */
150 unsigned long xbt_set_length (const xbt_set_t set) {
151    return xbt_dynar_length(set->dynar);
152 }
153
154 /***
155  *** Cursors
156  ***/
157 typedef struct xbt_set_cursor_ {
158   xbt_set_t set;
159   int val;
160 } s_xbt_set_cursor_t;
161
162 /** @brief Create the cursor if it does not exists, rewind it in any case. */
163 void         xbt_set_cursor_first       (xbt_set_t         set,
164                                           xbt_set_cursor_t *cursor) {
165
166   if (set != NULL) {
167     if (!*cursor) {
168       DEBUG0("Create the cursor on first use");
169       *cursor = xbt_new(s_xbt_set_cursor_t,1);
170       xbt_assert0(*cursor,
171                    "Malloc error during the creation of the cursor");
172     }
173     (*cursor)->set = set;
174     xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
175   } else {
176     *cursor = NULL;
177   }
178 }
179
180 /** @brief Move to the next element.  */
181 void         xbt_set_cursor_step        (xbt_set_cursor_t cursor) {
182   xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
183 }
184
185 /** @brief Get current data
186  * 
187  * \return true if it's ok, false if there is no more data
188  */
189 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
190                                           xbt_set_elm_t    *elm) {
191   xbt_set_cursor_t cursor;
192
193   if (!curs || !(*curs))
194     return FALSE;
195
196   cursor=*curs;
197
198   if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
199     free(cursor);
200     *curs=NULL;
201     return FALSE;    
202   } 
203   return TRUE;
204 }
205
206 #ifdef SIMGRID_TEST
207 #include "xbt.h"
208 #include "xbt/ex.h"
209
210 XBT_TEST_SUITE("set","Set data container");
211
212 typedef struct  {
213   /* headers */
214   unsigned int ID;
215   char        *name;
216   unsigned int name_len;
217
218   /* payload */
219   char         *data;
220 } s_my_elem_t,*my_elem_t;
221
222
223 static void my_elem_free(void *e) {
224   my_elem_t elm=(my_elem_t)e;
225
226   if (elm) {
227     free(elm->name);
228     free(elm->data);
229     free(elm);
230   }
231 }
232
233 static void debuged_add(xbt_set_t  set,
234                         const char *name,
235                         const char *data) {
236   my_elem_t    elm;
237
238   elm = xbt_new(s_my_elem_t,1);
239   elm->name=xbt_strdup(name);
240   elm->name_len=0;
241
242   elm->data=xbt_strdup(data);
243
244   xbt_test_log2("Add %s (->%s)",name,data);
245   xbt_set_add(set, (xbt_set_elm_t)elm, &my_elem_free);
246 }
247
248 static void fill(xbt_set_t *set) {
249   xbt_test_add0("Fill in the data set");
250
251   *set=xbt_set_new();
252   debuged_add(*set,"12",     "12");
253   debuged_add(*set,"12a",    "12a");
254   debuged_add(*set,"12b",    "12b");
255   debuged_add(*set,"123",    "123");
256   debuged_add(*set,"123456", "123456");
257   xbt_test_log0("Child becomes child of what to add");
258   debuged_add(*set,"1234",   "1234");
259   xbt_test_log0("Need of common ancestor");
260   debuged_add(*set,"123457", "123457");
261 }
262
263 static void search_name(xbt_set_t head,const char*key) {
264   my_elem_t elm;
265
266   xbt_test_add1("Search by name %s",key);
267   elm = (my_elem_t)xbt_set_get_by_name(head,key);
268   xbt_test_log2(" Found %s (under ID %d)\n",
269                 elm? elm->data:"(null)",
270                 elm? elm->ID:-1);
271   if (strcmp(key,elm->name))
272     THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
273            key,elm->name);
274   if (strcmp(elm->name,elm->data))
275     THROW2(mismatch_error,0,"The name (%s) != data (%s)",
276            key,elm->name);
277   fflush(stdout);
278 }
279
280 static void search_id(xbt_set_t head,int id,const char*key) {
281   my_elem_t elm;
282
283   xbt_test_add1("Search by id %d",id);
284   elm = (my_elem_t) xbt_set_get_by_id(head,id);
285   xbt_test_log2("Found %s (data %s)",
286                 elm? elm->name:"(null)",
287                 elm? elm->data:"(null)");
288   if (id != elm->ID)
289     THROW2(mismatch_error,0,"The found ID (%d) is not the one expected (%d)",
290            elm->ID,id);
291   if (strcmp(key,elm->name))
292     THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
293            elm->name,key);
294   if (strcmp(elm->name,elm->data))
295     THROW2(mismatch_error,0,"The name (%s) != data (%s)",
296            elm->name,elm->data);
297 }
298
299
300 static void traverse(xbt_set_t set) {
301   xbt_set_cursor_t cursor=NULL;
302   my_elem_t         elm=NULL;
303  
304   xbt_set_foreach(set,cursor,elm) {
305     xbt_test_assert0(elm,"Dude ! Got a null elm during traversal!");
306     xbt_test_log3("Id(%d):  %s->%s\n",elm->ID,elm->name,elm->data);
307     xbt_test_assert2(!strcmp(elm->name,elm->data),
308                      "Key(%s) != value(%s). Abording",
309                      elm->name,elm->data);
310   }
311 }
312
313 static void search_not_found(xbt_set_t set, const char *data) {
314   xbt_ex_t e;
315
316   xbt_test_add1("Search %s (expected not to be found)",data);
317   TRY {
318     xbt_set_get_by_name(set,data);
319     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
320   } CATCH(e) {
321     if (e.category != not_found_error) 
322       xbt_test_exception(e);
323     xbt_ex_free(e);  
324   }
325 }
326
327 xbt_set_t set = NULL;
328
329
330 XBT_TEST_UNIT("basic",test_set_basic,"Basic usage") {
331   set=NULL;
332
333   xbt_test_add0("Traverse the empty set");
334   traverse(set);
335   
336   xbt_test_add0("Free a data set");
337   fill(&set);
338   xbt_set_free(&set);
339
340   xbt_test_add0("Free the NULL data set");
341   xbt_set_free(&set);
342
343 }
344
345 XBT_TEST_UNIT("change",test_set_change,"Changing some values") {
346   fill(&set);
347
348   xbt_test_add0("Change 123 to 'Changed 123'");
349   debuged_add(set,"123","Changed 123");
350
351   xbt_test_add0("Change 123 back to '123'");
352   debuged_add(set,"123","123");
353
354   xbt_test_add0("Change 12a to 'Dummy 12a'");
355   debuged_add(set,"12a","Dummy 12a");
356
357   xbt_test_add0("Change 12a to '12a'");
358   debuged_add(set,"12a","12a");
359
360   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
361   xbt_test_add0("Traverse the resulting data set");
362   traverse(set);
363 }
364
365 XBT_TEST_UNIT("retrieve",test_set_retrieve,"Retrieving some values") {
366   my_elem_t elm;
367
368   xbt_test_add0("Search 123");
369   elm = (my_elem_t) xbt_set_get_by_name(set,"123");
370   xbt_test_assert0(elm,"elm must be there");
371   strcmp("123",elm->data);
372
373   search_not_found(set,"Can't be found");
374   search_not_found(set,"123 Can't be found");
375   search_not_found(set,"12345678 NOT");
376
377   search_name(set,"12");
378   search_name(set,"12a");
379   search_name(set,"12b");
380   search_name(set,"123");
381   search_name(set,"123456");
382   search_name(set,"1234");
383   search_name(set,"123457");
384
385   search_id(set,0,"12");
386   search_id(set,1,"12a");
387   search_id(set,2,"12b");
388   search_id(set,3,"123");
389   search_id(set,4,"123456");
390   search_id(set,5,"1234");
391   search_id(set,6,"123457");
392
393   xbt_test_add0("Traverse the resulting data set");
394   traverse(set);
395
396   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
397
398   xbt_test_add0("Free the data set (twice)");
399   xbt_set_free(&set);
400   xbt_set_free(&set);
401
402   xbt_test_add0("Traverse the resulting data set");
403   traverse(set);
404 }
405 #endif /* SIMGRID_TEST */