Logo AND Algorithmique Numérique Distribuée

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