Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
re-doxygenification
[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,
20              "set: 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 /** @brief Constructor */
30 xbt_set_t xbt_set_new (void) {
31   xbt_set_t res=xbt_new(s_xbt_set_t,1);
32
33   res->dict=xbt_dict_new ();
34   res->dynar=xbt_dynar_new(sizeof(void*),NULL);
35
36   return res;
37 }
38
39 /** @brief Destructor */
40 void  xbt_set_free(xbt_set_t *set) {
41   if (*set) {
42     xbt_dict_free ( &( (*set)->dict  ) );
43     xbt_dynar_free( &( (*set)->dynar ) );
44     xbt_free(*set);
45     *set = NULL;
46   }
47 }
48
49 /** @brief Add an element to a set. 
50  *
51  * \param set set to populate
52  * \param elm element to add. 
53  * \param free_func How to add the data 
54  *
55  * elm->name must be set;
56  * if elm->name_len <= 0, it is recomputed. If >0, it's used as is;
57  * elm->ID is attributed automatically.
58  */
59 void xbt_set_add    (xbt_set_t      set,
60                       xbt_set_elm_t  elm,
61                       void_f_pvoid_t *free_func) {
62
63   xbt_error_t   errcode;
64   xbt_set_elm_t found_in_dict;
65
66   if (elm->name_len <= 0) {
67     elm->name_len = strlen(elm->name);
68   }
69
70   errcode = xbt_dict_get_ext (set->dict, 
71                                     elm->name, elm->name_len,
72                                     (void**)&found_in_dict);
73   if (errcode == no_error) {
74     if (elm == found_in_dict) {
75       DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
76              elm->name, elm->ID);
77       return;
78     } else {
79       elm->ID=found_in_dict->ID;
80       DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
81       xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
82       xbt_dynar_set(set->dynar, elm->ID, &elm);
83       return;
84     }
85   } else {
86     xbt_assert_error(mismatch_error);
87   }
88
89   elm->ID = xbt_dynar_length( set->dynar );
90   xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
91   xbt_dynar_set(set->dynar, elm->ID, &elm);
92   DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
93
94 }
95
96 /** @brief Retrive data by providing its name.
97  * 
98  * \param set
99  * \param name Name of the searched cell
100  * \param dst where to put the found data into
101  */
102 xbt_error_t xbt_set_get_by_name    (xbt_set_t     set,
103                                       const char     *name,
104                                       /* OUT */xbt_set_elm_t *dst) {
105   xbt_error_t errcode;
106   errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
107   DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
108   return errcode;
109 }
110
111 /** @brief Retrive data by providing its name and the length of the name
112  *
113  * \param set
114  * \param name Name of the searched cell
115  * \param name_len length of the name, when strlen cannot be trusted
116  * \param dst where to put the found data into
117  *
118  * This is useful when strlen cannot be trusted because you don't use a char*
119  * as name, you weirdo.
120  */
121 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t      set,
122                                       const char     *name,
123                                       int             name_len,
124                                       /* OUT */xbt_set_elm_t *dst) {
125
126   return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
127 }
128
129 /** @brief Retrive data by providing its ID
130  *
131  * \param set
132  * \param id what you're looking for
133  * \param dst where to put the found data into
134  *
135  * @warning, if the ID does not exists, you're getting into trouble
136  */
137 xbt_error_t xbt_set_get_by_id      (xbt_set_t      set,
138                                       int             id,
139                                       /* OUT */xbt_set_elm_t *dst) {
140
141   /* Don't bother checking the bounds, the dynar does so */
142
143   *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
144   DEBUG3("Lookup type of id %d (of %lu): %s", 
145          id, xbt_dynar_length(set->dynar), (*dst)->name);
146   
147   return no_error;
148 }
149
150 /***
151  *** Cursors
152  ***/
153 typedef struct xbt_set_cursor_ {
154   xbt_set_t set;
155   int val;
156 } s_xbt_set_cursor_t;
157
158 /** @brief Create the cursor if it does not exists, rewind it in any case. */
159 void         xbt_set_cursor_first       (xbt_set_t         set,
160                                           xbt_set_cursor_t *cursor) {
161
162   if (set != NULL) {
163     if (!*cursor) {
164       DEBUG0("Create the cursor on first use");
165       *cursor = xbt_new(s_xbt_set_cursor_t,1);
166       xbt_assert0(*cursor,
167                    "Malloc error during the creation of the cursor");
168     }
169     (*cursor)->set = set;
170     xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
171   } else {
172     *cursor = NULL;
173   }
174 }
175
176 /** @brief Move to the next element.  */
177 void         xbt_set_cursor_step        (xbt_set_cursor_t cursor) {
178   xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
179 }
180
181 /** @brief Get current data
182  * 
183  * \return true if it's ok, false if there is no more data
184  */
185 int          xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
186                                           xbt_set_elm_t    *elm) {
187   xbt_set_cursor_t cursor;
188
189   if (!curs || !(*curs))
190     return FALSE;
191
192   cursor=*curs;
193
194   if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {
195     xbt_free(cursor);
196     *curs=NULL;
197     return FALSE;    
198   } 
199   return TRUE;
200 }