mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
list: add vlist_{insert, remove, remove_all}() and corresponding tests
This commit is contained in:
parent
3259abd581
commit
1ef09a7607
3 changed files with 77 additions and 3 deletions
|
@ -87,7 +87,11 @@ int vlist_destroy(struct vlist *l, dtor_cb_t dtor, bool free);
|
|||
void vlist_push(struct vlist *l, void *p);
|
||||
|
||||
/** Remove all occurences of a list item */
|
||||
void vlist_remove(struct vlist *l, void *p);
|
||||
void vlist_remove_all(struct vlist *l, void *p);
|
||||
|
||||
int vlist_remove(struct vlist *l, size_t idx);
|
||||
|
||||
int vlist_insert(struct vlist *l, size_t idx, void *p);
|
||||
|
||||
/** Return the first list element which is identified by a string in its first member variable.
|
||||
*
|
||||
|
@ -132,6 +136,7 @@ void vlist_extend(struct vlist *l, size_t len, void *val);
|
|||
/** Shallow copy a list. */
|
||||
int vlist_copy(struct vlist *dst, const struct vlist *src);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -111,7 +111,59 @@ void vlist_push(struct vlist *l, void *p)
|
|||
pthread_mutex_unlock(&l->lock);
|
||||
}
|
||||
|
||||
void vlist_remove(struct vlist *l, void *p)
|
||||
int vlist_remove(struct vlist *l, size_t idx)
|
||||
{
|
||||
pthread_mutex_lock(&l->lock);
|
||||
|
||||
assert(l->state == STATE_INITIALIZED);
|
||||
|
||||
if (idx >= l->length)
|
||||
return -1;
|
||||
|
||||
for (size_t i = idx; i < l->length - 1; i++)
|
||||
l->array[i] = l->array[i+1];
|
||||
|
||||
l->length--;
|
||||
|
||||
pthread_mutex_unlock(&l->lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vlist_insert(struct vlist *l, size_t idx, void *p)
|
||||
{
|
||||
size_t i;
|
||||
void *t, *o;
|
||||
|
||||
pthread_mutex_lock(&l->lock);
|
||||
|
||||
assert(l->state == STATE_INITIALIZED);
|
||||
|
||||
if (idx >= l->length)
|
||||
return -1;
|
||||
|
||||
/* Resize array if out of capacity */
|
||||
if (l->length + 1 > l->capacity) {
|
||||
l->capacity += LIST_CHUNKSIZE;
|
||||
l->array = realloc(l->array, l->capacity * sizeof(void *));
|
||||
}
|
||||
|
||||
o = p;
|
||||
for (i = idx; i < l->length; i++) {
|
||||
t = l->array[i];
|
||||
l->array[i] = o;
|
||||
o = t;
|
||||
}
|
||||
|
||||
l->array[l->length] = o;
|
||||
l->length++;
|
||||
|
||||
pthread_mutex_unlock(&l->lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vlist_remove_all(struct vlist *l, void *p)
|
||||
{
|
||||
int removed = 0;
|
||||
|
||||
|
|
|
@ -155,7 +155,24 @@ Test(list, basics)
|
|||
ret = vlist_contains(&l, (void *) 55);
|
||||
cr_assert(ret);
|
||||
|
||||
vlist_remove(&l, (void *) 55);
|
||||
void *before_ptr = vlist_at(&l, 12);
|
||||
|
||||
ret = vlist_insert(&l, 12, (void *) 123);
|
||||
cr_assert_eq(ret, 0);
|
||||
cr_assert_eq(vlist_at(&l, 12), (void *) 123, "Is: %p", vlist_at(&l, 12));
|
||||
|
||||
ret = vlist_remove(&l, 12);
|
||||
cr_assert_eq(ret, 0);
|
||||
cr_assert_eq(vlist_at(&l, 12), before_ptr);
|
||||
|
||||
int counts, before_len;
|
||||
|
||||
before_len = vlist_length(&l);
|
||||
counts = vlist_contains(&l, (void *) 55);
|
||||
cr_assert_gt(counts, 0);
|
||||
|
||||
vlist_remove_all(&l, (void *) 55);
|
||||
cr_assert_eq(vlist_length(&l), before_len - counts);
|
||||
|
||||
ret = vlist_contains(&l, (void *) 55);
|
||||
cr_assert(!ret);
|
||||
|
|
Loading…
Add table
Reference in a new issue