1 /* dict_cursor - iterators over dictionnaries */
3 /* Copyright (c) 2004-2011. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "dict_private.h"
13 #include <string.h> /* strlen() */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_cursor, xbt_dict,
16 "To traverse dictionaries");
19 /*####[ Dict cursor functions ]#############################################*/
20 /* To traverse (simple) dicts */
21 /* Don't add or remove entries to the dict while traversing !!! */
22 /*###########################################################################*/
23 struct s_xbt_dict_cursor {
24 xbt_dictelm_t current;
29 #undef xbt_dict_CURSOR_DEBUG
30 /*#define xbt_dict_CURSOR_DEBUG 1*/
33 * @param dict the dict
35 XBT_INLINE xbt_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t dict)
37 xbt_dict_cursor_t res = NULL;
39 res = xbt_new(s_xbt_dict_cursor_t, 1);
42 xbt_dict_cursor_rewind(res);
49 * @param cursor poor victim
51 XBT_INLINE void xbt_dict_cursor_free(xbt_dict_cursor_t * cursor)
58 * Sanity check to see if the head contains something
60 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor)
62 xbt_assert(cursor, "Null cursor");
66 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
67 XBT_INLINE void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
69 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_rewind");
73 if (cursor->dict != NULL) {
74 cursor->current = cursor->dict->table[0];
76 cursor->current = NULL;
81 * @brief Create the cursor if it does not exists. Rewind it in any case.
83 * @param dict on what to let the cursor iterate
84 * @param[out] cursor dest address
86 XBT_INLINE void xbt_dict_cursor_first(const xbt_dict_t dict,
87 xbt_dict_cursor_t * cursor)
89 XBT_DEBUG("xbt_dict_cursor_first");
91 XBT_DEBUG("Create the cursor on first use");
92 *cursor = xbt_dict_cursor_new(dict);
94 xbt_dict_cursor_rewind(*cursor);
96 if (dict != NULL && (*cursor)->current == NULL) {
97 xbt_dict_cursor_step(*cursor); /* find the first element */
103 * \brief Move to the next element.
105 XBT_INLINE void xbt_dict_cursor_step(xbt_dict_cursor_t cursor)
107 xbt_dictelm_t current;
110 XBT_DEBUG("xbt_dict_cursor_step");
113 current = cursor->current;
116 if (cursor->dict != NULL) {
118 if (current != NULL) {
119 XBT_DEBUG("current is not null, take the next element");
120 current = current->next;
121 XBT_DEBUG("next element: %p", current);
124 while (current == NULL && ++line <= cursor->dict->table_size) {
125 XBT_DEBUG("current is NULL, take the next line");
126 current = cursor->dict->table[line];
127 XBT_DEBUG("element in the next line: %p", current);
129 XBT_DEBUG("search finished, current = %p, line = %d", current, line);
131 cursor->current = current;
137 * @brief Get current data, or free the cursor if there is no data left
139 * @returns true if it's ok, false if there is no more data
141 XBT_INLINE int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor,
142 char **key, void **data)
145 xbt_dictelm_t current;
147 XBT_DEBUG("xbt_dict_get_or_free");
150 if (!cursor || !(*cursor))
153 current = (*cursor)->current;
154 if (current == NULL) { /* no data left */
155 xbt_dict_cursor_free(cursor);
160 *data = current->content;
165 * @brief Get current key
166 * @param cursor: the cursor
167 * @returns the current key
169 XBT_INLINE char *xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor)
171 __cursor_not_null(cursor);
173 return cursor->current->key;
177 * @brief Get current data
178 * @param cursor the cursor
179 * @returns the current data
181 XBT_INLINE void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor)
183 __cursor_not_null(cursor);
185 return cursor->current->content;
189 * @brief Set current data
190 * @param cursor the cursor
191 * @param data the new data
192 * @param free_ctn the function to free the new data
194 XBT_INLINE void xbt_dict_cursor_set_data(xbt_dict_cursor_t cursor,
196 void_f_pvoid_t free_ctn)
198 __cursor_not_null(cursor);
199 xbt_dictelm_set_data(cursor->dict, cursor->current, data, free_ctn);