liblist-c

C type-agnostic linked-list library
git clone https://git.sinitax.com/sinitax/liblist-c
Log | Files | Refs | LICENSE | sfeed.txt

commit ee0ca19605dec0939977957207dda9af5eb997e3
parent 2f54fc46a72f532feb3f98a4cc2cae88bbd0169f
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 27 Feb 2022 13:58:27 +0100

Added link inuse check, sorted insert, and list clear funcs

Diffstat:
Minclude/list.h | 6++++++
Mliblist.api | 5+++++
Mliblist.lds | 7++++++-
Msrc/list.c | 38+++++++++++++++++++++++++++++++++++++-
4 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/include/list.h b/include/list.h @@ -32,9 +32,13 @@ void list_init(struct list *list); void list_free(struct list *list, void (*free_item)(void *), int offset); +void list_clear(struct list *list); + bool list_empty(struct list *list); size_t list_len(struct list *list); +void list_insert_sorted(struct list *list, struct link *link, + int (*compare)(struct link *a, struct link *b)); void list_sort(struct list *list, int (*compare)(struct link *a, struct link *b)); @@ -52,6 +56,8 @@ struct link *list_pop_back(struct list *list); struct link *link_iter(struct link *link, int n); struct link *link_pop(struct link *link); +bool link_inuse(struct link *link); + void link_prepend(struct link *list, struct link *link); void link_append(struct link *list, struct link *link); diff --git a/liblist.api b/liblist.api @@ -1,9 +1,12 @@ list_init list_free +list_clear + list_empty list_len +list_insert_sorted list_sort list_index @@ -17,6 +20,8 @@ list_push_back list_pop_front list_pop_back +link_inuse + link_iter link_pop diff --git a/liblist.lds b/liblist.lds @@ -2,10 +2,13 @@ LIBLIST_1.0 { global: list_init; list_free; - + + list_clear; + list_empty; list_len; + list_insert_sorted; list_sort; list_index; @@ -19,6 +22,8 @@ LIBLIST_1.0 { list_pop_front; list_pop_back; + link_inuse; + link_iter; link_pop; diff --git a/src/list.c b/src/list.c @@ -6,7 +6,9 @@ #define ASSERT(x) assert((x), __FILE__, __LINE__, #x) -static void assert(bool cond, const char *file, size_t line, const char *condstr); +static void assert(bool cond, const char *file, + size_t line, const char *condstr); + static struct link *link_iter_fwd(struct link *link, size_t n); static struct link *link_iter_bwd(struct link *link, size_t n); @@ -76,6 +78,15 @@ list_free(struct list *list, void (*free_item)(void *), int offset) } } +void +list_clear(struct list *list) +{ + ASSERT(list != NULL); + + while (!list_empty(list)) + list_pop_back(list); +} + bool list_empty(struct list *list) { @@ -100,6 +111,22 @@ list_len(struct list *list) } void +list_insert_sorted(struct list *list, struct link *insert, + int (*compare)(struct link *a, struct link *b)) +{ + struct link *link; + + ASSERT(list != NULL && link != NULL && compare != NULL); + + for (LIST_ITER(list, link)) { + if (compare(insert, link) > 0) { + link_append(link, insert); + break; + } + } +} + +void list_sort(struct list *list, int (*compare)(struct link *a, struct link *b)) { struct link *link, *cmp, *next; @@ -229,6 +256,14 @@ link_append(struct link *cur, struct link *link) link->next->prev = link; } +bool +link_inuse(struct link *link) +{ + ASSERT(link != NULL); + + return link->prev != NULL && link->next != NULL; +} + struct link * link_iter(struct link *link, int n) { @@ -247,6 +282,7 @@ link_pop(struct link *link) link->prev->next = link->next; if (link->next) link->next->prev = link->prev; + *link = LINK_EMPTY; return link; }