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 /** \defgroup XBT_set A generic set datatype
20 * \brief A data container consisting in \ref XBT_dict and \ref XBT_dynar
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(set,xbt,"data container consisting in dict+dynar");
25 /*####[ Type definition ]####################################################*/
26 typedef struct xbt_set_ {
27 xbt_dict_t dict; /* data stored by name */
28 xbt_dynar_t dynar; /* data stored by ID */
31 /*####[ Memory ]############################################################*/
38 xbt_set_t xbt_set_new (void) {
39 xbt_set_t res=xbt_new(s_xbt_set_t,1);
41 res->dict=xbt_dict_new ();
42 res->dynar=xbt_dynar_new(sizeof(void*),NULL);
53 void xbt_set_free(xbt_set_t *set) {
55 xbt_dict_free ( &( (*set)->dict ) );
56 xbt_dynar_free( &( (*set)->dynar ) );
64 * \param set set to populate
65 * \param elm element to add.
66 * \param free_func How to add the data
68 * Add an element to a set.
70 * elm->name must be set;
71 * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
72 * elm->ID is attributed automatically.
74 void xbt_set_add (xbt_set_t set,
76 void_f_pvoid_t *free_func) {
79 xbt_set_elm_t found_in_dict;
81 if (elm->name_len <= 0) {
82 elm->name_len = strlen(elm->name);
85 errcode = xbt_dict_get_ext (set->dict,
86 elm->name, elm->name_len,
87 (void**)&found_in_dict);
88 if (errcode == no_error) {
89 if (elm == found_in_dict) {
90 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
94 elm->ID=found_in_dict->ID;
95 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
96 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
97 xbt_dynar_set(set->dynar, elm->ID, &elm);
101 xbt_assert_error(mismatch_error);
104 elm->ID = xbt_dynar_length( set->dynar );
105 xbt_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
106 xbt_dynar_set(set->dynar, elm->ID, &elm);
107 DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
114 * \param name Name of the searched cell
115 * \param dst where to put the found data into
117 * get a data stored in the cell by providing its name.
119 xbt_error_t xbt_set_get_by_name (xbt_set_t set,
121 /* OUT */xbt_set_elm_t *dst) {
123 errcode = xbt_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
124 DEBUG2("Lookup key %s: %s",name,xbt_error_name(errcode));
130 * \param name Name of the searched cell
131 * \param name_len length of the name, when strlen cannot be trusted
132 * \param dst where to put the found data into
134 * get a data stored in the cell by providing its name (and the length
135 * of the name, when strlen cannot be trusted because you don't use a char*
136 * as name, you weird guy).
138 xbt_error_t xbt_set_get_by_name_ext(xbt_set_t set,
141 /* OUT */xbt_set_elm_t *dst) {
143 return xbt_dict_get_ext (set->dict, name, name_len, (void**)dst);
149 * \param id what you're looking for
150 * \param dst where to put the found data into
152 * get a data stored in the cell by providing its id.
153 * @warning, if the ID does not exists, you're getting into trouble
155 xbt_error_t xbt_set_get_by_id (xbt_set_t set,
157 /* OUT */xbt_set_elm_t *dst) {
159 /* Don't bother checking the bounds, the dynar does so */
161 *dst = xbt_dynar_get_as(set->dynar,id,xbt_set_elm_t);
162 DEBUG3("Lookup type of id %d (of %lu): %s",
163 id, xbt_dynar_length(set->dynar), (*dst)->name);
171 typedef struct xbt_set_cursor_ {
174 } s_xbt_set_cursor_t;
178 * \param set on what to let the cursor iterate
179 * \param cursor dest address
181 * Create the cursor if it does not exists. Rewind it in any case.
183 void xbt_set_cursor_first (xbt_set_t set,
184 xbt_set_cursor_t *cursor) {
188 DEBUG0("Create the cursor on first use");
189 *cursor = xbt_new(s_xbt_set_cursor_t,1);
191 "Malloc error during the creation of the cursor");
193 (*cursor)->set = set;
194 xbt_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
202 * \param cursor the cursor
204 * Move to the next element.
206 void xbt_set_cursor_step (xbt_set_cursor_t cursor) {
207 xbt_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
212 * \param curs the cursor
213 * \param elm an element
214 * \return true if it's ok, false if there is no more data
218 int xbt_set_cursor_get_or_free (xbt_set_cursor_t *curs,
219 xbt_set_elm_t *elm) {
220 xbt_set_cursor_t cursor;
222 if (!curs || !(*curs))
227 if (! xbt_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {