3 /* set - data container consisting in dict+dynar */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 the GRAS posse. */
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. */
12 #include "xbt/sysdep.h"
14 #include "xbt/error.h"
15 #include "xbt/dynar.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt,"data container consisting in dict+dynar");
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 */
28 /*####[ Memory ]############################################################*/
35 xbt_set_t xbt_set_new (void) {
36 xbt_set_t res=xbt_new(s_xbt_set_t,1);
38 res->dict=xbt_dict_new ();
39 res->dynar=xbt_dynar_new(sizeof(void*),NULL);
50 void xbt_set_free(xbt_set_t *set) {
52 xbt_dict_free ( &( (*set)->dict ) );
53 xbt_dynar_free( &( (*set)->dynar ) );
61 * @set: set to populate
62 * @elm: element to add.
63 * @free_ctn: How to add the data
65 * Add an element to a set.
67 * elm->name must be set;
68 * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
69 * elm->ID is attributed automatically.
71 void xbt_set_add (xbt_set_t set,
73 void_f_pvoid_t *free_func) {
76 xbt_set_elm_t found_in_dict;
78 if (elm->name_len <= 0) {
79 elm->name_len = strlen(elm->name);
82 errcode = xbt_dict_get_ext (set->dict,
83 elm->name, elm->name_len,
84 (void**)&found_in_dict);
85 if (errcode == no_error) {
86 if (elm == found_in_dict) {
87 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
91 elm->ID=found_in_dict->ID;
92 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
93 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
94 xbt_dynar_set(set->dynar, elm->ID, &elm);
98 xbt_assert_error(mismatch_error);
101 elm->ID = xbt_dynar_length( set->dynar );
102 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
103 xbt_dynar_set(set->dynar, elm->ID, &elm);
104 DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
109 * xbt_set_get_by_name:
111 * @name: Name of the searched cell
112 * @dst: where to put the found data into
114 * get a data stored in the cell by providing its name.
116 xbt_error_t xbt_set_get_by_name (xbt_set_t set,
118 /* OUT */xbt_set_elm_t *dst) {
120 errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
121 DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
125 * xbt_set_get_by_name_ext:
127 * @name: Name of the searched cell
128 * @name_len: length of the name, when strlen cannot be trusted
129 * @dst: where to put the found data into
131 * get a data stored in the cell by providing its name (and the length
132 * of the name, when strlen cannot be trusted because you don't use a char*
133 * as name, you weird guy).
135 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t set,
138 /* OUT */xbt_set_elm_t *dst) {
140 return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
144 * xbt_set_get_by_code:
146 * @id: what you're looking for
147 * @dst: where to put the found data into
149 * get a data stored in the cell by providing its id.
150 * @warning, if the ID does not exists, you're getting into trouble
152 xbt_error_t xbt_set_get_by_id (xbt_set_t set,
154 /* OUT */xbt_set_elm_t *dst) {
156 /* Don't bother checking the bounds, the dynar does so */
158 *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
159 DEBUG3("Lookup type of id %d (of %lu): %s",
160 id, xbt_dynar_length(set->dynar), (*dst)->name);
168 typedef struct xbt_set_cursor_ {
171 } s_xbt_set_cursor_t;
174 * xbt_set_cursor_first:
175 * @set: on what to let the cursor iterate
176 * @cursor: dest address
178 * Create the cursor if it does not exists. Rewind it in any case.
180 void xbt_set_cursor_first (xbt_set_t set,
181 xbt_set_cursor_t *cursor) {
185 DEBUG0("Create the cursor on first use");
186 *cursor = xbt_new(s_xbt_set_cursor_t,1);
188 "Malloc error during the creation of the cursor");
190 (*cursor)->set = set;
191 xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
198 * xbt_set_cursor_step:
199 * @cursor: the cursor
201 * Move to the next element.
203 void xbt_set_cursor_step (xbt_set_cursor_t cursor) {
204 xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
208 * xbt_set_cursor_get_or_free:
209 * @cursor: the cursor
210 * @Returns: true if it's ok, false if there is no more data
214 int xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
215 xbt_set_elm_t *elm) {
216 xbt_set_cursor_t cursor;
218 if (!curs || !(*curs))
223 if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {