3 /* set - data container consisting in dict+dynar */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
11 #include "xbt/sysdep.h"
13 #include "xbt/error.h"
14 #include "xbt/dynar.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt,"data container consisting in dict+dynar");
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 */
27 /*####[ Memory ]############################################################*/
34 xbt_set_t xbt_set_new (void) {
35 xbt_set_t res=xbt_new(s_xbt_set_t,1);
37 res->dict=xbt_dict_new ();
38 res->dynar=xbt_dynar_new(sizeof(void*),NULL);
49 void xbt_set_free(xbt_set_t *set) {
51 xbt_dict_free ( &( (*set)->dict ) );
52 xbt_dynar_free( &( (*set)->dynar ) );
60 * \param set set to populate
61 * \param elm element to add.
62 * \param free_func How to add the data
64 * Add an element to a set.
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.
70 void xbt_set_add (xbt_set_t set,
72 void_f_pvoid_t *free_func) {
75 xbt_set_elm_t found_in_dict;
77 if (elm->name_len <= 0) {
78 elm->name_len = strlen(elm->name);
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)",
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);
97 xbt_assert_error(mismatch_error);
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);
110 * \param name Name of the searched cell
111 * \param dst where to put the found data into
113 * get a data stored in the cell by providing its name.
115 xbt_error_t xbt_set_get_by_name (xbt_set_t set,
117 /* OUT */xbt_set_elm_t *dst) {
119 errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
120 DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
126 * \param name Name of the searched cell
127 * \param name_len length of the name, when strlen cannot be trusted
128 * \param dst where to put the found data into
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).
134 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t set,
137 /* OUT */xbt_set_elm_t *dst) {
139 return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
145 * \param id what you're looking for
146 * \param dst where to put the found data into
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
151 xbt_error_t xbt_set_get_by_id (xbt_set_t set,
153 /* OUT */xbt_set_elm_t *dst) {
155 /* Don't bother checking the bounds, the dynar does so */
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);
167 typedef struct xbt_set_cursor_ {
170 } s_xbt_set_cursor_t;
174 * \param set on what to let the cursor iterate
175 * \param cursor dest address
177 * Create the cursor if it does not exists. Rewind it in any case.
179 void xbt_set_cursor_first (xbt_set_t set,
180 xbt_set_cursor_t *cursor) {
184 DEBUG0("Create the cursor on first use");
185 *cursor = xbt_new(s_xbt_set_cursor_t,1);
187 "Malloc error during the creation of the cursor");
189 (*cursor)->set = set;
190 xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
198 * \param cursor the cursor
200 * Move to the next element.
202 void xbt_set_cursor_step (xbt_set_cursor_t cursor) {
203 xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
208 * \param curs the cursor
209 * \param elm an element
210 * \return 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) ) {