/* dict - a generic dictionary, variation over hash table */
-/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2004-2011. The SimGrid Team.
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
"Dictionaries provide the same functionalities than hash tables");
-/*####[ Private prototypes ]#################################################*/
-
-static xbt_mallocator_t dict_mallocator = NULL;
-static void *dict_mallocator_new_f(void);
-static void dict_mallocator_free_f(void *dict);
-static void dict_mallocator_reset_f(void *dict);
-
-
-/*####[ Code ]###############################################################*/
/**
* \brief Constructor
* \return pointer to the destination
- * \see xbt_dict_new_ext(), xbt_dict_free()
+ * \see xbt_dict_free()
*
* Creates and initialize a new dictionary with a default hashtable size.
*/
{
xbt_dict_t dict;
- dict = xbt_mallocator_get(dict_mallocator);
+ dict = xbt_new(s_xbt_dict_t, 1);
dict->table_size = 127;
dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
dict->count = 0;
}
}
xbt_free(table);
- xbt_mallocator_release(dict_mallocator, *dict);
+ xbt_free(*dict);
*dict = NULL;
}
}
XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
uintptr_t key, uintptr_t data)
{
-
- unsigned int hash_code =
- xbt_dict_hash_ext((void *) &key, sizeof(uintptr_t));
-
- xbt_dictelm_t current, previous = NULL;
- xbt_assert(dict);
-
- XBT_DEBUG("ADD %zu->%zu; hash = %d, size = %d, & = %d", key, data,
- hash_code, dict->table_size, hash_code & dict->table_size);
- current = dict->table[hash_code & dict->table_size];
- while (current != NULL &&
- (hash_code != current->hash_code
- || sizeof(uintptr_t) != current->key_len
- || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
- previous = current;
- current = current->next;
- }
-
- if (current == NULL) {
- /* this key doesn't exist yet */
- current = xbt_dictielm_new(key, hash_code, data);
- dict->count++;
- if (previous == NULL) {
- dict->table[hash_code & dict->table_size] = current;
- dict->fill++;
- if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
- xbt_dict_rehash(dict);
- } else {
- previous->next = current;
- }
- } else {
-
- /* there is already an element with the same key: overwrite it */
- if (current->content != NULL && current->free_f != NULL) {
- current->free_f(current->content);
- }
- current->content = (void *) data;
- current->free_f = NULL;
- }
+ xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
}
/**
*/
XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
{
-
- unsigned int hash_code =
- xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t));
- xbt_dictelm_t current;
-
- xbt_assert(dict);
-
- current = dict->table[hash_code & dict->table_size];
- while (current != NULL &&
- (hash_code != current->hash_code
- || sizeof(uintptr_t) != current->key_len
- || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
- current = current->next;
- }
-
- if (current == NULL)
- return 0;
-
- return (uintptr_t) (current->content);
+ return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
}
/** Remove a uintptr_t key from the dict */
XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
{
-
- unsigned int hash_code =
- xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t));
- xbt_dictelm_t current, previous = NULL;
-
-
- current = dict->table[hash_code & dict->table_size];
- while (current != NULL &&
- (hash_code != current->hash_code
- || sizeof(uintptr_t) != current->key_len
- || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
- previous = current; /* save the previous node */
- current = current->next;
- }
-
- if (current == NULL)
- THROWF(not_found_error, 0, "key %zu not found", key);
-
- if (previous != NULL) {
- previous->next = current->next;
- } else {
- dict->table[hash_code & dict->table_size] = current->next;
- }
-
- if (!dict->table[hash_code & dict->table_size])
- dict->fill--;
-
- xbt_dictelm_free(current);
- dict->count--;
+ xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
}
*/
XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
{
- return (xbt_dict_size(dict) == 0);
+ return !dict || (xbt_dict_length(dict) == 0);
}
/**
while (element != NULL) {
printf(" %s -> '", element->key);
if (output != NULL) {
- (*output) (element->content);
+ output(element->content);
}
printf("'\n");
element = element->next;
*/
void xbt_dict_preinit(void)
{
- if (dict_mallocator != NULL) {
+ if (dict_elm_mallocator != NULL) {
/* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
- xbt_mallocator_free(dict_mallocator);
xbt_mallocator_free(dict_elm_mallocator);
}
- dict_mallocator = xbt_mallocator_new(256,
- dict_mallocator_new_f,
- dict_mallocator_free_f,
- dict_mallocator_reset_f);
dict_elm_mallocator = xbt_mallocator_new(256,
dict_elm_mallocator_new_f,
dict_elm_mallocator_free_f,
*/
void xbt_dict_postexit(void)
{
- if (dict_mallocator != NULL) {
- xbt_mallocator_free(dict_mallocator);
- dict_mallocator = NULL;
+ if (dict_elm_mallocator != NULL) {
xbt_mallocator_free(dict_elm_mallocator);
dict_elm_mallocator = NULL;
}
}
}
-static void *dict_mallocator_new_f(void)
-{
- return xbt_new(s_xbt_dict_t, 1);
-}
-
-static void dict_mallocator_free_f(void *dict)
-{
- xbt_free(dict);
-}
-
-static void dict_mallocator_reset_f(void *dict)
-{
- /* nothing to do because all fields are
- * initialized in xbt_dict_new
- */
-}
-
#ifdef SIMGRID_TEST
#include "xbt.h"
#include "xbt/ex.h"
data = xbt_dict_get(head, data);
THROWF(unknown_error, 0,
"Found something which shouldn't be there (%s)", data);
- } CATCH(e) {
+ }
+ CATCH(e) {
if (e.category != not_found_error)
xbt_test_exception(e);
xbt_ex_free(e);
static void count_check_get_key(xbt_dict_t dict, int length)
{
xbt_dict_cursor_t cursor;
- char *key, *key2;
+ char *key;
+ _XBT_GNUC_UNUSED char *key2;
void *data;
int effective = 0;
traverse(head);
TRY {
debuged_remove(head, "12346");
- } CATCH(e) {
+ }
+ CATCH(e) {
if (e.category != not_found_error)
xbt_test_exception(e);
xbt_ex_free(e);