Logo AND Algorithmique Numérique Distribuée

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