src/llist.c: Add llist_next_filter()

This convenience function can be used to return the successor of a list
item if it is matched by a filter callback and return NULL otherwise.

We will use this for an improved version of the LLIST_FIND_FOREACH macro
that can be used whenever results are known to be continuous.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2011-10-04 09:34:06 +02:00
parent a0afb7ded2
commit 1fa9564916
3 changed files with 18 additions and 0 deletions

View File

@ -114,6 +114,19 @@ llist_next (llist_item_t *i)
return i ? i->next : NULL; return i ? i->next : NULL;
} }
/*
* Return the successor of a list item if it is matched by some filter
* callback. Return NULL otherwise.
*/
llist_item_t *
llist_next_filter (llist_item_t *i, long data, llist_fn_match_t fn_match)
{
if (i && i->next && fn_match (i->next->data, data))
return i->next;
else
return NULL;
}
/* /*
* Get the actual data of an item. * Get the actual data of an item.
*/ */

View File

@ -65,6 +65,7 @@ void llist_free_inner (llist_t *, llist_fn_free_t);
llist_item_t *llist_first (llist_t *); llist_item_t *llist_first (llist_t *);
llist_item_t *llist_nth (llist_t *, int); llist_item_t *llist_nth (llist_t *, int);
llist_item_t *llist_next (llist_item_t *); llist_item_t *llist_next (llist_item_t *);
llist_item_t *llist_next_filter (llist_item_t *, long, llist_fn_match_t);
llist_item_t *llist_find_first (llist_t *, long, llist_fn_match_t); llist_item_t *llist_find_first (llist_t *, long, llist_fn_match_t);
llist_item_t *llist_find_next (llist_item_t *, long, llist_fn_match_t); llist_item_t *llist_find_next (llist_item_t *, long, llist_fn_match_t);
llist_item_t *llist_find_nth (llist_t *, int, long, llist_fn_match_t); llist_item_t *llist_find_nth (llist_t *, int, long, llist_fn_match_t);
@ -72,6 +73,8 @@ llist_item_t *llist_find_nth (llist_t *, int, long, llist_fn_match_t);
#define LLIST_FIRST(l) llist_first(l) #define LLIST_FIRST(l) llist_first(l)
#define LLIST_NTH(l, n) llist_nth(l, n) #define LLIST_NTH(l, n) llist_nth(l, n)
#define LLIST_NEXT(i) llist_next(i) #define LLIST_NEXT(i) llist_next(i)
#define LLIST_NEXT_FILTER(i, data, fn_match) \
llist_next_filter(i, data, (llist_fn_match_t)fn_match)
#define LLIST_FIND_FIRST(l, data, fn_match) \ #define LLIST_FIND_FIRST(l, data, fn_match) \
llist_find_first(l, data, (llist_fn_match_t)fn_match) llist_find_first(l, data, (llist_fn_match_t)fn_match)
#define LLIST_FIND_NEXT(i, data, fn_match) \ #define LLIST_FIND_NEXT(i, data, fn_match) \

View File

@ -64,6 +64,8 @@ struct llist_ts {
#define LLIST_TS_FIRST(l_ts) llist_first ((llist_t *)l_ts) #define LLIST_TS_FIRST(l_ts) llist_first ((llist_t *)l_ts)
#define LLIST_TS_NTH(l_ts, n) llist_nth ((llist_t *)l_ts, n) #define LLIST_TS_NTH(l_ts, n) llist_nth ((llist_t *)l_ts, n)
#define LLIST_TS_NEXT(i) llist_next (i) #define LLIST_TS_NEXT(i) llist_next (i)
#define LLIST_TS_NEXT_FILTER(i, data, fn_match) \
llist_next_filter (i, data, (llist_fn_match_t)fn_match)
#define LLIST_TS_FIND_FIRST(l_ts, data, fn_match) \ #define LLIST_TS_FIND_FIRST(l_ts, data, fn_match) \
llist_find_first ((llist_t *)l_ts, data, (llist_fn_match_t)fn_match) llist_find_first ((llist_t *)l_ts, data, (llist_fn_match_t)fn_match)
#define LLIST_TS_FIND_NEXT(i, data, fn_match) \ #define LLIST_TS_FIND_NEXT(i, data, fn_match) \