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)
36 if (dict_elm_mallocator == nullptr)
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);
53 * @param dict the dictionary to be freed
55 * Frees a dictionary with all the data
57 void xbt_dict_free(xbt_dict_t * dict)
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;
68 while (current != nullptr) {
70 current = current->next;
71 xbt_dictelm_free(*dict, previous);
81 /** Returns the amount of elements in the dict */
82 unsigned int xbt_dict_size(const_xbt_dict_t dict)
84 return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
87 /* Expend the size of the dict */
88 static void xbt_dict_rehash(xbt_dict_t dict)
90 const unsigned oldsize = dict->table_size + 1;
91 unsigned newsize = oldsize * 2;
93 xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
94 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
96 dict->table_size = newsize;
97 dict->table = currcell;
98 XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
100 for (unsigned i = 0; i < oldsize; i++, currcell++) {
101 if (*currcell == nullptr) /* empty cell */
104 xbt_dictelm_t *twincell = currcell + oldsize;
105 xbt_dictelm_t *pprev = currcell;
106 xbt_dictelm_t bucklet = *currcell;
107 for (; bucklet != nullptr; bucklet = *pprev) {
108 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
109 - stay in cell i (ie, currcell)
110 - go to the cell i+oldsize (ie, twincell) */
111 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
112 *pprev = bucklet->next;
113 bucklet->next = *twincell;
114 if (*twincell == nullptr)
118 pprev = &bucklet->next;
122 if (*currcell == nullptr) /* everything moved */
128 * @brief Add data to the dict (arbitrary key)
129 * @param dict the container
130 * @param key the key to set the new data
131 * @param key_len the size of the @a key
132 * @param data the data to add in the dict
134 * 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 void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data)
139 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
141 xbt_dictelm_t current;
142 xbt_dictelm_t previous = nullptr;
144 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
145 dict->table_size, hash_code & dict->table_size);
146 current = dict->table[hash_code & dict->table_size];
147 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
148 || memcmp(key, current->key, key_len))) {
150 current = current->next;
153 if (current == nullptr) {
154 /* this key doesn't exist yet */
155 current = xbt_dictelm_new(key, key_len, hash_code, data);
157 if (previous == nullptr) {
158 dict->table[hash_code & dict->table_size] = current;
160 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
161 xbt_dict_rehash(dict);
163 previous->next = current;
166 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
167 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
168 /* there is already an element with the same key: overwrite it */
169 xbt_dictelm_set_data(dict, current, data);
174 * @brief Add data to the dict (null-terminated key)
176 * @param dict the dict
177 * @param key the key to set the new data
178 * @param data the data to add in the dict
180 * set the @a data in the structure under the @a key, which is a null terminated string.
182 void xbt_dict_set(xbt_dict_t dict, const char* key, void* data)
184 xbt_dict_set_ext(dict, key, strlen(key), data);
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. Returns nullptr when not found.
197 void* xbt_dict_get_or_null_ext(const_xbt_dict_t dict, const char* key, int key_len)
199 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
200 const s_xbt_dictelm* 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)
210 return current->content;
214 * @brief Retrieve data from the dict (null-terminated key)
216 * @param dict the dealer of data
217 * @param key the key to find data
218 * @return the data that we are looking for
220 * Search the given @a key. Returns nullptr when not found.
222 void* xbt_dict_get_or_null(const_xbt_dict_t dict, const char* key)
224 const s_xbt_dictelm* current = xbt_dict_get_elm_or_null(dict, key);
226 if (current == nullptr)
229 return current->content;
233 * @brief Retrieve element from the dict (null-terminated key)
235 * @param dict the dealer of data
236 * @param key the key to find data
237 * @return the s_xbt_dictelm_t that we are looking for
239 * Search the given @a key. Returns nullptr when not found.
241 xbt_dictelm_t xbt_dict_get_elm_or_null(const_xbt_dict_t dict, const char* key)
243 unsigned int hash_code = xbt_str_hash(key);
244 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
246 while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
247 current = current->next;
252 * @brief Remove data from the dict (arbitrary key)
254 * @param dict the trash can
255 * @param key the key of the data to be removed
256 * @param key_len the size of the @a key
258 * Remove the entry associated with the given @a key (throws std::out_of_range)
260 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
262 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
263 xbt_dictelm_t previous = nullptr;
264 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
266 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
267 || strncmp(key, current->key, key_len))) {
268 previous = current; /* save the previous node */
269 current = current->next;
272 if (current == nullptr)
273 throw std::out_of_range(simgrid::xbt::string_printf("key %.*s not found", key_len, key));
275 if (previous != nullptr) {
276 previous->next = current->next;
278 dict->table[hash_code & dict->table_size] = current->next;
282 if (not dict->table[hash_code & dict->table_size])
285 xbt_dictelm_free(dict, current);
290 * @brief Return the number of elements in the dict.
291 * @param dict a dictionary
293 int xbt_dict_length(const_xbt_dict_t dict)
299 * @brief test if the dict is empty or not
301 int xbt_dict_is_empty(const_xbt_dict_t dict)
303 return not dict || (xbt_dict_length(dict) == 0);
307 * Create the dict mallocators.
308 * This is an internal XBT function called during the lib initialization.
309 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
311 void xbt_dict_preinit()
313 if (dict_elm_mallocator == nullptr)
314 dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
315 dict_elm_mallocator_reset_f);
319 * Destroy the dict mallocators.
320 * This is an internal XBT function during the lib initialization
322 void xbt_dict_postexit()
324 if (dict_elm_mallocator != nullptr) {
325 xbt_mallocator_free(dict_elm_mallocator);
326 dict_elm_mallocator = nullptr;