Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b64b6bbb62449b357f7a2c6f18fa0a2405493a85
[simgrid.git] / src / xbt / dict_cursor.c
1 /* dict_cursor - iterators over dictionnaries                               */
2
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/misc.h"
9 #include "xbt/ex.h"
10 #include "dict_private.h"
11
12 #include <string.h>             /* strlen() */
13
14 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
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 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   if (*cursor) {
54     xbt_free(*cursor);
55     *cursor = NULL;
56   }
57 }
58
59 /*
60  * Sanity check to see if the head contains something
61  */
62 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor)
63 {
64   xbt_assert0(cursor, "Null cursor");
65 }
66
67
68 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
69 XBT_INLINE void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
70 {
71   CDEBUG0(xbt_dict_cursor, "xbt_dict_cursor_rewind");
72   xbt_assert(cursor);
73
74   cursor->line = 0;
75   if (cursor->dict != NULL) {
76     cursor->current = cursor->dict->table[0];
77   } else {
78     cursor->current = NULL;
79   }
80 }
81
82 /**
83  * @brief Create the cursor if it does not exists. Rewind it in any case.
84  *
85  * @param      dict   on what to let the cursor iterate
86  * @param[out] cursor dest address
87  */
88 XBT_INLINE void xbt_dict_cursor_first(const xbt_dict_t dict, xbt_dict_cursor_t * cursor)
89 {
90   DEBUG0("xbt_dict_cursor_first");
91   if (!*cursor) {
92     DEBUG0("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   DEBUG0("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       DEBUG0("current is not null, take the next element");
121       current = current->next;
122       DEBUG1("next element: %p", current);
123     }
124
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);
129     }
130     DEBUG2("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   DEBUG0("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 }