Logo AND Algorithmique Numérique Distribuée

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