Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1110af751649b050b2f98346152ccaa99c5b7646
[simgrid.git] / src / xbt / dict_cursor.c
1 /* $Id$ */
2
3 /* dict_cursor - iterators over dictionnaries                               */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/misc.h"
11 #include "xbt/ex.h"
12 #include "dict_private.h"
13
14 #include <string.h> /* strlen() */
15
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict_cursor,xbt_dict,"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_dict_cursor_t xbt_dict_cursor_new(const xbt_dict_t dict) {
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 void xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
52   if (*cursor) {
53     xbt_free(*cursor);
54     *cursor = NULL;
55   }
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   xbt_assert0(cursor, "Null cursor");
63 }
64
65
66 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
67 void xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
68   CDEBUG0(xbt_dict_cursor, "xbt_dict_cursor_rewind");
69   xbt_assert(cursor);
70
71   cursor->line = 0;
72   if (cursor->dict != NULL) {
73     cursor->current = cursor->dict->table[0];
74   }
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 void xbt_dict_cursor_first(const xbt_dict_t   dict,
87                            xbt_dict_cursor_t *cursor){
88   DEBUG0("xbt_dict_cursor_first");
89   if (!*cursor) {
90     DEBUG0("Create the cursor on first use");
91     *cursor = xbt_dict_cursor_new(dict);
92   }
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 void xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
106   DEBUG0("xbt_dict_cursor_step");
107   xbt_assert(cursor);
108
109   xbt_dictelm_t current = cursor->current;
110   int line = cursor->line;
111
112   if (cursor->dict != NULL) {
113
114     if (current != NULL) {
115       DEBUG0("current is not null, take the next element");
116       current = current->next;
117       DEBUG1("next element: %p", current);
118     }
119     
120     while (current == NULL && ++line < cursor->dict->table_size) {
121       DEBUG0("current is NULL, take the next line");
122       current = cursor->dict->table[line];
123       DEBUG1("element in the next line: %p", current);
124     }
125     DEBUG2("search finished, current = %p, line = %d", current, line);
126     
127     cursor->current = current;
128     cursor->line = line;
129   }
130 }
131
132 /**
133  * @brief Get current data, or free the cursor if there is no data left
134  *
135  * @returns true if it's ok, false if there is no more data
136  */
137 int xbt_dict_cursor_get_or_free(xbt_dict_cursor_t  *cursor,
138                                 char               **key,
139                                 void               **data) {
140   DEBUG0("xbt_dict_get_or_free");
141
142   xbt_dictelm_t current;
143
144   if (!cursor || !(*cursor))
145     return FALSE;
146
147   current = (*cursor)->current;
148   if (current == NULL) { /* no data left */
149     xbt_dict_cursor_free(cursor);
150     return FALSE;
151   }
152   
153   *key = current->key;
154   *data = current->content;
155   return TRUE;
156 }
157
158 /**
159  * @brief Get current key
160  * @param cursor: the cursor
161  * @returns the current key
162  */
163 char *xbt_dict_cursor_get_key(xbt_dict_cursor_t   cursor) {
164   __cursor_not_null(cursor);
165
166   return cursor->current->key;
167 }
168
169 /**
170  * @brief Get current data
171  * @param cursor the cursor
172  * @returns the current data
173  */
174 void *xbt_dict_cursor_get_data(xbt_dict_cursor_t   cursor) {
175   __cursor_not_null(cursor);
176
177   return cursor->current->content;
178 }
179
180