1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved. */
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. */
9 #include "dict_private.h"
10 #include "simgrid/Exception.hpp"
11 #include "src/xbt_modinter.h"
14 #include "xbt/mallocator.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities as hash tables");
24 * @param free_ctn function to call with (@a data as argument) when @a data is removed from the dictionary
25 * @return pointer to the destination
26 * @see xbt_dict_free()
28 * Creates and initialize a new dictionary with a default hashtable size.
29 * The dictionary is homogeneous: each element share the same free function.
31 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
33 if (dict_elm_mallocator == nullptr)
38 dict = xbt_new(s_xbt_dict_t, 1);
39 dict->free_f = free_ctn;
40 dict->table_size = 127;
41 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
50 * @param dict the dictionary to be freed
52 * Frees a dictionary with all the data
54 void xbt_dict_free(xbt_dict_t * dict)
56 if (dict != nullptr && *dict != nullptr) {
57 int table_size = (*dict)->table_size;
58 xbt_dictelm_t* table = (*dict)->table;
59 /* Warning: the size of the table is 'table_size+1'...
60 * This is because table_size is used as a binary mask in xbt_dict_rehash */
61 for (int i = 0; (*dict)->count && i <= table_size; i++) {
62 xbt_dictelm_t current = table[i];
63 xbt_dictelm_t previous;
65 while (current != nullptr) {
67 current = current->next;
68 xbt_dictelm_free(*dict, previous);
78 /** Returns the amount of elements in the dict */
79 unsigned int xbt_dict_size(xbt_dict_t dict)
81 return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
84 /* Expend the size of the dict */
85 static void xbt_dict_rehash(xbt_dict_t dict)
87 const unsigned oldsize = dict->table_size + 1;
88 unsigned newsize = oldsize * 2;
90 xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
91 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
93 dict->table_size = newsize;
94 dict->table = currcell;
95 XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
97 for (unsigned i = 0; i < oldsize; i++, currcell++) {
98 if (*currcell == nullptr) /* empty cell */
101 xbt_dictelm_t *twincell = currcell + oldsize;
102 xbt_dictelm_t *pprev = currcell;
103 xbt_dictelm_t bucklet = *currcell;
104 for (; bucklet != nullptr; bucklet = *pprev) {
105 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
106 - stay in cell i (ie, currcell)
107 - go to the cell i+oldsize (ie, twincell) */
108 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
109 *pprev = bucklet->next;
110 bucklet->next = *twincell;
111 if (*twincell == nullptr)
115 pprev = &bucklet->next;
119 if (*currcell == nullptr) /* everything moved */
125 * @brief Add data to the dict (arbitrary key)
126 * @param dict the container
127 * @param key the key to set the new data
128 * @param key_len the size of the @a key
129 * @param data the data to add in the dict
130 * @param free_ctn unused parameter (kept for compatibility)
132 * 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
135 void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data,
136 XBT_ATTRIB_UNUSED void_f_pvoid_t free_ctn)
138 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
140 xbt_dictelm_t current;
141 xbt_dictelm_t previous = nullptr;
143 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
144 dict->table_size, hash_code & dict->table_size);
145 current = dict->table[hash_code & dict->table_size];
146 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
147 || memcmp(key, current->key, key_len))) {
149 current = current->next;
152 if (current == nullptr) {
153 /* this key doesn't exist yet */
154 current = xbt_dictelm_new(key, key_len, hash_code, data);
156 if (previous == nullptr) {
157 dict->table[hash_code & dict->table_size] = current;
159 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
160 xbt_dict_rehash(dict);
162 previous->next = current;
165 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
166 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
167 /* there is already an element with the same key: overwrite it */
168 xbt_dictelm_set_data(dict, current, data);
173 * @brief Add data to the dict (null-terminated key)
175 * @param dict the dict
176 * @param key the key to set the new data
177 * @param data the data to add in the dict
178 * @param free_ctn unused parameter (kept for compatibility)
180 * set the @a data in the structure under the @a key, which is anull terminated string.
182 void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
184 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
188 * @brief Retrieve data from the dict (arbitrary key)
190 * @param dict the dealer of data
191 * @param key the key to find data
192 * @param key_len the size of the @a key
193 * @return the data that we are looking for
195 * Search the given @a key. Throws not_found_error when not found.
197 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
199 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
200 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
202 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
203 || memcmp(key, current->key, key_len))) {
204 current = current->next;
207 if (current == nullptr)
208 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
210 return current->content;
213 /** @brief like xbt_dict_get_ext(), but returning nullptr when not found */
214 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
216 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
217 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
219 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
220 || memcmp(key, current->key, key_len))) {
221 current = current->next;
224 if (current == nullptr)
227 return current->content;
231 * @brief retrieve the key associated to that object. Warning, that's a linear search
233 * Returns nullptr if the object cannot be found
235 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
237 for (int i = 0; i <= dict->table_size; i++) {
238 xbt_dictelm_t current = dict->table[i];
239 while (current != nullptr) {
240 if (current->content == data)
242 current = current->next;
249 * @brief Retrieve data from the dict (null-terminated key)
251 * @param dict the dealer of data
252 * @param key the key to find data
253 * @return the data that we are looking for
255 * Search the given @a key. Throws not_found_error when not found.
256 * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
258 void *xbt_dict_get(xbt_dict_t dict, const char *key)
260 return xbt_dict_get_elm(dict, key)->content;
264 * @brief Retrieve element from the dict (null-terminated key)
266 * @param dict the dealer of data
267 * @param key the key to find data
268 * @return the s_xbt_dictelm_t that we are looking for
270 * Search the given @a key. Throws not_found_error when not found.
271 * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
273 xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
275 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
277 if (current == nullptr)
278 THROWF(not_found_error, 0, "key %s not found", key);
284 * @brief like xbt_dict_get(), but returning nullptr when not found
286 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
288 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
290 if (current == nullptr)
293 return current->content;
297 * @brief like xbt_dict_get_elm(), but returning nullptr when not found
299 xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
301 unsigned int hash_code = xbt_str_hash(key);
302 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
304 while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
305 current = current->next;
310 * @brief Remove data from the dict (arbitrary key)
312 * @param dict the trash can
313 * @param key the key of the data to be removed
314 * @param key_len the size of the @a key
316 * Remove the entry associated with the given @a key (throws not_found)
318 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
320 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
321 xbt_dictelm_t previous = nullptr;
322 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
324 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
325 || strncmp(key, current->key, key_len))) {
326 previous = current; /* save the previous node */
327 current = current->next;
330 if (current == nullptr)
331 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
333 if (previous != nullptr) {
334 previous->next = current->next;
336 dict->table[hash_code & dict->table_size] = current->next;
340 if (not dict->table[hash_code & dict->table_size])
343 xbt_dictelm_free(dict, current);
348 * @brief Remove data from the dict (null-terminated key)
350 * @param dict the dict
351 * @param key the key of the data to be removed
353 * Remove the entry associated with the given @a key
355 void xbt_dict_remove(xbt_dict_t dict, const char *key)
357 xbt_dict_remove_ext(dict, key, strlen(key));
360 /** @brief Remove all data from the dict */
361 void xbt_dict_reset(xbt_dict_t dict)
363 if (dict->count == 0)
366 for (int i = 0; i <= dict->table_size; i++) {
367 xbt_dictelm_t previous = nullptr;
368 xbt_dictelm_t current = dict->table[i];
369 while (current != nullptr) {
371 current = current->next;
372 xbt_dictelm_free(dict, previous);
374 dict->table[i] = nullptr;
382 * @brief Return the number of elements in the dict.
383 * @param dict a dictionary
385 int xbt_dict_length(xbt_dict_t dict)
391 * @brief test if the dict is empty or not
393 int xbt_dict_is_empty(xbt_dict_t dict)
395 return not dict || (xbt_dict_length(dict) == 0);
399 * @brief Outputs the content of the structure (debugging purpose)
401 * @param dict the exibitionist
402 * @param output a function to dump each data in the tree
404 * Outputs the content of the structure. (for debugging purpose).
405 * @a output is a function to output the data. If nullptr, data won't be displayed.
407 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
409 xbt_dictelm_t element;
410 printf("Dict %p:\n", dict);
411 if (dict != nullptr) {
412 for (int i = 0; i < dict->table_size; i++) {
413 element = dict->table[i];
416 while (element != nullptr) {
417 printf(" %s -> '", element->key);
418 if (output != nullptr) {
419 output(element->content);
422 element = element->next;
433 * Create the dict mallocators.
434 * This is an internal XBT function called during the lib initialization.
435 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
437 void xbt_dict_preinit()
439 if (dict_elm_mallocator == nullptr)
440 dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
441 dict_elm_mallocator_reset_f);
445 * Destroy the dict mallocators.
446 * This is an internal XBT function during the lib initialization
448 void xbt_dict_postexit()
450 if (dict_elm_mallocator != nullptr) {
451 xbt_mallocator_free(dict_elm_mallocator);
452 dict_elm_mallocator = nullptr;