Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ignore generated
[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/error.h"
14 #include "xbt/dynar.h"
15 #include "xbt/dict.h"
16
17 #include "xbt/set.h"
18
19 /** \defgroup XBT_set A generic set datatype
20   *  \brief A data container consisting in \ref XBT_dict and \ref XBT_dynar
21   */
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt,"data container consisting in dict+dynar");
24
25 /*####[ Type definition ]####################################################*/
26 typedef struct xbt_set_ {
27   xbt_dict_t  dict;  /* data stored by name */
28   xbt_dynar_t dynar; /* data stored by ID   */
29 } s_xbt_set_t;
30
31 /*####[ Memory  ]############################################################*/
32 /**
33  * \ingroup XBT_set 
34  * \return a new set
35  *
36  * Creates a new set.
37  */
38 xbt_set_t xbt_set_new (void) {
39   xbt_set_t res=xbt_new(s_xbt_set_t,1);
40
41   res->dict=xbt_dict_new ();
42   res->dynar=xbt_dynar_new(sizeof(void*),NULL);
43
44   return res;
45 }
46
47 /**
48  * \ingroup XBT_set 
49  * \param set
50  *
51  * Frees a set.
52  */
53 void  xbt_set_free(xbt_set_t *set) {
54   if (*set) {
55     xbt_dict_free ( &( (*set)->dict  ) );
56     xbt_dynar_free( &( (*set)->dynar ) );
57     xbt_free(*set);
58     *set = NULL;
59   }
60 }
61
62 /**
63  * \ingroup XBT_set 
64  * \param set set to populate
65  * \param elm element to add. 
66  * \param free_func How to add the data 
67  *
68  * Add an element to a set. 
69  *
70  * elm->name must be set;
71  * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
72  * elm->ID is attributed automatically.
73  */
74 void xbt_set_add    (xbt_set_t      set,
75                       xbt_set_elm_t  elm,
76                       void_f_pvoid_t *free_func) {
77
78   xbt_error_t   errcode;
79   xbt_set_elm_t found_in_dict;
80
81   if (elm->name_len <= 0) {
82     elm->name_len = strlen(elm->name);
83   }
84
85   errcode = xbt_dict_get_ext (set->dict, 
86                                     elm->name, elm->name_len,
87                                     (void**)&found_in_dict);
88   if (errcode == no_error) {
89     if (elm == found_in_dict) {
90       DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
91              elm->name, elm->ID);
92       return;
93     } else {
94       elm->ID=found_in_dict->ID;
95       DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
96       xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
97       xbt_dynar_set(set->dynar, elm->ID, &elm);
98       return;
99     }
100   } else {
101     xbt_assert_error(mismatch_error);
102   }
103
104   elm->ID = xbt_dynar_length( set->dynar );
105   xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
106   xbt_dynar_set(set->dynar, elm->ID, &elm);
107   DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
108
109 }
110
111 /**
112  * \ingroup XBT_set 
113  * \param set
114  * \param name Name of the searched cell
115  * \param dst where to put the found data into
116  *
117  * get a data stored in the cell by providing its name.
118  */
119 xbt_error_t xbt_set_get_by_name    (xbt_set_t     set,
120                                       const char     *name,
121                                       /* OUT */xbt_set_elm_t *dst) {
122   xbt_error_t errcode;
123   errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
124   DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
125   return errcode;
126 }
127 /**
128  * \ingroup XBT_set 
129  * \param set
130  * \param name Name of the searched cell
131  * \param name_len length of the name, when strlen cannot be trusted
132  * \param dst where to put the found data into
133  *
134  * get a data stored in the cell by providing its name (and the length
135  * of the name, when strlen cannot be trusted because you don't use a char*
136  * as name, you weird guy).
137  */
138 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t      set,
139                                       const char     *name,
140                                       int             name_len,
141                                       /* OUT */xbt_set_elm_t *dst) {
142
143   return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
144 }
145
146 /**
147  * \ingroup XBT_set 
148  * \param set
149  * \param id what you're looking for
150  * \param dst where to put the found data into
151  *
152  * get a data stored in the cell by providing its id. 
153  * @warning, if the ID does not exists, you're getting into trouble
154  */
155 xbt_error_t xbt_set_get_by_id      (xbt_set_t      set,
156                                       int             id,
157                                       /* OUT */xbt_set_elm_t *dst) {
158
159   /* Don't bother checking the bounds, the dynar does so */
160
161   *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
162   DEBUG3("Lookup type of id %d (of %lu): %s", 
163          id, xbt_dynar_length(set->dynar), (*dst)->name);
164   
165   return no_error;
166 }
167
168 /***
169  *** Cursors
170  ***/
171 typedef struct xbt_set_cursor_ {
172   xbt_set_t set;
173   int val;
174 } s_xbt_set_cursor_t;
175
176 /**
177  * \ingroup XBT_set 
178  * \param set on what to let the cursor iterate
179  * \param cursor dest address
180  *
181  * Create the cursor if it does not exists. Rewind it in any case.
182  */
183 void         xbt_set_cursor_first       (xbt_set_t         set,
184                                           xbt_set_cursor_t *cursor) {
185
186   if (set != NULL) {
187     if (!*cursor) {
188       DEBUG0("Create the cursor on first use");
189       *cursor = xbt_new(s_xbt_set_cursor_t,1);
190       xbt_assert0(*cursor,
191                    "Malloc error during the creation of the cursor");
192     }
193     (*cursor)->set = set;
194     xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
195   } else {
196     *cursor = NULL;
197   }
198 }
199
200 /**
201  * \ingroup XBT_set 
202  * \param cursor the cursor
203  *
204  * Move to the next element. 
205  */
206 void         xbt_set_cursor_step        (xbt_set_cursor_t cursor) {
207   xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
208 }
209
210 /**
211  * \ingroup XBT_set 
212  * \param curs the cursor
213  * \param elm an element
214  * \return true if it's ok, false if there is no more data
215  *
216  * Get current data
217  */
218 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
219                                           xbt_set_elm_t    *elm) {
220   xbt_set_cursor_t cursor;
221
222   if (!curs || !(*curs))
223     return FALSE;
224
225   cursor=*curs;
226
227   if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
228     xbt_free(cursor);
229     *curs=NULL;
230     return FALSE;    
231   } 
232   return TRUE;
233 }