mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
list: added new list_index() function
This commit is contained in:
parent
e3fc832a0c
commit
e32fe06efd
2 changed files with 31 additions and 0 deletions
|
@ -110,3 +110,10 @@ void list_sort(struct list *l, cmp_cb_t cmp);
|
|||
|
||||
/** Set single element in list */
|
||||
int list_set(struct list *l, int index, void *value);
|
||||
|
||||
/** Return index in list for value.
|
||||
*
|
||||
* @retval <0 No list entry matching \p value was found.
|
||||
* @retval >=0 Entry \p value was found at returned index.
|
||||
*/
|
||||
ssize_t list_index(struct list *l, void *value);
|
||||
|
|
24
lib/list.c
24
lib/list.c
|
@ -198,3 +198,27 @@ int list_set(struct list *l, int index, void *value)
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t list_index(struct list *l, void *p)
|
||||
{
|
||||
void *e;
|
||||
ssize_t f;
|
||||
|
||||
pthread_mutex_lock(&l->lock);
|
||||
|
||||
assert(l->state == STATE_INITIALIZED);
|
||||
|
||||
for (size_t i = 0; i < list_length(l); i++) {
|
||||
e = list_at(l, i);
|
||||
if (e == p) {
|
||||
f = i;
|
||||
goto found;
|
||||
}
|
||||
}
|
||||
|
||||
f = -1;
|
||||
|
||||
found: pthread_mutex_unlock(&l->lock);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue