/* 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 "xbt/ex.h"
#include "dict_private.h"
-#include <stdlib.h> /* malloc() */
-#include <string.h> /* strlen() */
-
-#include <stdio.h>
-
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
"Dictionaries provide the same functionnalities than hash tables");
/*####[ Private prototypes ]#################################################*/
/*####[ Code ]###############################################################*/
/**
- * \ingroup XBT_dict
- *
- * \return pointer to the destination
+ * @brief Constructor
+ * @return pointer to the destination
*
* Creates and initialize a new dictionnary
*/
return res;
}
/**
- * \ingroup XBT_dict
- * \param dict the dictionnary to be freed
+ * @brief Destructor
+ * @param dict the dictionnary to be freed
*
* Frees a cache structure with all its childs.
*/
xbt_dictelm_free( &( (*dict)->head ) );
(*dict)->head = NULL;
}
- xbt_free(*dict);
+ free(*dict);
*dict=NULL;
}
}
/**
- * \ingroup XBT_dict
- *
+ * \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 #key
+ * \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 (#key as argument) when
- * #key is removed from the dictionnary
+ * \param free_ctn function to call with (\a key as argument) when
+ * \a key is removed from the dictionnary
*
- * set 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.
+ * 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.
*/
void
xbt_dict_set_ext(xbt_dict_t dict,
}
/**
- * \ingroup XBT_dict
+ * \brief Add data to the dict (null-terminated key)
*
* \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 (#key as argument) when
- * #key is removed from the dictionnary
+ * \param free_ctn function to call with (\a key as argument) when
+ * \a key is removed from the dictionnary
*
- * set 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.
*/
void
}
/**
- * \ingroup XBT_dict
+ * \brief Retrieve data from the dict (arbitrary key)
*
* \param dict the dealer of data
* \param key the key to find data
- * \param key_len the size of the #key
- * \param data the data that we are looking for
- * \return xbt_error
+ * \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.
*/
-xbt_error_t
-xbt_dict_get_ext(xbt_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) {
xbt_assert(dict);
- return xbt_dictelm_get_ext(dict->head, key, key_len, data);
+ return xbt_dictelm_get_ext(dict->head, key, key_len);
}
/**
- * \ingroup XBT_dict
+ * \brief Retrieve data from the dict (null-terminated key)
*
* \param dict the dealer of data
* \param key the key to find data
- * \param data the data that we are looking for
- * \return xbt_error
+ * \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.
*/
-xbt_error_t
+void *
xbt_dict_get(xbt_dict_t dict,
- const char *key,
- /* OUT */void **data) {
+ const char *key) {
xbt_assert(dict);
- return xbt_dictelm_get(dict->head, key, data);
+ return xbt_dictelm_get(dict->head, key);
+}
+
+/**
+ * \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;
}
/**
- * \ingroup XBT_dict
+ * \brief Remove data from the dict (arbitrary key)
*
* \param dict the trash can
* \param key the key of the data to be removed
- * \param key_len the size of the #key
- * \return xbt_error_t
+ * \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)
*/
-xbt_error_t
+void
xbt_dict_remove_ext(xbt_dict_t dict,
const char *key,
int key_len) {
xbt_assert(dict);
- return xbt_dictelm_remove_ext(dict->head, key, key_len);
+ xbt_dictelm_remove_ext(dict->head, key, key_len);
}
/**
- * \ingroup XBT_dict
+ * \brief Remove data from the dict (null-terminated key)
*
* \param dict the head of the dict
* \param key the key of the data to be removed
- * \return xbt_error_t
*
- * Remove the entry associated with the given #key
+ * Remove the entry associated with the given \a key
*/
-xbt_error_t
+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 xbt_dictelm_remove(dict->head, key);
+ xbt_dictelm_remove(dict->head, key);
}
/**
- * \ingroup XBT_dict
+ * \brief Outputs the content of the structure (debuging purpose)
*
* \param dict the exibitionist
* \param output a function to dump each data in the tree
- * \return xbt_error_t
*
- * 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.
*/
void