Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a83c005d589fa5aaee5a8ca3a6e7aa8c58696c44
[simgrid.git] / src / xbt / dict_cursor.c
1 /* dict_cursor - iterators over dictionnaries                               */
2
3 /* Copyright (c) 2004-2011. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/misc.h"
10 #include "xbt/ex.h"
11 #include "dict_private.h"
12
13 #include <string.h>             /* strlen() */
14
15 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_cursor, xbt_dict,
17                                 "To traverse dictionaries");
18
19
20 /*####[ Dict cursor functions ]#############################################*/
21 /* To traverse (simple) dicts                                               */
22 /* Don't add or remove entries to the dict while traversing !!!             */
23 /*###########################################################################*/
24 struct s_xbt_dict_cursor {
25   xbt_dictelm_t current;
26   int line;
27   xbt_dict_t dict;
28 };
29
30 #undef xbt_dict_CURSOR_DEBUG
31 /*#define xbt_dict_CURSOR_DEBUG 1*/
32
33 /** @brief Creator
34  *  @param dict the dict
35  */
36 XBT_INLINE xbt_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t dict)
37 {
38   xbt_dict_cursor_t res = NULL;
39
40   res = xbt_new(s_xbt_dict_cursor_t, 1);
41   res->dict = dict;
42
43   xbt_dict_cursor_rewind(res);
44
45   return res;
46 }
47
48 /**
49  * @brief Destructor
50  * @param cursor poor victim
51  */
52 XBT_INLINE void xbt_dict_cursor_free(xbt_dict_cursor_t * cursor)
53 {
54   xbt_free(*cursor);
55   *cursor = NULL;
56 }
57
58 /*
59  * Sanity check to see if the head contains something
60  */
61 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor)
62 {
63   xbt_assert(cursor, "Null cursor");
64 }
65
66
67 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
68 XBT_INLINE void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
69 {
70   XBT_CDEBUG(xbt_dict_cursor, "xbt_dict_cursor_rewind");
71   xbt_assert(cursor);
72
73   cursor->line = 0;
74   if (cursor->dict != NULL) {
75     cursor->current = cursor->dict->table[0];
76   } else {
77     cursor->current = NULL;
78   }
79 }
80
81 /**
82  * @brief Create the cursor if it does not exists. Rewind it in any case.
83  *
84  * @param      dict   on what to let the cursor iterate
85  * @param[out] cursor dest address
86  */
87 XBT_INLINE void xbt_dict_cursor_first(const xbt_dict_t dict,
88                                       xbt_dict_cursor_t * cursor)
89 {
90   XBT_DEBUG("xbt_dict_cursor_first");
91   if (!*cursor) {
92     XBT_DEBUG("Create the cursor on first use");
93     *cursor = xbt_dict_cursor_new(dict);
94   } else {
95     xbt_dict_cursor_rewind(*cursor);
96   }
97   if (dict != NULL && (*cursor)->current == NULL) {
98     xbt_dict_cursor_step(*cursor);      /* find the first element */
99   }
100 }
101
102
103 /**
104  * \brief Move to the next element.
105  */
106 XBT_INLINE void xbt_dict_cursor_step(xbt_dict_cursor_t cursor)
107 {
108   xbt_dictelm_t current;
109   int line;
110
111   XBT_DEBUG("xbt_dict_cursor_step");
112   xbt_assert(cursor);
113
114   current = cursor->current;
115   line = cursor->line;
116
117   if (cursor->dict != NULL) {
118
119     if (current != NULL) {
120       XBT_DEBUG("current is not null, take the next element");
121       current = current->next;
122       XBT_DEBUG("next element: %p", current);
123     }
124
125     while (current == NULL && ++line <= cursor->dict->table_size) {
126       XBT_DEBUG("current is NULL, take the next line");
127       current = cursor->dict->table[line];
128       XBT_DEBUG("element in the next line: %p", current);
129     }
130     XBT_DEBUG("search finished, current = %p, line = %d", current, line);
131
132     cursor->current = current;
133     cursor->line = line;
134   }
135 }
136
137 /**
138  * @brief Get current data, or free the cursor if there is no data left
139  *
140  * @returns true if it's ok, false if there is no more data
141  */
142 XBT_INLINE int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor,
143                                            char **key, void **data)
144 {
145
146   xbt_dictelm_t current;
147
148   XBT_DEBUG("xbt_dict_get_or_free");
149
150
151   if (!cursor || !(*cursor))
152     return FALSE;
153
154   current = (*cursor)->current;
155   if (current == NULL) {        /* no data left */
156     xbt_dict_cursor_free(cursor);
157     return FALSE;
158   }
159
160   *key = current->key;
161   *data = current->content;
162   return TRUE;
163 }
164
165 /**
166  * @brief Get current key
167  * @param cursor: the cursor
168  * @returns the current key
169  */
170 XBT_INLINE char *xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor)
171 {
172   __cursor_not_null(cursor);
173
174   return cursor->current->key;
175 }
176
177 /**
178  * @brief Get current data
179  * @param cursor the cursor
180  * @returns the current data
181  */
182 XBT_INLINE void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor)
183 {
184   __cursor_not_null(cursor);
185
186   return cursor->current->content;
187 }
188
189 /**
190  * @brief Set current data
191  * @param cursor the cursor
192  * @param data the new data
193  * @param free_ctn the function to free the new data
194  */
195 XBT_INLINE void xbt_dict_cursor_set_data(xbt_dict_cursor_t cursor,
196                                          void *data,
197                                          void_f_pvoid_t free_ctn)
198 {
199   __cursor_not_null(cursor);
200   xbt_dictelm_set_data(cursor->dict, cursor->current, data, free_ctn);
201 }