Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface revolution: do not try to survive to malloc failure
[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 "gras_private.h"
12
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(set,gros,"data container consisting in dict+dynar");
14
15 /*####[ Type definition ]####################################################*/
16 struct gras_set_ {
17   gras_dict_t  *dict;  /* data stored by name */
18   gras_dynar_t *dynar; /* data stored by ID   */
19 };
20
21 /*####[ Memory  ]############################################################*/
22 /**
23  * gras_set_new:
24  * @dst: where to
25  *
26  * Creates a new set.
27  */
28 void gras_set_new (gras_set_t **dst) {
29   gras_set_t *res=gras_new(gras_set_t,1);
30   gras_error_t errcode;
31
32   gras_dict_new (&(res->dict));
33   gras_dynar_new(&(res->dynar), sizeof(void*),NULL);
34
35   *dst=res;
36 }
37
38 /**
39  * gras_set_free:
40  * @set:
41  *
42  * Frees a set.
43  */
44 void         gras_set_free(gras_set_t **set) {
45   if (*set) {
46     gras_dict_free ( &( (*set)->dict  ) );
47     gras_dynar_free(    (*set)->dynar  );
48     gras_free(*set);
49     *set=NULL;
50   }
51 }
52
53 /**
54  * gras_set_add:
55  * @set: set to populate
56  * @elm: element to add. 
57  * @free_ctn: How to add the data 
58  *
59  * Add an element to a set. 
60  *
61  * elm->name must be set;
62  * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
63  * elm->ID is attributed automatically.
64  */
65 void gras_set_add    (gras_set_t     *set,
66                       gras_set_elm_t *elm,
67                       void_f_pvoid_t *free_func) {
68
69   gras_error_t    errcode;
70   gras_set_elm_t *found_in_dict;
71
72   if (elm->name_len <= 0) {
73     elm->name_len = strlen(elm->name);
74   }
75
76   errcode = gras_dict_get_ext (set->dict, 
77                                     elm->name, elm->name_len,
78                                     (void**)&found_in_dict);
79   if (errcode == no_error) {
80     if (elm == found_in_dict) {
81       DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
82              elm->name, elm->ID);
83       return;
84     } else {
85       elm->ID=found_in_dict->ID;
86       DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
87       gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
88       gras_dynar_set(set->dynar, elm->ID, &elm);
89       return;
90     }
91   } else {
92     gras_assert_error(mismatch_error);
93   }
94
95   elm->ID = gras_dynar_length( set->dynar );
96   gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
97   gras_dynar_set(set->dynar, elm->ID, &elm);
98   DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
99
100 }
101
102 /**
103  * gras_set_get_by_name:
104  * @set:
105  * @name: Name of the searched cell
106  * @dst: where to put the found data into
107  *
108  * get a data stored in the cell by providing its name.
109  */
110 gras_error_t gras_set_get_by_name    (gras_set_t     *set,
111                                       const char     *name,
112                                       /* OUT */gras_set_elm_t **dst) {
113   gras_error_t errcode;
114   errcode = gras_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
115   DEBUG2("Lookup key %s: %s",name,gras_error_name(errcode));
116   return errcode;
117 }
118 /**
119  * gras_set_get_by_name_ext:
120  * @set:
121  * @name: Name of the searched cell
122  * @name_len: length of the name, when strlen cannot be trusted
123  * @dst: where to put the found data into
124  *
125  * get a data stored in the cell by providing its name (and the length
126  * of the name, when strlen cannot be trusted because you don't use a char*
127  * as name, you weird guy).
128  */
129 gras_error_t gras_set_get_by_name_ext(gras_set_t     *set,
130                                       const char     *name,
131                                       int             name_len,
132                                       /* OUT */gras_set_elm_t **dst) {
133
134   return gras_dict_get_ext (set->dict, name, name_len, (void**)dst);
135 }
136
137 /**
138  * gras_set_get_by_code:
139  * @set:
140  * @id: what you're looking for
141  * @dst: where to put the found data into
142  *
143  * get a data stored in the cell by providing its id. 
144  * @warning, if the ID does not exists, you're getting into trouble
145  */
146 gras_error_t gras_set_get_by_id      (gras_set_t     *set,
147                                       int             id,
148                                       /* OUT */gras_set_elm_t **dst) {
149
150   /* Don't bother checking the bounds, the dynar does so */
151
152   gras_dynar_get(set->dynar,id,dst);
153   DEBUG3("Lookup type of id %d (of %lu): %s", 
154          id, gras_dynar_length(set->dynar), (*dst)->name);
155   
156   return no_error;
157 }
158
159 /***
160  *** Cursors
161  ***/
162 struct gras_set_cursor_ {
163   gras_set_t *set;
164   int val;
165 };
166
167 /**
168  * gras_set_cursor_first:
169  * @set: on what to let the cursor iterate
170  * @cursor: dest address
171  *
172  * Create the cursor if it does not exists. Rewind it in any case.
173  */
174 void         gras_set_cursor_first       (gras_set_t   *set,
175                                           gras_set_cursor_t **cursor) {
176
177   if (set != NULL) {
178     if (!*cursor) {
179       DEBUG0("Create the cursor on first use");
180       *cursor = gras_new(gras_set_cursor_t,1);
181       gras_assert0(*cursor,
182                    "Malloc error during the creation of the cursor");
183     }
184     (*cursor)->set = set;
185     gras_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
186   } else {
187     *cursor = NULL;
188   }
189 }
190
191 /**
192  * gras_set_cursor_step:
193  * @cursor: the cursor
194  *
195  * Move to the next element. 
196  */
197 void         gras_set_cursor_step        (gras_set_cursor_t  *cursor) {
198   gras_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
199 }
200
201 /**
202  * gras_set_cursor_get_or_free:
203  * @cursor: the cursor
204  * @Returns: true if it's ok, false if there is no more data
205  *
206  * Get current data
207  */
208 int          gras_set_cursor_get_or_free (gras_set_cursor_t **curs,
209                                           gras_set_elm_t    **elm) {
210   gras_set_cursor_t *cursor;
211
212   if (!curs || !(*curs))
213     return FALSE;
214
215   cursor=*curs;
216
217   if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
218     gras_free(cursor);
219     *curs=NULL;
220     return FALSE;    
221   } 
222   return TRUE;
223 }