Commit e4c660eb authored by Tim Rühsen's avatar Tim Rühsen

Fix hashmap + stringmap docs

* include/wget/wget.h: Add docs for hashmap/stringmap typedefs,
  fix argument of wget_hashmap_iterator_free()
* libwget/hashmap.c: Add docs, fix wget_hashmap_iterator_free()
* libwget/stringmap.c: Add docs
* unit-tests/test.c: Fix call to wget_stringmap_iterator_free()
parent 7537c9fd
Pipeline #42134649 failed with stages
in 27 minutes and 39 seconds
......@@ -689,17 +689,26 @@ WGETAPI void
WGETAPI void
wget_vector_sort(wget_vector_t *v);
/*
* Hashmap datatype routines
/**
* \ingroup libwget-hashmap
*
* @{
*/
/// Type of the hashmap
typedef struct _wget_hashmap_st wget_hashmap_t;
/// Type of the hashmap iterator
typedef struct _wget_hashmap_iterator_st wget_hashmap_iterator_t;
/// Type of the hashmap compare function
typedef int (*wget_hashmap_compare_t)(const void *key1, const void *key2);
/// Type of the hashmap hash function
typedef unsigned int (*wget_hashmap_hash_t)(const void *value);
/// Type of the hashmap browse callback function
typedef int (*wget_hashmap_browse_t)(void *ctx, const void *key, void *value);
/// Type of the hashmap key destructor function
typedef void (*wget_hashmap_key_destructor_t)(void *key);
/// Type of the hashmap value destructor function
typedef void (*wget_hashmap_value_destructor_t)(void *value);
/** @} */
WGETAPI wget_hashmap_t
*wget_hashmap_create(int max, wget_hashmap_hash_t hash, wget_hashmap_compare_t cmp) G_GNUC_WGET_MALLOC;
......@@ -740,25 +749,35 @@ WGETAPI void
WGETAPI wget_hashmap_iterator_t
*wget_hashmap_iterator_alloc(wget_hashmap_t *h) G_GNUC_WGET_MALLOC;
WGETAPI void
wget_hashmap_iterator_free(wget_hashmap_iterator_t *it);
wget_hashmap_iterator_free(wget_hashmap_iterator_t **iter);
WGETAPI void
*wget_hashmap_iterator_next(wget_hashmap_iterator_t *it, void **value);
*wget_hashmap_iterator_next(wget_hashmap_iterator_t *iter, void **value);
/*
* Stringmap datatype routines
/**
* \ingroup libwget-stringmap
*
* @{
*/
/// Type of the stringmap
typedef wget_hashmap_t wget_stringmap_t;
/// Type of the stringmap iterator
typedef wget_hashmap_iterator_t wget_stringmap_iterator_t;
/// Type of the stringmap compare function
typedef int (*wget_stringmap_compare_t)(const char *key1, const char *key2);
/// Type of the stringmap hash function
typedef unsigned int (*wget_stringmap_hash_t)(const char *value);
/// Type of the stringmap browse callback function
typedef int (*wget_stringmap_browse_t)(void *ctx, const char *key, void *value);
/// Type of the stringmap key destructor function
typedef void (*wget_stringmap_key_destructor_t)(char *key);
/// Type of the stringmap value destructor function
typedef void (*wget_stringmap_value_destructor_t)(void *value);
/** @} */
/// Wrapper around wget_hashmap_iterator_alloc().
#define wget_stringmap_iterator_alloc wget_hashmap_iterator_alloc
/// Wrapper around wget_hashmap_iterator_free().
#define wget_stringmap_iterator_free wget_hashmap_iterator_free
// #define wget_stringmap_iterator_next(a,b) ((char *) wget_hashmap_iterator_next((a),(b)))
WGETAPI wget_stringmap_t *
wget_stringmap_create(int max) G_GNUC_WGET_MALLOC;
......
......@@ -76,40 +76,71 @@ struct _wget_hashmap_iterator_st {
pos;
};
/**
* \file
* \brief Hashmap functions
* \defgroup libwget-hashmap Hashmap functions
* @{
*
* Hashmaps are key/value stores that perform at O(1) for insertion, searching and removing.
*/
/**
* \param[in] h Hashmap
* \return New iterator instance for \p h
*
* Creates a hashmap iterator for \p.
*/
wget_hashmap_iterator_t *wget_hashmap_iterator_alloc(wget_hashmap_t *h)
{
struct _wget_hashmap_iterator_st *it = wget_calloc(1, sizeof(struct _wget_hashmap_iterator_st));
struct _wget_hashmap_iterator_st *iter = wget_calloc(1, sizeof(struct _wget_hashmap_iterator_st));
it->h = h;
iter->h = h;
return (wget_hashmap_iterator_t *) it;
return (wget_hashmap_iterator_t *) iter;
}
void wget_hashmap_iterator_free(wget_hashmap_iterator_t *it)
/**
* \param[in] iter Hashmap iterator
*
* Free the given iterator \p iter.
*/
void wget_hashmap_iterator_free(wget_hashmap_iterator_t **iter)
{
xfree(it);
if (iter)
xfree(*iter);
}
void *wget_hashmap_iterator_next(wget_hashmap_iterator_t *it, void **value)
/**
* \param[in] iter Hashmap iterator
* \param[out] value Pointer to the value belonging to the returned key
* \return Pointer to the key or NULL if no more elements left
*
* Returns the next key / value in the hashmap. If all key/value pairs have been
* iterated over the function returns NULL and \p value is untouched.
*
* When iterating over a hashmap, the order of returned key/value pairs is not defined.
*/
void *wget_hashmap_iterator_next(wget_hashmap_iterator_t *iter, void **value)
{
struct _wget_hashmap_iterator_st *iter = (struct _wget_hashmap_iterator_st *) it;
struct _wget_hashmap_st *h = iter->h;
struct _wget_hashmap_iterator_st *_iter = (struct _wget_hashmap_iterator_st *) iter;
struct _wget_hashmap_st *h = _iter->h;
if (iter->entry) {
if ((iter->entry = iter->entry->next)) {
if (_iter->entry) {
if ((_iter->entry = _iter->entry->next)) {
found:
if (value)
*value = iter->entry->value;
return iter->entry->key;
*value = _iter->entry->value;
return _iter->entry->key;
}
iter->pos++;
_iter->pos++;
}
if (!iter->entry) {
for (; iter->pos < h->max; iter->pos++) {
if (h->entry[iter->pos]) {
iter->entry = h->entry[iter->pos];
if (!_iter->entry) {
for (; _iter->pos < h->max; _iter->pos++) {
if (h->entry[_iter->pos]) {
_iter->entry = h->entry[_iter->pos];
goto found;
}
}
......@@ -118,15 +149,6 @@ found:
return NULL;
}
/**
* \file
* \brief Hashmap functions
* \defgroup libwget-hashmap Hashmap functions
* @{
*
* Hashmaps are key/value stores that perform at O(1) for insertion, searching and removing.
*/
/**
* \param[in] max Initial number of pre-allocated entries
* \param[in] hash Hash function to build hashes from elements
......
......@@ -352,6 +352,16 @@ void wget_stringmap_set_resize_factor(wget_stringmap_t *h, float factor)
wget_hashmap_set_resize_factor(h, factor);
}
/**
* \param[in] iter Stringmap iterator
* \param[out] value Pointer to the value belonging to the returned key
* \return Pointer to the key or NULL if no more elements left
*
* Returns the next key / value in the stringmap. If all key/value pairs have been
* iterated over the function returns NULL and \p value is untouched.
*
* When iterating over a stringmap, the order of returned key/value pairs is not defined.
*/
void *wget_stringmap_iterator_next(wget_stringmap_iterator_t *h, char **value)
{
return wget_hashmap_iterator_next(h, (void **) value);
......
......@@ -1869,7 +1869,7 @@ static void test_stringmap(void)
info_printf("key/value don't match (%s | %s)\n", key, val);
} else ok++;
}
wget_stringmap_iterator_free(iter); iter = NULL;
wget_stringmap_iterator_free(&iter);
if (it != wget_stringmap_size(m)) {
failed++;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment