1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2020. 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"
16 #include "xbt/string.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities as hash tables");
23 constexpr int MAX_FILL_PERCENT = 80;
27 * @param free_ctn function to call with (@a data as argument) when @a data is removed from the dictionary
28 * @return pointer to the destination
29 * @see xbt_dict_free()
31 * Creates and initialize a new dictionary with a default hashtable size.
32 * The dictionary is homogeneous: each element share the same free function.
34 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
40 dict = xbt_new(s_xbt_dict_t, 1);
41 dict->free_f = free_ctn;
42 dict->table_size = 127;
43 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
52 * @param dict the dictionary to be freed
54 * Frees a dictionary with all the data
56 void xbt_dict_free(xbt_dict_t * dict)
58 if (dict != nullptr && *dict != nullptr) {
59 int table_size = (*dict)->table_size;
60 xbt_dictelm_t* table = (*dict)->table;
61 /* Warning: the size of the table is 'table_size+1'...
62 * This is because table_size is used as a binary mask in xbt_dict_rehash */
63 for (int i = 0; (*dict)->count && i <= table_size; i++) {
64 xbt_dictelm_t current = table[i];
65 xbt_dictelm_t previous;
67 while (current != nullptr) {
69 current = current->next;
70 xbt_dictelm_free(*dict, previous);
80 /** Returns the amount of elements in the dict */
81 unsigned int xbt_dict_size(const_xbt_dict_t dict)
83 return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
86 /* Expend the size of the dict */
87 static void xbt_dict_rehash(xbt_dict_t dict)
89 const unsigned oldsize = dict->table_size + 1;
90 unsigned newsize = oldsize * 2;
92 xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
93 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
95 dict->table_size = newsize;
96 dict->table = currcell;
97 XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
99 for (unsigned i = 0; i < oldsize; i++, currcell++) {
100 if (*currcell == nullptr) /* empty cell */
103 xbt_dictelm_t *twincell = currcell + oldsize;
104 xbt_dictelm_t *pprev = currcell;
105 xbt_dictelm_t bucklet = *currcell;
106 for (; bucklet != nullptr; bucklet = *pprev) {
107 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
108 - stay in cell i (ie, currcell)
109 - go to the cell i+oldsize (ie, twincell) */
110 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
111 *pprev = bucklet->next;
112 bucklet->next = *twincell;
113 if (*twincell == nullptr)
117 pprev = &bucklet->next;
121 if (*currcell == nullptr) /* everything moved */
127 * @brief Add data to the dict (arbitrary key)
128 * @param dict the container
129 * @param key the key to set the new data
130 * @param key_len the size of the @a key
131 * @param data the data to add in the dict
133 * 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
136 void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data)
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
179 * set the @a data in the structure under the @a key, which is a null terminated string.
181 void xbt_dict_set(xbt_dict_t dict, const char* key, void* data)
183 xbt_dict_set_ext(dict, key, strlen(key), data);
187 * @brief Retrieve data from the dict (arbitrary key)
189 * @param dict the dealer of data
190 * @param key the key to find data
191 * @param key_len the size of the @a key
192 * @return the data that we are looking for
194 * Search the given @a key. Returns nullptr when not found.
196 void* xbt_dict_get_or_null_ext(const_xbt_dict_t dict, const char* key, int key_len)
198 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
199 const s_xbt_dictelm* current = dict->table[hash_code & dict->table_size];
201 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
202 || memcmp(key, current->key, key_len))) {
203 current = current->next;
206 if (current == nullptr)
209 return current->content;
213 * @brief Retrieve data from the dict (null-terminated key)
215 * @param dict the dealer of data
216 * @param key the key to find data
217 * @return the data that we are looking for
219 * Search the given @a key. Returns nullptr when not found.
221 void* xbt_dict_get_or_null(const_xbt_dict_t dict, const char* key)
223 const s_xbt_dictelm* current = xbt_dict_get_elm_or_null(dict, key);
225 if (current == nullptr)
228 return current->content;
232 * @brief Retrieve element from the dict (null-terminated key)
234 * @param dict the dealer of data
235 * @param key the key to find data
236 * @return the s_xbt_dictelm_t that we are looking for
238 * Search the given @a key. Returns nullptr when not found.
240 xbt_dictelm_t xbt_dict_get_elm_or_null(const_xbt_dict_t dict, const char* key)
242 unsigned int hash_code = xbt_str_hash(key);
243 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
245 while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
246 current = current->next;
251 * @brief Remove data from the dict (arbitrary key)
253 * @param dict the trash can
254 * @param key the key of the data to be removed
255 * @param key_len the size of the @a key
257 * Remove the entry associated with the given @a key (throws std::out_of_range)
259 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
261 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
262 xbt_dictelm_t previous = nullptr;
263 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
265 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
266 || strncmp(key, current->key, key_len))) {
267 previous = current; /* save the previous node */
268 current = current->next;
271 if (current == nullptr)
272 throw std::out_of_range(simgrid::xbt::string_printf("key %.*s not found", key_len, key));
274 if (previous != nullptr) {
275 previous->next = current->next;
277 dict->table[hash_code & dict->table_size] = current->next;
281 if (not dict->table[hash_code & dict->table_size])
284 xbt_dictelm_free(dict, current);
289 * @brief Return the number of elements in the dict.
290 * @param dict a dictionary
292 int xbt_dict_length(const_xbt_dict_t dict)
298 * @brief test if the dict is empty or not
300 int xbt_dict_is_empty(const_xbt_dict_t dict)
302 return not dict || (xbt_dict_length(dict) == 0);
306 * Create the dict mallocators.
307 * This is an internal XBT function called during the lib initialization.
308 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
310 void xbt_dict_preinit()
312 if (dict_elm_mallocator == nullptr)
313 dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
314 dict_elm_mallocator_reset_f);
318 * Destroy the dict mallocators.
319 * This is an internal XBT function during the lib initialization
321 void xbt_dict_postexit()
323 if (dict_elm_mallocator != nullptr) {
324 xbt_mallocator_free(dict_elm_mallocator);
325 dict_elm_mallocator = nullptr;