Logo AND Algorithmique Numérique Distribuée

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