X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/aa7c707291dd1533fcac940f565b42188d558798..b85cfb5c5c896217c19efbd161886ce5b52ec090:/src/xbt/dict.c diff --git a/src/xbt/dict.c b/src/xbt/dict.c index 6602cc0023..1327951c25 100644 --- a/src/xbt/dict.c +++ b/src/xbt/dict.c @@ -2,59 +2,43 @@ /* dict - a generic dictionnary, variation over the B-tree concept */ -/* Authors: Martin Quinson */ -/* Copyright (C) 2003 the OURAGAN project. */ +/* Copyright (c) 2003,2004 Martin Quinson. 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. */ + * under the terms of the license (GNU LGPL) which comes with this package. */ - -#include "gras_private.h" +#include "xbt/ex.h" #include "dict_private.h" -#include /* malloc() */ -#include /* strlen() */ - -#include - -GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict,GRAS); - +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt, + "Dictionaries provide the same functionnalities than hash tables"); /*####[ Private prototypes ]#################################################*/ - /*####[ Code ]###############################################################*/ /** - * gras_dict_new: - * - * @whereto: pointer to the destination + * @brief Constructor + * @return pointer to the destination * * Creates and initialize a new dictionnary */ -gras_error_t -gras_dict_new(gras_dict_t **whereto) { - gras_dict_t *dict; - - if (!(dict = calloc(1, sizeof(gras_dict_t)))) - RAISE_MALLOC; - - dict->head=NULL; - - *whereto = dict; - - return no_error; +xbt_dict_t +xbt_dict_new(void) { + xbt_dict_t res= xbt_new(s_xbt_dict_t,1); + res->head=NULL; + return res; } /** - * gras_dict_free: - * @dict: the dictionnary to be freed + * @brief Destructor + * @param dict the dictionnary to be freed * * Frees a cache structure with all its childs. */ void -gras_dict_free(gras_dict_t **dict) { +xbt_dict_free(xbt_dict_t *dict) { if (dict && *dict) { if ((*dict)->head) { - gras_dictelm_free( &( (*dict)->head ) ); + xbt_dictelm_free( &( (*dict)->head ) ); (*dict)->head = NULL; } free(*dict); @@ -63,145 +47,172 @@ gras_dict_free(gras_dict_t **dict) { } /** - * gras_dict_insert_ext: - * - * @p_dict: the container - * @key: the key to insert the new data - * @data: the data to add in the dict - * @Returns: a gras_error - * - * Insert the @data in the structure under the @key, which can be any kind - * of data, as long as its length is provided in @key_len. + * \brief Add data to the dict (arbitrary key) + * \param dict the container + * \param key the key to set the new data + * \param key_len the size of the \a key + * \param data the data to add in the dict + * \param free_ctn function to call with (\a key as argument) when + * \a key is removed from the dictionnary + * + * 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 in \a key_len. */ -gras_error_t -gras_dict_insert_ext(gras_dict_t *p_dict, - const char *key, - int key_len, - void *data, - void_f_pvoid_t *free_ctn) { +void +xbt_dict_set_ext(xbt_dict_t dict, + const char *key, + int key_len, + void *data, + void_f_pvoid_t *free_ctn) { - gras_assert(p_dict); + xbt_assert(dict); - return gras_dictelm_insert_ext(&(p_dict->head), - key, key_len, data, free_ctn); + xbt_dictelm_set_ext(&(dict->head), + key, key_len, data, free_ctn); } /** - * gras_dict_insert: + * \brief Add data to the dict (null-terminated key) * - * @head: the head of the dict - * @key: the key to insert the new data - * @data: the data to add in the dict - * @Returns: a gras_error + * \param dict the head of the dict + * \param key the key to set the new data + * \param data the data to add in the dict + * \param free_ctn function to call with (\a key as argument) when + * \a key is removed from the dictionnary * - * Insert the @data in the structure under the @key, which is a + * set the \a data in the structure under the \a key, which is a * null terminated string. */ -gras_error_t -gras_dict_insert(gras_dict_t *p_dict, - const char *key, - void *data, - void_f_pvoid_t *free_ctn) { +void +xbt_dict_set(xbt_dict_t dict, + const char *key, + void *data, + void_f_pvoid_t *free_ctn) { - gras_assert(p_dict); + xbt_assert(dict); - return gras_dictelm_insert(&(p_dict->head), key, data, free_ctn); + xbt_dictelm_set(&(dict->head), key, data, free_ctn); } /** - * gras_dict_retrieve_ext: + * \brief Retrieve data from the dict (arbitrary key) * - * @dict: the dealer of data - * @key: the key to find data - * @data: the data that we are looking for - * @Returns: gras_error + * \param dict the dealer of data + * \param key the key to find data + * \param key_len the size of the \a key + * \returns the data that we are looking for * - * Search the given @key. mismatch_error when not found. + * Search the given \a key. throws not_found_error when not found. */ -gras_error_t -gras_dict_retrieve_ext(gras_dict_t *dict, - const char *key, - int key_len, - /* OUT */void **data) { +void * +xbt_dict_get_ext(xbt_dict_t dict, + const char *key, + int key_len) { - gras_assert(dict); + xbt_assert(dict); - return gras_dictelm_retrieve_ext(dict->head, key, key_len, data); + return xbt_dictelm_get_ext(dict->head, key, key_len); } /** - * gras_dict_retrieve: + * \brief Retrieve data from the dict (null-terminated key) * - * @dict: the dealer of data - * @key: the key to find data - * @data: the data that we are looking for - * @Returns: gras_error + * \param dict the dealer of data + * \param key the key to find data + * \returns the data that we are looking for * - * Search the given @key. mismatch_error when not found. + * Search the given \a key. THROWs mismatch_error when not found. + * Check xbt_dict_get_or_null() for a version returning NULL without exception when + * not found. */ -gras_error_t -gras_dict_retrieve(gras_dict_t *dict, - const char *key, - /* OUT */void **data) { - gras_assert(dict); +void * +xbt_dict_get(xbt_dict_t dict, + const char *key) { + xbt_ex_t e; + xbt_assert(dict); + + TRY { + return xbt_dictelm_get(dict->head, key); + } CATCH(e) { + if(e.category==not_found_error) { + return NULL; + } + RETHROW; + } + return NULL; +} - return gras_dictelm_retrieve(dict->head, key, data); +/** + * \brief like xbt_dict_get(), but returning NULL when not found + */ +void * +xbt_dict_get_or_null(xbt_dict_t dict, + const char *key) { + xbt_ex_t e; + void *res; + TRY { + res = xbt_dictelm_get(dict->head, key); + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + xbt_ex_free(e); + res=NULL; + } + return res; } /** - * gras_dict_remove_ext: + * \brief Remove data from the dict (arbitrary key) * - * @dict: the trash can - * @key: the key of the data to be removed - * @Returns: gras_error_t + * \param dict the trash can + * \param key the key of the data to be removed + * \param key_len the size of the \a key + * * - * Remove the entry associated with the given @key + * Remove the entry associated with the given \a key (throws not_found) */ -gras_error_t -gras_dict_remove_ext(gras_dict_t *dict, +void +xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) { - gras_assert(dict); + xbt_assert(dict); - return gras_dictelm_remove_ext(dict->head, key, key_len); + xbt_dictelm_remove_ext(dict->head, key, key_len); } /** - * gras_dict_remove: + * \brief Remove data from the dict (null-terminated key) * - * @head: the head of the dict - * @key: the key of the data to be removed - * @Returns: gras_error_t + * \param dict the head of the dict + * \param key the key of the data to be removed * - * Remove the entry associated with the given @key + * Remove the entry associated with the given \a key */ -gras_error_t -gras_dict_remove(gras_dict_t *dict, +void +xbt_dict_remove(xbt_dict_t dict, const char *key) { - if (!dict) - RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key); + if (!dict) + THROW1(arg_error,0,"Asked to remove key %s from NULL dict",key); - return gras_dictelm_remove(dict->head, key); + xbt_dictelm_remove(dict->head, key); } /** - * gras_dict_dump: + * \brief Outputs the content of the structure (debuging purpose) * - * @dict: the exibitionist - * @output: a function to dump each data in the tree - * @Returns: gras_error_t + * \param dict the exibitionist + * \param output a function to dump each data in the tree * - * Ouputs the content of the structure. (for debuging purpose). @ouput is a - * function to output the data. If NULL, data won't be displayed. + * Ouputs the content of the structure. (for debuging purpose). \a ouput is a + * function to output the data. If NULL, data won't be displayed, just the tree structure. */ -gras_error_t -gras_dict_dump(gras_dict_t *dict, +void +xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t *output) { - printf("Dict %p:\n", dict); - return gras_dictelm_dump(dict ? dict->head: NULL, output); + printf("Dict %p:\n", (void*)dict); + xbt_dictelm_dump(dict ? dict->head: NULL, output); } -