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);
39 res->dict=xbt_dict_new ();
40 res->dynar=xbt_dynar_new(sizeof(void*),NULL);
51 void xbt_set_free(xbt_set_t *set) {
53 xbt_dict_free ( &( (*set)->dict ) );
54 xbt_dynar_free( &( (*set)->dynar ) );
62 * @set: set to populate
63 * @elm: element to add.
64 * @free_ctn: How to add the data
66 * Add an element to a set.
68 * elm->name must be set;
69 * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
70 * elm->ID is attributed automatically.
72 void xbt_set_add (xbt_set_t set,
74 void_f_pvoid_t *free_func) {
77 xbt_set_elm_t found_in_dict;
79 if (elm->name_len <= 0) {
80 elm->name_len = strlen(elm->name);
83 errcode = xbt_dict_get_ext (set->dict,
84 elm->name, elm->name_len,
85 (void**)&found_in_dict);
86 if (errcode == no_error) {
87 if (elm == found_in_dict) {
88 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
92 elm->ID=found_in_dict->ID;
93 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
94 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
95 xbt_dynar_set(set->dynar, elm->ID, &elm);
99 xbt_assert_error(mismatch_error);
102 elm->ID = xbt_dynar_length( set->dynar );
103 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
104 xbt_dynar_set(set->dynar, elm->ID, &elm);
105 DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
110 * xbt_set_get_by_name:
112 * @name: Name of the searched cell
113 * @dst: where to put the found data into
115 * get a data stored in the cell by providing its name.
117 xbt_error_t xbt_set_get_by_name (xbt_set_t set,
119 /* OUT */xbt_set_elm_t *dst) {
121 errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
122 DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
126 * xbt_set_get_by_name_ext:
128 * @name: Name of the searched cell
129 * @name_len: length of the name, when strlen cannot be trusted
130 * @dst: where to put the found data into
132 * get a data stored in the cell by providing its name (and the length
133 * of the name, when strlen cannot be trusted because you don't use a char*
134 * as name, you weird guy).
136 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t set,
139 /* OUT */xbt_set_elm_t *dst) {
141 return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
145 * xbt_set_get_by_code:
147 * @id: what you're looking for
148 * @dst: where to put the found data into
150 * get a data stored in the cell by providing its id.
151 * @warning, if the ID does not exists, you're getting into trouble
153 xbt_error_t xbt_set_get_by_id (xbt_set_t set,
155 /* OUT */xbt_set_elm_t *dst) {
157 /* Don't bother checking the bounds, the dynar does so */
159 *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
160 DEBUG3("Lookup type of id %d (of %lu): %s",
161 id, xbt_dynar_length(set->dynar), (*dst)->name);
169 typedef struct xbt_set_cursor_ {
172 } s_xbt_set_cursor_t;
175 * xbt_set_cursor_first:
176 * @set: on what to let the cursor iterate
177 * @cursor: dest address
179 * Create the cursor if it does not exists. Rewind it in any case.
181 void xbt_set_cursor_first (xbt_set_t set,
182 xbt_set_cursor_t *cursor) {
186 DEBUG0("Create the cursor on first use");
187 *cursor = xbt_new(s_xbt_set_cursor_t,1);
189 "Malloc error during the creation of the cursor");
191 (*cursor)->set = set;
192 xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
199 * xbt_set_cursor_step:
200 * @cursor: the cursor
202 * Move to the next element.
204 void xbt_set_cursor_step (xbt_set_cursor_t cursor) {
205 xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
209 * xbt_set_cursor_get_or_free:
210 * @cursor: the cursor
211 * @Returns: true if it's ok, false if there is no more data
215 int xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
216 xbt_set_elm_t *elm) {
217 xbt_set_cursor_t cursor;
219 if (!curs || !(*curs))
224 if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {