1 /* dict_cursor - iterators over dictionaries */
3 /* Copyright (c) 2004-2017. 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, "To traverse dictionaries");
17 /*####[ Dict cursor functions ]#############################################*/
18 /* To traverse (simple) dicts */
19 /* Don't add or remove entries to the dict while traversing !!! */
20 /*###########################################################################*/
23 * @param dict the dict
25 inline xbt_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t dict)
27 xbt_dict_cursor_t res = xbt_new(s_xbt_dict_cursor_t, 1);
30 xbt_dict_cursor_rewind(res);
37 * @param cursor poor victim
39 inline void xbt_dict_cursor_free(xbt_dict_cursor_t * cursor)
46 * Sanity check to see if the head contains something
48 static inline void __cursor_not_null(xbt_dict_cursor_t cursor)
50 xbt_assert(cursor, "Null cursor");
53 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
54 inline void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
56 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_rewind");
60 if (cursor->dict != NULL) {
61 cursor->current = cursor->dict->table[0];
63 cursor->current = NULL;
68 * @brief Create the cursor if it does not exists. Rewind it in any case.
70 * @param dict on what to let the cursor iterate
71 * @param[out] cursor dest address
73 inline void xbt_dict_cursor_first(const xbt_dict_t dict, xbt_dict_cursor_t * cursor)
75 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_first");
77 XBT_CDEBUG(xbt_dict_cursor, "Create the cursor on first use");
78 *cursor = xbt_dict_cursor_new(dict);
80 xbt_dict_cursor_rewind(*cursor);
82 if (dict != NULL && (*cursor)->current == NULL) {
83 xbt_dict_cursor_step(*cursor); /* find the first element */
87 /** \brief Move to the next element. */
88 inline void xbt_dict_cursor_step(xbt_dict_cursor_t cursor)
90 xbt_dictelm_t current;
93 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_step");
96 current = cursor->current;
99 if (cursor->dict != NULL) {
100 if (current != NULL) {
101 XBT_CDEBUG(xbt_dict_cursor, "current is not null, take the next element");
102 current = current->next;
103 XBT_CDEBUG(xbt_dict_cursor, "next element: %p", current);
106 while (current == NULL && (line + 1) <= cursor->dict->table_size) {
108 XBT_CDEBUG(xbt_dict_cursor, "current is NULL, take the next line");
109 current = cursor->dict->table[line];
110 XBT_CDEBUG(xbt_dict_cursor, "element in the next line: %p", current);
112 XBT_CDEBUG(xbt_dict_cursor, "search finished, current = %p, line = %d", current, line);
114 cursor->current = current;
120 * @brief Get current data, or free the cursor if there is no data left
122 * @returns true if it's ok, false if there is no more data
124 inline int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor, char **key, void **data)
126 xbt_dictelm_t current;
128 XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_get_or_free");
130 if (!cursor || !(*cursor))
133 current = (*cursor)->current;
134 if (current == NULL) { /* no data left */
135 xbt_dict_cursor_free(cursor);
140 *data = current->content;
145 * @brief Get current key
146 * @param cursor: the cursor
147 * @returns the current key
149 inline char *xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor)
151 __cursor_not_null(cursor);
153 return cursor->current->key;
157 * @brief Get current data
158 * @param cursor the cursor
159 * @returns the current data
161 inline void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor)
163 __cursor_not_null(cursor);
165 return cursor->current->content;
169 * @brief Set current data
170 * @param cursor the cursor
171 * @param data the new data
172 * @param free_ctn the function to free the new data
174 inline void xbt_dict_cursor_set_data(xbt_dict_cursor_t cursor, void *data, void_f_pvoid_t free_ctn)
176 __cursor_not_null(cursor);
177 xbt_dictelm_set_data(cursor->dict, cursor->current, data, free_ctn);