Commit 0c35a58b authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

list: add vlist_{insert, remove, remove_all}() and corresponding tests

parent 09832330
......@@ -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);
......
Supports Markdown
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