Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Data description]
[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,GRAS);
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_retrieve_ext (set->dict, 
81                                     elm->name, elm->name_len,
82                                     (void**) &found_in_dict);
83   if (errcode == no_error) {
84     elm->ID=found_in_dict->ID;
85     DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
86     TRY(gras_dict_insert_ext(set->dict, elm->name, elm->name_len, elm, free_func));
87     TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
88     return no_error;
89
90   } else if (errcode != mismatch_error) {
91     return errcode; /* I expected mismatch_error */
92   }
93
94   elm->ID = gras_dynar_length( set->dynar );
95   TRY(gras_dict_insert_ext(set->dict, elm->name, elm->name_len, elm, free_func));
96   TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
97   DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
98
99   return no_error;
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  * Retrieve 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
114   return gras_dict_retrieve_ext(set->dict, name, strlen(name), (void**) dst);
115 }
116 /**
117  * gras_set_get_by_name_ext:
118  * @set:
119  * @name: Name of the searched cell
120  * @name_len: length of the name, when strlen cannot be trusted
121  * @dst: where to put the found data into
122  *
123  * Retrieve a data stored in the cell by providing its name (and the length
124  * of the name, when strlen cannot be trusted because you don't use a char*
125  * as name, you weird guy).
126  */
127 gras_error_t gras_set_get_by_name_ext(gras_set_t     *set,
128                                       const char     *name,
129                                       int             name_len,
130                                       /* OUT */gras_set_elm_t **dst) {
131
132   return gras_dict_retrieve_ext (set->dict, name, name_len, (void**)dst);
133 }
134
135 /**
136  * gras_set_get_by_code:
137  * @set:
138  * @name: Name of the searched cell
139  * @name_len: length of the name, when strlen cannot be trusted
140  * @dst: where to put the found data into
141  *
142  * Retrieve a data stored in the cell by providing its name (and the length
143  * of the name, when strlen cannot be trusted because you don't use a char*
144  * as name, you weird guy).
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   DEBUG2("Lookup type of id %d (of %d)", 
150          id, gras_dynar_length(set->dynar));
151   if (id < gras_dynar_length(set->dynar) &&
152       id >= 0) {
153     gras_dynar_get(set->dynar,id,dst);
154     DEBUG3("Lookup type of id %d (of %d): %s", 
155            id, gras_dynar_length(set->dynar), (*dst)->name);
156   
157   } else {
158     DEBUG1("Cannot get ID %d: out of bound", id);
159     return mismatch_error;
160   }
161   return no_error;
162 }
163
164 /***
165  *** Cursors
166  ***/
167 struct gras_set_cursor_ {
168   gras_set_t *set;
169   int val;
170 };
171
172 /**
173  * gras_set_cursor_first:
174  * @set: on what to let the cursor iterate
175  * @cursor: dest address
176  *
177  * Create the cursor if it does not exists. Rewind it in any case.
178  */
179 void         gras_set_cursor_first       (gras_set_t   *set,
180                                           gras_set_cursor_t **cursor) {
181
182   if (set != NULL) {
183     if (!*cursor) {
184       DEBUG0("Create the cursor on first use");
185       *cursor = (gras_set_cursor_t*)malloc(sizeof(gras_set_cursor_t));
186       gras_assert0(*cursor,
187                    "Malloc error during the creation of the cursor");
188     }
189     (*cursor)->set = set;
190     gras_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
191   } else {
192     *cursor = NULL;
193   }
194 }
195
196 /**
197  * gras_set_cursor_step:
198  * @cursor: the cursor
199  *
200  * Move to the next element. 
201  */
202 void         gras_set_cursor_step        (gras_set_cursor_t  *cursor) {
203   gras_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
204 }
205
206 /**
207  * gras_set_cursor_get_or_free:
208  * @cursor: the cursor
209  * @Returns: true if it's ok, false if there is no more data
210  *
211  * Get current data
212  */
213 int          gras_set_cursor_get_or_free (gras_set_cursor_t **curs,
214                                           gras_set_elm_t    **elm) {
215   gras_set_cursor_t *cursor;
216
217   if (!curs || !(*curs))
218     return FALSE;
219
220   cursor=*curs;
221
222   if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
223     free(cursor);
224     *curs=NULL;
225     return FALSE;    
226   } 
227   return TRUE;
228 }