3 /* dict_cursor - iterators over dictionnaries */
5 /* Copyright (c) 2003, 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. */
12 #include "dict_private.h"
14 #include <string.h> /* strlen() */
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_cursor, xbt_dict,
18 "To traverse dictionaries");
21 /*####[ Dict cursor functions ]#############################################*/
22 /* To traverse (simple) dicts */
23 /* Don't add or remove entries to the dict while traversing !!! */
24 /*###########################################################################*/
25 struct xbt_dict_cursor_ {
26 xbt_dictelm_t current;
31 #undef xbt_dict_CURSOR_DEBUG
32 /*#define xbt_dict_CURSOR_DEBUG 1*/
35 * @param dict the dict
37 XBT_INLINE xbt_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t dict)
39 xbt_dict_cursor_t res = NULL;
41 res = xbt_new(s_xbt_dict_cursor_t, 1);
44 xbt_dict_cursor_rewind(res);
51 * @param cursor poor victim
53 XBT_INLINE void xbt_dict_cursor_free(xbt_dict_cursor_t * cursor)
62 * Sanity check to see if the head contains something
64 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor)
66 xbt_assert0(cursor, "Null cursor");
70 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
71 XBT_INLINE void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
73 CDEBUG0(xbt_dict_cursor, "xbt_dict_cursor_rewind");
77 if (cursor->dict != NULL) {
78 cursor->current = cursor->dict->table[0];
80 cursor->current = NULL;
85 * @brief Create the cursor if it does not exists. Rewind it in any case.
87 * @param dict on what to let the cursor iterate
88 * @param[out] cursor dest address
90 XBT_INLINE void xbt_dict_cursor_first(const xbt_dict_t dict, xbt_dict_cursor_t * cursor)
92 DEBUG0("xbt_dict_cursor_first");
94 DEBUG0("Create the cursor on first use");
95 *cursor = xbt_dict_cursor_new(dict);
97 xbt_dict_cursor_rewind(*cursor);
99 if (dict != NULL && (*cursor)->current == NULL) {
100 xbt_dict_cursor_step(*cursor); /* find the first element */
106 * \brief Move to the next element.
108 XBT_INLINE void xbt_dict_cursor_step(xbt_dict_cursor_t cursor)
110 xbt_dictelm_t current;
113 DEBUG0("xbt_dict_cursor_step");
116 current = cursor->current;
119 if (cursor->dict != NULL) {
121 if (current != NULL) {
122 DEBUG0("current is not null, take the next element");
123 current = current->next;
124 DEBUG1("next element: %p", current);
127 while (current == NULL && ++line <= cursor->dict->table_size) {
128 DEBUG0("current is NULL, take the next line");
129 current = cursor->dict->table[line];
130 DEBUG1("element in the next line: %p", current);
132 DEBUG2("search finished, current = %p, line = %d", current, line);
134 cursor->current = current;
140 * @brief Get current data, or free the cursor if there is no data left
142 * @returns true if it's ok, false if there is no more data
144 XBT_INLINE int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor,
145 char **key, void **data)
148 xbt_dictelm_t current;
150 DEBUG0("xbt_dict_get_or_free");
153 if (!cursor || !(*cursor))
156 current = (*cursor)->current;
157 if (current == NULL) { /* no data left */
158 xbt_dict_cursor_free(cursor);
163 *data = current->content;
168 * @brief Get current key
169 * @param cursor: the cursor
170 * @returns the current key
172 XBT_INLINE char *xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor)
174 __cursor_not_null(cursor);
176 return cursor->current->key;
180 * @brief Get current data
181 * @param cursor the cursor
182 * @returns the current data
184 XBT_INLINE void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor)
186 __cursor_not_null(cursor);
188 return cursor->current->content;