1 /* dict_cursor - iterators over dictionnaries */
3 /* Copyright (c) 2004-2014. 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 /*###########################################################################*/
24 #undef xbt_dict_CURSOR_DEBUG
25 /*#define xbt_dict_CURSOR_DEBUG 1*/
28 * @param dict the dict
30 XBT_INLINE xbt_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t dict)
32 xbt_dict_cursor_t res = NULL;
34 res = xbt_new(s_xbt_dict_cursor_t, 1);
37 xbt_dict_cursor_rewind(res);
44 * @param cursor poor victim
46 XBT_INLINE void xbt_dict_cursor_free(xbt_dict_cursor_t * cursor)
53 * Sanity check to see if the head contains something
55 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor)
57 xbt_assert(cursor, "Null cursor");
61 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
62 XBT_INLINE void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
64 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_rewind");
68 if (cursor->dict != NULL) {
69 cursor->current = cursor->dict->table[0];
71 cursor->current = NULL;
76 * @brief Create the cursor if it does not exists. Rewind it in any case.
78 * @param dict on what to let the cursor iterate
79 * @param[out] cursor dest address
81 XBT_INLINE void xbt_dict_cursor_first(const xbt_dict_t dict,
82 xbt_dict_cursor_t * cursor)
84 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_first");
86 XBT_CDEBUG(xbt_dict_cursor, "Create the cursor on first use");
87 *cursor = xbt_dict_cursor_new(dict);
89 xbt_dict_cursor_rewind(*cursor);
91 if (dict != NULL && (*cursor)->current == NULL) {
92 xbt_dict_cursor_step(*cursor); /* find the first element */
98 * \brief Move to the next element.
100 XBT_INLINE void xbt_dict_cursor_step(xbt_dict_cursor_t cursor)
102 xbt_dictelm_t current;
105 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_step");
108 current = cursor->current;
111 if (cursor->dict != NULL) {
113 if (current != NULL) {
114 XBT_CDEBUG(xbt_dict_cursor, "current is not null, take the next element");
115 current = current->next;
116 XBT_CDEBUG(xbt_dict_cursor, "next element: %p", current);
119 while (current == NULL && ++line <= cursor->dict->table_size) {
120 XBT_CDEBUG(xbt_dict_cursor, "current is NULL, take the next line");
121 current = cursor->dict->table[line];
122 XBT_CDEBUG(xbt_dict_cursor, "element in the next line: %p", current);
124 XBT_CDEBUG(xbt_dict_cursor, "search finished, current = %p, line = %d", current, line);
126 cursor->current = current;
132 * @brief Get current data, or free the cursor if there is no data left
134 * @returns true if it's ok, false if there is no more data
136 XBT_INLINE int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor,
137 char **key, void **data)
140 xbt_dictelm_t current;
142 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_get_or_free");
145 if (!cursor || !(*cursor))
148 current = (*cursor)->current;
149 if (current == NULL) { /* no data left */
150 xbt_dict_cursor_free(cursor);
155 *data = current->content;
160 * @brief Get current key
161 * @param cursor: the cursor
162 * @returns the current key
164 XBT_INLINE char *xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor)
166 __cursor_not_null(cursor);
168 return cursor->current->key;
172 * @brief Get current data
173 * @param cursor the cursor
174 * @returns the current data
176 XBT_INLINE void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor)
178 __cursor_not_null(cursor);
180 return cursor->current->content;
184 * @brief Set current data
185 * @param cursor the cursor
186 * @param data the new data
187 * @param free_ctn the function to free the new data
189 XBT_INLINE void xbt_dict_cursor_set_data(xbt_dict_cursor_t cursor,
191 void_f_pvoid_t free_ctn)
193 __cursor_not_null(cursor);
194 xbt_dictelm_set_data(cursor->dict, cursor->current, data, free_ctn);