X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/2860c34b0b52fc10d0075a0778fd0ddcca68065e..6d1fc1c31cb2152b6d20742081118524dbb78d14:/src/xbt/dict.cpp diff --git a/src/xbt/dict.cpp b/src/xbt/dict.cpp index c6827205e4..9d5dc32464 100644 --- a/src/xbt/dict.cpp +++ b/src/xbt/dict.cpp @@ -1,12 +1,11 @@ /* dict - a generic dictionary, variation over hash table */ -/* Copyright (c) 2004-2015. The SimGrid Team. +/* Copyright (c) 2004-2017. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ - #include #include @@ -73,8 +72,8 @@ xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn) */ void xbt_dict_free(xbt_dict_t * dict) { - int i; - xbt_dictelm_t current, previous; + xbt_dictelm_t current; + xbt_dictelm_t previous; int table_size; xbt_dictelm_t *table; @@ -85,7 +84,7 @@ void xbt_dict_free(xbt_dict_t * dict) table = (*dict)->table; /* Warning: the size of the table is 'table_size+1'... * This is because table_size is used as a binary mask in xbt_dict_rehash */ - for (i = 0; (*dict)->count && i <= table_size; i++) { + for (int i = 0; (*dict)->count && i <= table_size; i++) { current = table[i]; while (current != nullptr) { previous = current; @@ -103,7 +102,7 @@ void xbt_dict_free(xbt_dict_t * dict) /** Returns the amount of elements in the dict */ unsigned int xbt_dict_size(xbt_dict_t dict) { - return (dict ? (unsigned int) dict->count : (unsigned int) 0); + return (dict != nullptr ? static_cast(dict->count) : static_cast(0)); } /* Expend the size of the dict */ @@ -114,7 +113,8 @@ static void xbt_dict_rehash(xbt_dict_t dict) xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t)); memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */ - dict->table_size = --newsize; + newsize--; + dict->table_size = newsize; dict->table = currcell; XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize); @@ -135,7 +135,6 @@ static void xbt_dict_rehash(xbt_dict_t dict) if (!*twincell) dict->fill++; *twincell = bucklet; - continue; } else { pprev = &bucklet->next; } @@ -162,8 +161,10 @@ void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, { unsigned int hash_code = xbt_str_hash_ext(key, key_len); - xbt_dictelm_t current, previous = nullptr; + xbt_dictelm_t current; + xbt_dictelm_t previous = nullptr; + xbt_assert(!free_ctn, "Cannot set an individual free function in homogeneous dicts."); XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code, dict->table_size, hash_code & dict->table_size); current = dict->table[hash_code & dict->table_size]; @@ -175,7 +176,7 @@ void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, if (current == nullptr) { /* this key doesn't exist yet */ - current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn); + current = xbt_dictelm_new(key, key_len, hash_code, data); dict->count++; if (previous == nullptr) { dict->table[hash_code & dict->table_size] = current; @@ -360,11 +361,12 @@ void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) if (current == nullptr) THROWF(not_found_error, 0, "key %.*s not found", key_len, key); - - if (previous != nullptr) { - previous->next = current->next; - } else { - dict->table[hash_code & dict->table_size] = current->next; + else { + if (previous != nullptr) { + previous->next = current->next; + } else { + dict->table[hash_code & dict->table_size] = current->next; + } } if (!dict->table[hash_code & dict->table_size]) @@ -516,11 +518,8 @@ void xbt_dict_dump_sizes(xbt_dict_t dict) } /* Report current sizes */ - if (count == 0) - continue; - if (size == 0) - continue; - printf("%uelm x %u cells; ", count, size); + if (count != 0 && size != 0) + printf("%uelm x %u cells; ", count, size); } printf("\n"); xbt_dynar_free(&sizes); @@ -536,9 +535,6 @@ void xbt_dict_preinit() if (dict_elm_mallocator == nullptr) dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f, dict_elm_mallocator_reset_f); - if (dict_het_elm_mallocator == nullptr) - dict_het_elm_mallocator = xbt_mallocator_new(256, dict_het_elm_mallocator_new_f, dict_het_elm_mallocator_free_f, - dict_het_elm_mallocator_reset_f); } /** @@ -550,8 +546,6 @@ void xbt_dict_postexit() if (dict_elm_mallocator != nullptr) { xbt_mallocator_free(dict_elm_mallocator); dict_elm_mallocator = nullptr; - xbt_mallocator_free(dict_het_elm_mallocator); - dict_het_elm_mallocator = nullptr; } if (all_sizes) { unsigned int count; @@ -560,15 +554,16 @@ void xbt_dict_postexit() int total_count = 0; printf("Overall stats:"); xbt_dynar_foreach(all_sizes, count, size) { - if (count == 0) - continue; - if (size == 0) - continue; - printf("%uelm x %d cells; ", count, size); - avg += count * size; - total_count += size; + if (count != 0 && size != 0) { + printf("%uelm x %d cells; ", count, size); + avg += count * size; + total_count += size; + } } - printf("; %f elm per cell\n", avg / (double) total_count); + if (total_count > 0) + printf("; %f elm per cell\n", avg / (double)total_count); + else + printf("; 0 elm per cell\n"); } }