Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change mmalloc.h into a public header
[simgrid.git] / src / xbt / dict_cursor.c
1 /* dict_cursor - iterators over dictionnaries                               */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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 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   if (*cursor) {
55     xbt_free(*cursor);
56     *cursor = NULL;
57   }
58 }
59
60 /*
61  * Sanity check to see if the head contains something
62  */
63 static XBT_INLINE void __cursor_not_null(xbt_dict_cursor_t cursor)
64 {
65   xbt_assert0(cursor, "Null cursor");
66 }
67
68
69 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
70 XBT_INLINE void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor)
71 {
72   CDEBUG0(xbt_dict_cursor, "xbt_dict_cursor_rewind");
73   xbt_assert(cursor);
74
75   cursor->line = 0;
76   if (cursor->dict != NULL) {
77     cursor->current = cursor->dict->table[0];
78   } else {
79     cursor->current = NULL;
80   }
81 }
82
83 /**
84  * @brief Create the cursor if it does not exists. Rewind it in any case.
85  *
86  * @param      dict   on what to let the cursor iterate
87  * @param[out] cursor dest address
88  */
89 XBT_INLINE void xbt_dict_cursor_first(const xbt_dict_t dict, xbt_dict_cursor_t * cursor)
90 {
91   DEBUG0("xbt_dict_cursor_first");
92   if (!*cursor) {
93     DEBUG0("Create the cursor on first use");
94     *cursor = xbt_dict_cursor_new(dict);
95   } else {
96     xbt_dict_cursor_rewind(*cursor);
97   }
98   if (dict != NULL && (*cursor)->current == NULL) {
99     xbt_dict_cursor_step(*cursor);      /* find the first element */
100   }
101 }
102
103
104 /**
105  * \brief Move to the next element.
106  */
107 XBT_INLINE void xbt_dict_cursor_step(xbt_dict_cursor_t cursor)
108 {
109   xbt_dictelm_t current;
110   int line;
111
112   DEBUG0("xbt_dict_cursor_step");
113   xbt_assert(cursor);
114
115   current = cursor->current;
116   line = cursor->line;
117
118   if (cursor->dict != NULL) {
119
120     if (current != NULL) {
121       DEBUG0("current is not null, take the next element");
122       current = current->next;
123       DEBUG1("next element: %p", current);
124     }
125
126     while (current == NULL && ++line <= cursor->dict->table_size) {
127       DEBUG0("current is NULL, take the next line");
128       current = cursor->dict->table[line];
129       DEBUG1("element in the next line: %p", current);
130     }
131     DEBUG2("search finished, current = %p, line = %d", current, line);
132
133     cursor->current = current;
134     cursor->line = line;
135   }
136 }
137
138 /**
139  * @brief Get current data, or free the cursor if there is no data left
140  *
141  * @returns true if it's ok, false if there is no more data
142  */
143 XBT_INLINE int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t * cursor,
144                                 char **key, void **data)
145 {
146
147   xbt_dictelm_t current;
148
149   DEBUG0("xbt_dict_get_or_free");
150
151
152   if (!cursor || !(*cursor))
153     return FALSE;
154
155   current = (*cursor)->current;
156   if (current == NULL) {        /* no data left */
157     xbt_dict_cursor_free(cursor);
158     return FALSE;
159   }
160
161   *key = current->key;
162   *data = current->content;
163   return TRUE;
164 }
165
166 /**
167  * @brief Get current key
168  * @param cursor: the cursor
169  * @returns the current key
170  */
171 XBT_INLINE char *xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor)
172 {
173   __cursor_not_null(cursor);
174
175   return cursor->current->key;
176 }
177
178 /**
179  * @brief Get current data
180  * @param cursor the cursor
181  * @returns the current data
182  */
183 XBT_INLINE void *xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor)
184 {
185   __cursor_not_null(cursor);
186
187   return cursor->current->content;
188 }
189
190 /**
191  * @brief Set current data
192  * @param cursor the cursor
193  * @param data the new data
194  * @param free_ctn the function to free the new data
195  */
196 XBT_INLINE void xbt_dict_cursor_set_data(xbt_dict_cursor_t cursor,
197                                          void *data, void_f_pvoid_t free_ctn)
198 {
199   __cursor_not_null(cursor);
200   if(cursor->current->free_f)
201     cursor->current->free_f(cursor->current->content);
202   
203   cursor->current->content = data;
204   cursor->current->free_f = free_ctn;
205   return;
206 }
207
208
209