diff options
author | saravanank <saravanank@vmware.com> | 2019-05-03 15:57:57 +0200 |
---|---|---|
committer | saravanank <saravanank@vmware.com> | 2019-05-15 06:40:50 +0200 |
commit | 9b68e496042b7fc8bf2f27671fbfbc6f02541a17 (patch) | |
tree | 699ce1e408e8b6cb6e6b49298f1bdfc176bb415f /lib/linklist.c | |
parent | pimd: PIM BSM Processing g2rp timer start and expiry routine(dummy) (diff) | |
download | frr-9b68e496042b7fc8bf2f27671fbfbc6f02541a17.tar.xz frr-9b68e496042b7fc8bf2f27671fbfbc6f02541a17.zip |
lib: implement utility function API which does the following
1. listnode_add_sort_nodup - This API adds to list only if no duplicate
element available in the list. returns true/false
2. list_filter_out_nodes - This API deletes the nodes which satisfy the given
condition. condition is passed as a func ptr in
API. This function takes in node data(void ptr).
Signed-off-by: Saravanan K <saravanank@vmware.com>
Diffstat (limited to 'lib/linklist.c')
-rw-r--r-- | lib/linklist.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/linklist.c b/lib/linklist.c index 40c4b2716..6cb639b27 100644 --- a/lib/linklist.c +++ b/lib/linklist.c @@ -92,6 +92,46 @@ void listnode_add_head(struct list *list, void *val) list->count++; } +bool listnode_add_sort_nodup(struct list *list, void *val) +{ + struct listnode *n; + struct listnode *new; + int ret; + + assert(val != NULL); + + if (list->cmp) { + for (n = list->head; n; n = n->next) { + ret = (*list->cmp)(val, n->data); + if (ret < 0) { + new = listnode_new(); + new->data = val; + + new->next = n; + new->prev = n->prev; + + if (n->prev) + n->prev->next = new; + else + list->head = new; + n->prev = new; + list->count++; + return true; + } + /* found duplicate return false */ + if (ret == 0) + return false; + } + } + + new = listnode_new(); + new->data = val; + + LISTNODE_ATTACH(list, new); + + return true; +} + void listnode_add_sort(struct list *list, void *val) { struct listnode *n; @@ -242,6 +282,23 @@ void list_delete_all_node(struct list *list) list->count = 0; } +void list_filter_out_nodes(struct list *list, bool (*cond)(void *data)) +{ + struct listnode *node; + struct listnode *next; + void *data; + + assert(list); + + for (ALL_LIST_ELEMENTS(list, node, next, data)) { + if ((cond && cond(data)) || (!cond)) { + if (*list->del) + (*list->del)(data); + list_delete_node(list, node); + } + } +} + void list_delete(struct list **list) { assert(*list); |