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,"To traverse dictionaries");
20 /*####[ Dict cursor functions ]#############################################*/
21 /* To traverse (simple) dicts */
22 /* Don't add or remove entries to the dict while traversing !!! */
23 /*###########################################################################*/
24 struct xbt_dict_cursor_ {
25 xbt_dictelm_t current;
30 #undef xbt_dict_CURSOR_DEBUG
31 /*#define xbt_dict_CURSOR_DEBUG 1*/
34 * @param dict the dict
36 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 void xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
59 * Sanity check to see if the head contains something
61 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor) {
62 xbt_assert0(cursor, "Null cursor");
66 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
67 void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
68 CDEBUG0(xbt_dict_cursor, "xbt_dict_cursor_rewind");
72 if (cursor->dict != NULL) {
73 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 void xbt_dict_cursor_first(const xbt_dict_t dict,
87 xbt_dict_cursor_t *cursor){
88 DEBUG0("xbt_dict_cursor_first");
90 DEBUG0("Create the cursor on first use");
91 *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 void xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
108 xbt_dictelm_t current ;
111 DEBUG0("xbt_dict_cursor_step");
114 current = cursor->current;
117 if (cursor->dict != NULL) {
119 if (current != NULL) {
120 DEBUG0("current is not null, take the next element");
121 current = current->next;
122 DEBUG1("next element: %p", current);
125 while (current == NULL && ++line < cursor->dict->table_size) {
126 DEBUG0("current is NULL, take the next line");
127 current = cursor->dict->table[line];
128 DEBUG1("element in the next line: %p", current);
130 DEBUG2("search finished, current = %p, line = %d", current, line);
132 cursor->current = current;
138 * @brief Get current data, or free the cursor if there is no data left
140 * @returns true if it's ok, false if there is no more data
142 int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t *cursor,
146 xbt_dictelm_t current;
148 DEBUG0("xbt_dict_get_or_free");
151 if (!cursor || !(*cursor))
154 current = (*cursor)->current;
155 if (current == NULL) { /* no data left */
156 xbt_dict_cursor_free(cursor);
161 *data = current->content;
166 * @brief Get current key
167 * @param cursor: the cursor
168 * @returns the current key
170 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 void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor) {
182 __cursor_not_null(cursor);
184 return cursor->current->content;