Commit 8a96e670 authored by Tim Rühsen's avatar Tim Rühsen

Use float for container growth policy

* include/wget/wget.h: Use float for *_set_growth_policy()
* libwget/hashmap.c: Use float for wget_hashmap_set_growth_policy()
* libwget/stringmap.c: Use float for wget_stringmap_set_growth_policy()
* libwget/vector.c: Use float for wget_vector_set_growth_policy()

Additionally, the +/- logic has been reversed in the above functions.
parent cdb36007
Pipeline #41877810 passed with stages
in 47 minutes and 4 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, int off);
wget_vector_set_growth_policy(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,7 +703,7 @@ 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, int off);
wget_hashmap_set_growth_policy(wget_hashmap_t *h, float off);
WGETAPI int
wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const void *value, size_t valuesize);
WGETAPI int
......@@ -793,7 +793,7 @@ WGETAPI void
WGETAPI void
wget_stringmap_setloadfactor(wget_stringmap_t *h, float factor);
WGETAPI void
wget_stringmap_set_growth_policy(wget_stringmap_t *h, int off);
wget_stringmap_set_growth_policy(wget_stringmap_t *h, float off);
/*
* Thread wrapper routines
......
......@@ -57,13 +57,13 @@ struct _wget_hashmap_st {
wget_hashmap_value_destructor_t
value_destructor; // value destructor function
_entry_t
**entry; // pointer to array of pointers to entries
**entry; // pointer to array of pointers to entries
int
max, // allocated entries
cur, // number of entries in use
off, // resize strategy: >0: resize = max + off, <0: resize = -off * max
max, // allocated entries
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;
};
......@@ -97,7 +97,7 @@ 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->off = 2;
h->hash = hash;
h->cmp = cmp;
h->key_destructor = free;
......@@ -166,13 +166,18 @@ 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) {
hashmap_rehash(h, h->max + h->off, 0);
} else if (h->off<-1) {
hashmap_rehash(h, h->max * -h->off, 0);
newsize = (int) (h->max * h->off);
} else if (h->off < 0) {
newsize = (int) (h->max - h->off);
} else {
// no resizing occurs
newsize = 0; // resizing switched off
}
if (newsize > 0)
hashmap_rehash(h, newsize, 0);
}
}
......@@ -579,17 +584,20 @@ void wget_hashmap_setloadfactor(wget_hashmap_t *h, float factor)
/**
* \param[in] h Hashmap
* \param[in] off Hashmap growth mode:
* positive values: increase size by \p off entries on each resize
* negative values: increase size by multiplying \p -off, e.g. -2 doubles the size on each resize
* 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
*
* Set the growth policy for internal memory.
*
* Default is -2.
* Default is 2.
*/
void wget_hashmap_set_growth_policy(wget_hashmap_t *h, int off)
void wget_hashmap_set_growth_policy(wget_hashmap_t *h, float off)
{
if (h)
h->off = off;
if (!h)
return;
h->off = off;
}
/**@}*/
......@@ -357,14 +357,15 @@ void wget_stringmap_setloadfactor(wget_stringmap_t *h, float factor)
/**
* \param[in] h Stringmap
* \param[in] off Stringmap growth mode:
* positive values: increase size by \p off entries on each resize
* negative values: increase size by multiplying \p -off, e.g. -2 doubles the size on each resize
* 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
*
* Set the growth policy for internal memory.
*
* Default is -2.
* Default is 2.
*/
void wget_stringmap_set_growth_policy(wget_stringmap_t *h, int off)
void wget_stringmap_set_growth_policy(wget_stringmap_t *h, float off)
{
wget_hashmap_set_growth_policy(h, off);
}
......
......@@ -43,10 +43,11 @@ struct _wget_vector_st {
**entry; // pointer to array of pointers to elements
int
max, // allocated elements
cur, // number of elements in use
off; // number of elements to add if resize occurs
cur; // number of elements in use
bool
sorted : 1; // 1=list is sorted, 0=list is not sorted
float
off; // number of elements to add if resize occurs
};
/**
......@@ -71,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->off = 2;
v->cmp = cmp;
return v;
......@@ -80,12 +81,14 @@ 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 vector by \p off entries on each resize
* negative values: increase vector by multiplying \p -off, e.g. -2 doubles the size on each resize
* 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).
*
* Set the growth policy for internal memory.
*
* Default is 2.
*/
void wget_vector_set_growth_policy(wget_vector_t *v, int off)
void wget_vector_set_growth_policy(wget_vector_t *v, float off)
{
if (v)
v->off = off;
......@@ -107,14 +110,16 @@ 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->entry = xrealloc(v->entry, (v->max += v->off) * sizeof(void *));
} else if (v->off<-1) {
v->entry = xrealloc(v->entry, (v->max *= -v->off) * sizeof(void *));
v->max = (int) (v->max * v->off);
} else if (v->off < 0) {
v->max = (int) (v->max - v->off);
} else {
if (alloc)
free(elemp);
return -1;
}
v->entry = xrealloc(v->entry, v->max * sizeof(void *));
}
memmove(&v->entry[pos + 1], &v->entry[pos], (v->cur - pos) * 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