Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removed a bunch of unused variables. Mostly some xbt_error_t errcode that
[simgrid.git] / src / xbt / set.c
1 /* $Id$ */
2
3 /* set - data container consisting in dict+dynar                            */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 the GRAS posse.                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "xbt/misc.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/log.h"
14 #include "xbt/error.h"
15 #include "xbt/dynar.h"
16 #include "xbt/dict.h"
17
18 #include "xbt/set.h"
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt,"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 /**
30  * xbt_set_new:
31  * @dst: where to
32  *
33  * Creates a new set.
34  */
35 xbt_set_t xbt_set_new (void) {
36   xbt_set_t res=xbt_new(s_xbt_set_t,1);
37
38   res->dict=xbt_dict_new ();
39   res->dynar=xbt_dynar_new(sizeof(void*),NULL);
40
41   return res;
42 }
43
44 /**
45  * xbt_set_free:
46  * @set:
47  *
48  * Frees a set.
49  */
50 void  xbt_set_free(xbt_set_t *set) {
51   if (*set) {
52     xbt_dict_free ( &( (*set)->dict  ) );
53     xbt_dynar_free( &( (*set)->dynar ) );
54     xbt_free(*set);
55     *set = NULL;
56   }
57 }
58
59 /**
60  * xbt_set_add:
61  * @set: set to populate
62  * @elm: element to add. 
63  * @free_ctn: How to add the data 
64  *
65  * Add an element to a set. 
66  *
67  * elm->name must be set;
68  * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
69  * elm->ID is attributed automatically.
70  */
71 void xbt_set_add    (xbt_set_t      set,
72                       xbt_set_elm_t  elm,
73                       void_f_pvoid_t *free_func) {
74
75   xbt_error_t   errcode;
76   xbt_set_elm_t found_in_dict;
77
78   if (elm->name_len <= 0) {
79     elm->name_len = strlen(elm->name);
80   }
81
82   errcode = xbt_dict_get_ext (set->dict, 
83                                     elm->name, elm->name_len,
84                                     (void**)&found_in_dict);
85   if (errcode == no_error) {
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   } else {
98     xbt_assert_error(mismatch_error);
99   }
100
101   elm->ID = xbt_dynar_length( set->dynar );
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);
105
106 }
107
108 /**
109  * xbt_set_get_by_name:
110  * @set:
111  * @name: Name of the searched cell
112  * @dst: where to put the found data into
113  *
114  * get a data stored in the cell by providing its name.
115  */
116 xbt_error_t xbt_set_get_by_name    (xbt_set_t     set,
117                                       const char     *name,
118                                       /* OUT */xbt_set_elm_t *dst) {
119   xbt_error_t errcode;
120   errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
121   DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
122   return errcode;
123 }
124 /**
125  * xbt_set_get_by_name_ext:
126  * @set:
127  * @name: Name of the searched cell
128  * @name_len: length of the name, when strlen cannot be trusted
129  * @dst: where to put the found data into
130  *
131  * get a data stored in the cell by providing its name (and the length
132  * of the name, when strlen cannot be trusted because you don't use a char*
133  * as name, you weird guy).
134  */
135 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t      set,
136                                       const char     *name,
137                                       int             name_len,
138                                       /* OUT */xbt_set_elm_t *dst) {
139
140   return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
141 }
142
143 /**
144  * xbt_set_get_by_code:
145  * @set:
146  * @id: what you're looking for
147  * @dst: where to put the found data into
148  *
149  * get a data stored in the cell by providing its id. 
150  * @warning, if the ID does not exists, you're getting into trouble
151  */
152 xbt_error_t xbt_set_get_by_id      (xbt_set_t      set,
153                                       int             id,
154                                       /* OUT */xbt_set_elm_t *dst) {
155
156   /* Don't bother checking the bounds, the dynar does so */
157
158   *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
159   DEBUG3("Lookup type of id %d (of %lu): %s", 
160          id, xbt_dynar_length(set->dynar), (*dst)->name);
161   
162   return no_error;
163 }
164
165 /***
166  *** Cursors
167  ***/
168 typedef struct xbt_set_cursor_ {
169   xbt_set_t set;
170   int val;
171 } s_xbt_set_cursor_t;
172
173 /**
174  * xbt_set_cursor_first:
175  * @set: on what to let the cursor iterate
176  * @cursor: dest address
177  *
178  * Create the cursor if it does not exists. Rewind it in any case.
179  */
180 void         xbt_set_cursor_first       (xbt_set_t         set,
181                                           xbt_set_cursor_t *cursor) {
182
183   if (set != NULL) {
184     if (!*cursor) {
185       DEBUG0("Create the cursor on first use");
186       *cursor = xbt_new(s_xbt_set_cursor_t,1);
187       xbt_assert0(*cursor,
188                    "Malloc error during the creation of the cursor");
189     }
190     (*cursor)->set = set;
191     xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
192   } else {
193     *cursor = NULL;
194   }
195 }
196
197 /**
198  * xbt_set_cursor_step:
199  * @cursor: the cursor
200  *
201  * Move to the next element. 
202  */
203 void         xbt_set_cursor_step        (xbt_set_cursor_t cursor) {
204   xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
205 }
206
207 /**
208  * xbt_set_cursor_get_or_free:
209  * @cursor: the cursor
210  * @Returns: true if it's ok, false if there is no more data
211  *
212  * Get current data
213  */
214 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
215                                           xbt_set_elm_t    *elm) {
216   xbt_set_cursor_t cursor;
217
218   if (!curs || !(*curs))
219     return FALSE;
220
221   cursor=*curs;
222
223   if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
224     xbt_free(cursor);
225     *curs=NULL;
226     return FALSE;    
227   } 
228   return TRUE;
229 }