Commit 744fa130 authored by Tim Rühsen's avatar Tim Rühsen

Rename container set functions

* include/wget/wget.h:
  rename wget_vector_set_growth_policy() to wget_vector_set_resize_factor(),
  rename wget_hashmap_set_growth_policy() to wget_hashmap_set_resize_factor(),
  rename wget_hashmap_setloadfactor() to wget_hashmap_set_load_factor(),
  rename wget_stringmap_setloadfactor() to wget_stringmap_set_load_factor(),
  rename wget_stringmap_set_growth_policy() to wget_stringmap_set_resize_factor().
* libwget/hashmap.c: Likewise
* libwget/stringmap.c: Likewise
* libwget/vector.c: Likewise
parent 1328e176
Pipeline #41905042 failed with stages
in 27 minutes and 33 seconds
......@@ -633,7 +633,7 @@ typedef int (*wget_vector_destructor_t)(void *elem);
WGETAPI wget_vector_t *
wget_vector_create(int max, wget_vector_compare_t cmp) G_GNUC_WGET_MALLOC;
WGETAPI void
wget_vector_set_growth_policy(wget_vector_t *v, float off);
wget_vector_set_resize_factor(wget_vector_t *v, float off);
WGETAPI int
wget_vector_find(const wget_vector_t *v, const void *elem) G_GNUC_WGET_NONNULL((2));
WGETAPI int
......@@ -703,15 +703,11 @@ 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;
WGETAPI void
wget_hashmap_set_growth_policy(wget_hashmap_t *h, float off);
wget_hashmap_set_resize_factor(wget_hashmap_t *h, float factor);
WGETAPI int
wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const void *value, size_t valuesize);
WGETAPI int
wget_hashmap_put_noalloc(wget_hashmap_t *h, const void *key, const void *value);
//WGETAPI int
// wget_hashmap_put_ident(WGET_HASHMAP *h, const void *key, size_t keysize);
//WGETAPI int
// wget_hashmap_put_ident_noalloc(WGET_HASHMAP *h, const void *key);
WGETAPI int
wget_hashmap_size(const wget_hashmap_t *h) G_GNUC_WGET_PURE;
WGETAPI int
......@@ -722,6 +718,7 @@ WGETAPI void
wget_hashmap_clear(wget_hashmap_t *h);
WGETAPI int
wget_hashmap_get(const wget_hashmap_t *h, const void *key, void **value);
#define wget_hashmap_get(a, b, c) wget_hashmap_get((a), (b), (void **)(c))
WGETAPI int
wget_hashmap_contains(const wget_hashmap_t *h, const void *key);
WGETAPI int
......@@ -737,7 +734,7 @@ WGETAPI void
WGETAPI void
wget_hashmap_set_value_destructor(wget_hashmap_t *h, wget_hashmap_value_destructor_t destructor);
WGETAPI void
wget_hashmap_setloadfactor(wget_hashmap_t *h, float factor);
wget_hashmap_set_load_factor(wget_hashmap_t *h, float factor);
/*
* Stringmap datatype routines
......@@ -758,10 +755,6 @@ WGETAPI int
wget_stringmap_put(wget_stringmap_t *h, const char *key, const void *value, size_t valuesize);
WGETAPI int
wget_stringmap_put_noalloc(wget_stringmap_t *h, const char *key, const void *value);
//WGETAPI int
// wget_stringmap_put_ident(WGET_STRINGMAP *h, const char *key);
//WGETAPI int
// wget_stringmap_put_ident_noalloc(WGET_STRINGMAP *h, const char *key);
WGETAPI int
wget_stringmap_size(const wget_stringmap_t *h) G_GNUC_WGET_PURE;
WGETAPI int
......@@ -772,6 +765,7 @@ WGETAPI void
wget_stringmap_clear(wget_stringmap_t *h);
WGETAPI int
wget_stringmap_get(const wget_stringmap_t *h, const char *key, void **value);
#define wget_stringmap_get(a, b, c) wget_stringmap_get((a), (b), (void **)(c))
WGETAPI int
wget_stringmap_contains(const wget_stringmap_t *h, const char *key);
WGETAPI int
......@@ -787,9 +781,9 @@ WGETAPI void
WGETAPI void
wget_stringmap_set_value_destructor(wget_hashmap_t *h, wget_stringmap_value_destructor_t destructor);
WGETAPI void
wget_stringmap_setloadfactor(wget_stringmap_t *h, float factor);
wget_stringmap_set_load_factor(wget_stringmap_t *h, float factor);
WGETAPI void
wget_stringmap_set_growth_policy(wget_stringmap_t *h, float off);
wget_stringmap_set_resize_factor(wget_stringmap_t *h, float factor);
/*
* Thread wrapper routines
......
......@@ -63,8 +63,8 @@ struct _wget_hashmap_st {
cur, // number of entries in use
threshold; // resize when max reaches threshold
float
off, // resize strategy: >0: resize = off * max, <0: resize = max + (-off)
factor;
resize_factor, // resize strategy: >0: resize = off * max, <0: resize = max + (-off)
load_factor;
};
/**
......@@ -97,13 +97,13 @@ wget_hashmap_t *wget_hashmap_create(int max, wget_hashmap_hash_t hash, wget_hash
h->entry = xcalloc(max, sizeof(_entry_t *));
h->max = max;
h->cur = 0;
h->off = 2;
h->resize_factor = 2;
h->hash = hash;
h->cmp = cmp;
h->key_destructor = free;
h->value_destructor = free;
h->factor = 0.75;
h->threshold = (int)(max * h->factor);
h->load_factor = 0.75;
h->threshold = (int)(max * h->load_factor);
return h;
}
......@@ -148,7 +148,7 @@ static void hashmap_rehash(wget_hashmap_t *h, int newmax, int recalc_hash)
xfree(h->entry);
h->entry = new_entry;
h->max = newmax;
h->threshold = (int)(newmax * h->factor);
h->threshold = (int)(newmax * h->load_factor);
}
}
......@@ -166,15 +166,7 @@ static void hashmap_new_entry(wget_hashmap_t *h, unsigned int hash, const char *
h->entry[pos] = entry;
if (++h->cur >= h->threshold) {
int newsize;
if (h->off > 0) {
newsize = (int) (h->max * h->off);
} else if (h->off < 0) {
newsize = (int) (h->max - h->off);
} else {
newsize = 0; // resizing switched off
}
int newsize = (int) (h->max * h->resize_factor);
if (newsize > 0)
hashmap_rehash(h, newsize, 0);
......@@ -270,6 +262,18 @@ int wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const v
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
* \return 1 if \p key has been found, 0 if not found
*
* Check if \p key exists in \p h.
*/
int wget_hashmap_contains(const wget_hashmap_t *h, const void *key)
{
return wget_hashmap_get(h, key, NULL);
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
......@@ -280,6 +284,7 @@ int wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const v
*
* Neither \p h nor \p key must be %NULL.
*/
#undef wget_hashmap_get
int wget_hashmap_get(const wget_hashmap_t *h, const void *key, void **value)
{
if (h && key) {
......@@ -295,18 +300,6 @@ int wget_hashmap_get(const wget_hashmap_t *h, const void *key, void **value)
return 0;
}
/**
* \param[in] h Hashmap
* \param[in] key Key to search for
* \return 1 if \p key has been found, 0 if not found
*
* Check if \p key exists in \p h.
*/
int wget_hashmap_contains(const wget_hashmap_t *h, const void *key)
{
return wget_hashmap_get(h, key, NULL);
}
G_GNUC_WGET_NONNULL_ALL
static int hashmap_remove_entry(wget_hashmap_t *h, const char *key, int free_kv)
{
......@@ -544,32 +537,31 @@ void wget_hashmap_set_value_destructor(wget_hashmap_t *h, wget_hashmap_value_des
*
* Default is 0.75.
*/
void wget_hashmap_setloadfactor(wget_hashmap_t *h, float factor)
void wget_hashmap_set_load_factor(wget_hashmap_t *h, float factor)
{
if (h) {
h->factor = factor;
h->threshold = (int)(h->max * h->factor);
h->load_factor = factor;
h->threshold = (int)(h->max * h->load_factor);
// rehashing occurs earliest on next put()
}
}
/**
* \param[in] h Hashmap
* \param[in] off Hashmap growth mode:
* positive values: increase size by multiplying \p off, e.g. 2 doubles the size on each resize
* negative values: increase size by \p -off entries on each resize (the integer value is taken).
* 0: switch off resizing
* \param[in] factor Hashmap growth factor
*
* Set the growth policy for internal memory.
* Set the factor for resizing the hashmap when it's load factor is reached.
*
* The new size is 'factor * oldsize'. If the new size is less or equal 0,
* the involved put function will do nothing and the internal state of
* the hashmap will not change.
*
* Default is 2.
*/
void wget_hashmap_set_growth_policy(wget_hashmap_t *h, float off)
void wget_hashmap_set_resize_factor(wget_hashmap_t *h, float factor)
{
if (!h)
return;
h->off = off;
if (h)
h->resize_factor = factor;
}
/**@}*/
......@@ -165,6 +165,7 @@ int wget_stringmap_put(wget_stringmap_t *h, const char *key, const void *value,
*
* Neither \p h nor \p key must be %NULL.
*/
#undef wget_stringmap_get
int wget_stringmap_get(const wget_stringmap_t *h, const char *key, void **value)
{
return wget_hashmap_get(h, key, value);
......@@ -329,25 +330,26 @@ void wget_stringmap_set_value_destructor(wget_hashmap_t *h, wget_stringmap_value
*
* Default is 0.75.
*/
void wget_stringmap_setloadfactor(wget_stringmap_t *h, float factor)
void wget_stringmap_set_load_factor(wget_stringmap_t *h, float factor)
{
wget_hashmap_setloadfactor(h, factor);
wget_hashmap_set_load_factor(h, factor);
}
/**
* \param[in] h Stringmap
* \param[in] off Stringmap growth mode:
* positive values: increase size by multiplying \p off, e.g. 2 doubles the size on each resize
* negative values: increase size by \p -off entries on each resize (the integer value is taken).
* 0: switch off resizing
* \param[in] off Stringmap growth factor
*
* Set the growth policy for internal memory.
* Set the factor for resizing the stringmap when it's load factor is reached.
*
* The new size is 'factor * oldsize'. If the new size is less or equal 0,
* the involved put function will do nothing and the internal state of
* the stringmap will not change.
*
* Default is 2.
*/
void wget_stringmap_set_growth_policy(wget_stringmap_t *h, float off)
void wget_stringmap_set_resize_factor(wget_stringmap_t *h, float factor)
{
wget_hashmap_set_growth_policy(h, off);
wget_hashmap_set_resize_factor(h, factor);
}
/**@}*/
......@@ -47,7 +47,7 @@ struct _wget_vector_st {
bool
sorted : 1; // 1=list is sorted, 0=list is not sorted
float
off; // number of elements to add if resize occurs
resize_factor; // factor to calculate new vector size
};
/**
......@@ -72,7 +72,7 @@ wget_vector_t *wget_vector_create(int max, wget_vector_compare_t cmp)
v->entry = xmalloc(max * sizeof(void *));
v->max = max;
v->off = 2;
v->resize_factor = 2;
v->cmp = cmp;
return v;
......@@ -80,18 +80,20 @@ wget_vector_t *wget_vector_create(int max, wget_vector_compare_t cmp)
/**
* \param[in] v Vector
* \param[in] off Vector growth mode:
* positive values: increase size by multiplying \p off, e.g. 2 doubles the size on each resize
* negative values: increase size by \p -off entries on each resize (the integer value is taken).
* \param[in] factor Vector growth factor
*
* Set the growth policy for internal memory.
* Set the factor for resizing the vector when it is full.
*
* The new size is 'factor * oldsize'. If the new size is less or equal the old size,
* the involved insertion function will return an error and the internal state of
* the vector will not change.
*
* Default is 2.
*/
void wget_vector_set_growth_policy(wget_vector_t *v, float off)
void wget_vector_set_resize_factor(wget_vector_t *v, float factor)
{
if (v)
v->off = off;
v->resize_factor = factor;
}
static int G_GNUC_WGET_NONNULL((2)) _vec_insert_private(wget_vector_t *v, const void *elem, size_t size, int pos, int replace, int alloc)
......@@ -109,16 +111,15 @@ static int G_GNUC_WGET_NONNULL((2)) _vec_insert_private(wget_vector_t *v, const
if (!replace) {
if (v->max == v->cur) {
if (v->off > 0) {
v->max = (int) (v->max * v->off);
} else if (v->off < 0) {
v->max = (int) (v->max - v->off);
} else {
int newsize = (int) (v->max * v->resize_factor);
if (newsize <= v->max) {
if (alloc)
free(elemp);
return -1;
}
v->max = newsize;
v->entry = xrealloc(v->entry, v->max * sizeof(void *));
}
......
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