Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'wifi_clean' into 'master'
[simgrid.git] / src / xbt / dict.cpp
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2022. The SimGrid Team. 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/dict.h"
9 #include "dict_private.h"
10 #include "simgrid/Exception.hpp"
11 #include "xbt/ex.h"
12 #include "xbt/log.h"
13 #include "xbt/mallocator.h"
14 #include "xbt/str.h"
15 #include "xbt/string.hpp"
16 #include "xbt/xbt_modinter.h"
17
18 #include <algorithm>
19 #include <cstdio>
20 #include <cstring>
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities as hash tables");
23
24 constexpr int MAX_FILL_PERCENT = 80;
25
26 /**
27  * @brief Constructor
28  * @param free_ctn function to call with (@a data as argument) when @a data is removed from the dictionary
29  * @return pointer to the destination
30  * @see xbt_dict_free()
31  *
32  * Creates and initialize a new dictionary with a default hashtable size.
33  * The dictionary is homogeneous: each element share the same free function.
34  */
35 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
36 {
37   xbt_dict_preinit();
38
39   xbt_dict_t dict;
40
41   dict = xbt_new(s_xbt_dict_t, 1);
42   dict->free_f = free_ctn;
43   dict->table_size = 127;
44   dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
45   dict->count = 0;
46   dict->fill = 0;
47
48   return dict;
49 }
50
51 /**
52  * @brief Destructor
53  * @param dict the dictionary to be freed
54  *
55  * Frees a dictionary with all the data
56  */
57 void xbt_dict_free(xbt_dict_t * dict)
58 {
59   if (dict != nullptr && *dict != nullptr) {
60     int table_size       = (*dict)->table_size;
61     xbt_dictelm_t* table = (*dict)->table;
62     /* Warning: the size of the table is 'table_size+1'...
63      * This is because table_size is used as a binary mask in xbt_dict_rehash */
64     for (int i = 0; (*dict)->count && i <= table_size; i++) {
65       xbt_dictelm_t current = table[i];
66       xbt_dictelm_t previous;
67
68       while (current != nullptr) {
69         previous = current;
70         current = current->next;
71         xbt_dictelm_free(*dict, previous);
72         (*dict)->count--;
73       }
74     }
75     xbt_free(table);
76     xbt_free(*dict);
77     *dict = nullptr;
78   }
79 }
80
81 /** Returns the amount of elements in the dict */
82 unsigned int xbt_dict_size(const_xbt_dict_t dict)
83 {
84   return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
85 }
86
87 /* Expend the size of the dict */
88 static void xbt_dict_rehash(xbt_dict_t dict)
89 {
90   const unsigned oldsize = dict->table_size + 1;
91   unsigned newsize = oldsize * 2;
92
93   auto* newtable = static_cast<xbt_dictelm_t*>(xbt_realloc(dict->table, newsize * sizeof(xbt_dictelm_t)));
94   std::fill(newtable + oldsize, newtable + newsize, nullptr); /* zero second half */
95   newsize--;
96   dict->table_size = newsize;
97   dict->table      = newtable;
98   XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
99
100   for (unsigned i = 0; i < oldsize; i++) {
101     xbt_dictelm_t* currcell = &newtable[i];
102     if (*currcell == nullptr) /* empty cell */
103       continue;
104
105     xbt_dictelm_t *twincell = currcell + oldsize;
106     xbt_dictelm_t *pprev = currcell;
107     xbt_dictelm_t bucklet = *currcell;
108     while (bucklet != nullptr) {
109       /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
110          - stay  in  cell i (ie, currcell)
111          - go to the cell i+oldsize (ie, twincell) */
112       if ((bucklet->hash_code & newsize) != i) {        /* Move to b */
113         *pprev = bucklet->next;
114         bucklet->next = *twincell;
115         if (*twincell == nullptr)
116           dict->fill++;
117         *twincell = bucklet;
118       } else {
119         pprev = &bucklet->next;
120       }
121       bucklet = *pprev;
122     }
123
124     if (*currcell == nullptr) /* everything moved */
125       dict->fill--;
126   }
127 }
128
129 /**
130  * @brief Add data to the dict (arbitrary key)
131  * @param dict the container
132  * @param key the key to set the new data
133  * @param key_len the size of the @a key
134  * @param data the data to add in the dict
135  *
136  * Set the @a data in the structure under the @a key, which can be any kind of data, as long as its length is provided
137  * in @a key_len.
138  */
139 void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data)
140 {
141   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
142
143   xbt_dictelm_t current;
144   xbt_dictelm_t previous = nullptr;
145
146   XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
147              dict->table_size, hash_code & dict->table_size);
148   current = dict->table[hash_code & dict->table_size];
149   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
150           || memcmp(key, current->key, key_len))) {
151     previous = current;
152     current = current->next;
153   }
154
155   if (current == nullptr) {
156     /* this key doesn't exist yet */
157     current = xbt_dictelm_new(key, key_len, hash_code, data);
158     dict->count++;
159     if (previous == nullptr) {
160       dict->table[hash_code & dict->table_size] = current;
161       dict->fill++;
162       if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
163         xbt_dict_rehash(dict);
164     } else {
165       previous->next = current;
166     }
167   } else {
168     XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
169                key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
170     /* there is already an element with the same key: overwrite it */
171     xbt_dictelm_set_data(dict, current, data);
172   }
173 }
174
175 /**
176  * @brief Add data to the dict (null-terminated key)
177  *
178  * @param dict the dict
179  * @param key the key to set the new data
180  * @param data the data to add in the dict
181  *
182  * set the @a data in the structure under the @a key, which is a null terminated string.
183  */
184 void xbt_dict_set(xbt_dict_t dict, const char* key, void* data)
185 {
186   xbt_dict_set_ext(dict, key, strlen(key), data);
187 }
188
189 /**
190  * @brief Retrieve data from the dict (arbitrary key)
191  *
192  * @param dict the dealer of data
193  * @param key the key to find data
194  * @param key_len the size of the @a key
195  * @return the data that we are looking for
196  *
197  * Search the given @a key. Returns nullptr when not found.
198  */
199 void* xbt_dict_get_or_null_ext(const_xbt_dict_t dict, const char* key, int key_len)
200 {
201   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
202   const s_xbt_dictelm* current = dict->table[hash_code & dict->table_size];
203
204   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
205           || memcmp(key, current->key, key_len))) {
206     current = current->next;
207   }
208
209   if (current == nullptr)
210     return nullptr;
211
212   return current->content;
213 }
214
215 /**
216  * @brief Retrieve data from the dict (null-terminated key)
217  *
218  * @param dict the dealer of data
219  * @param key the key to find data
220  * @return the data that we are looking for
221  *
222  * Search the given @a key. Returns nullptr when not found.
223  */
224 void* xbt_dict_get_or_null(const_xbt_dict_t dict, const char* key)
225 {
226   const s_xbt_dictelm* current = xbt_dict_get_elm_or_null(dict, key);
227
228   if (current == nullptr)
229     return nullptr;
230
231   return current->content;
232 }
233
234 /**
235  * @brief Retrieve element from the dict (null-terminated key)
236  *
237  * @param dict the dealer of data
238  * @param key the key to find data
239  * @return the s_xbt_dictelm_t that we are looking for
240  *
241  * Search the given @a key. Returns nullptr when not found.
242  */
243 xbt_dictelm_t xbt_dict_get_elm_or_null(const_xbt_dict_t dict, const char* key)
244 {
245   unsigned int hash_code = xbt_str_hash(key);
246   xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
247
248   while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
249     current = current->next;
250   return current;
251 }
252
253 /**
254  * @brief Remove data from the dict (arbitrary key)
255  *
256  * @param dict the trash can
257  * @param key the key of the data to be removed
258  * @param key_len the size of the @a key
259  *
260  * Remove the entry associated with the given @a key (throws std::out_of_range)
261  */
262 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
263 {
264   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
265   xbt_dictelm_t previous = nullptr;
266   xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
267
268   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
269           || strncmp(key, current->key, key_len))) {
270     previous = current;         /* save the previous node */
271     current = current->next;
272   }
273
274   if (current == nullptr)
275     throw std::out_of_range(simgrid::xbt::string_printf("key %.*s not found", key_len, key));
276   else {
277     if (previous != nullptr) {
278       previous->next = current->next;
279     } else {
280       dict->table[hash_code & dict->table_size] = current->next;
281     }
282   }
283
284   if (not dict->table[hash_code & dict->table_size])
285     dict->fill--;
286
287   xbt_dictelm_free(dict, current);
288   dict->count--;
289 }
290
291 /**
292  * @brief Return the number of elements in the dict.
293  * @param dict a dictionary
294  */
295 int xbt_dict_length(const_xbt_dict_t dict)
296 {
297   return dict->count;
298 }
299
300 /**
301  * @brief test if the dict is empty or not
302  */
303 int xbt_dict_is_empty(const_xbt_dict_t dict)
304 {
305   return not dict || (xbt_dict_length(dict) == 0);
306 }
307
308 /**
309  * Create the dict mallocators.
310  * This is an internal XBT function called during the lib initialization.
311  * It can be used several times to recreate the mallocator, for example when you switch to MC mode
312  */
313 void xbt_dict_preinit()
314 {
315   if (dict_elm_mallocator == nullptr)
316     dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
317       dict_elm_mallocator_reset_f);
318 }
319
320 /**
321  * Destroy the dict mallocators.
322  * This is an internal XBT function during the lib initialization
323  */
324 void xbt_dict_postexit()
325 {
326   if (dict_elm_mallocator != nullptr) {
327     xbt_mallocator_free(dict_elm_mallocator);
328     dict_elm_mallocator = nullptr;
329   }
330 }