commit ba2dfcbf03b25bad7e93ecf7448a21f69b24e8b4
parent a60fca54f73071de1b03e96f845dc9147dc41a7e
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 8 Jun 2023 03:13:30 +0200
Add user pointer to sort api
Diffstat:
3 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/include/list.h b/include/list.h
@@ -50,7 +50,7 @@
#endif
typedef void (*list_item_free_fn)(void *p);
-typedef bool (*list_sort_order_fn)(const void *p1, const void *p2);
+typedef bool (*list_sort_order_fn)(const void *p1, const void *p2, void *u);
struct list_link {
struct list_link *prev;
@@ -78,11 +78,11 @@ struct list_link *list_at(struct list *list, size_t n);
struct list_link *list_at_back(struct list *list, size_t n);
void list_insert_sorted(struct list *list, struct list_link *link,
- bool reverse, list_sort_order_fn in_order, size_t offset);
+ bool reverse, list_sort_order_fn in_order, size_t offset, void *user);
void list_insert_sorted_bwd(struct list *list, struct list_link *link,
- bool reverse, list_sort_order_fn in_order, size_t offset);
+ bool reverse, list_sort_order_fn in_order, size_t offset, void *user);
void list_insertion_sort(struct list *list,
- bool reverse, list_sort_order_fn in_order, size_t offset);
+ bool reverse, list_sort_order_fn in_order, size_t offset, void *user);
static inline void list_insert_front(struct list *list, struct list_link *link);
static inline void list_insert_back(struct list *list, struct list_link *link);
diff --git a/src/list.c b/src/list.c
@@ -92,14 +92,14 @@ list_at_back(struct list *list, size_t n)
void
list_insert_sorted(struct list *list, struct list_link *insert,
- bool reverse, list_sort_order_fn in_order, size_t offset)
+ bool reverse, list_sort_order_fn in_order, size_t offset, void *user)
{
struct list_link *link;
LIST_ABORT_ON_ARGS(!list || !insert || !in_order);
for (LIST_ITER(list, link)) {
- if (in_order(insert, link) == !reverse) {
+ if (in_order(insert, link, user) == !reverse) {
list_link_prepend(link, insert);
return;
}
@@ -110,7 +110,7 @@ list_insert_sorted(struct list *list, struct list_link *insert,
void
list_insert_sorted_bwd(struct list *list, struct list_link *insert,
- bool reverse, list_sort_order_fn in_order, size_t offset)
+ bool reverse, list_sort_order_fn in_order, size_t offset, void *user)
{
struct list_link *link;
void *link_item, *insert_item;
@@ -120,7 +120,7 @@ list_insert_sorted_bwd(struct list *list, struct list_link *insert,
insert_item = ((void *) insert) - offset;
for (LIST_ITER_BWD(list, link)) {
link_item = ((void *) link) - offset;
- if (in_order(link, insert) == !reverse) {
+ if (in_order(link, insert, user) == !reverse) {
list_link_append(link, insert);
return;
}
@@ -130,8 +130,8 @@ list_insert_sorted_bwd(struct list *list, struct list_link *insert,
}
void
-list_insertion_sort(struct list *list,
- bool reverse, list_sort_order_fn in_order, size_t offset)
+list_insertion_sort(struct list *list, bool reverse,
+ list_sort_order_fn in_order, size_t offset, void *user)
{
struct list_link *link, *cmp, *next;
void *link_item, *cmp_item;
@@ -145,7 +145,7 @@ list_insertion_sort(struct list *list,
link_item = ((void *) link) - offset;
while (LIST_INNER(cmp)) {
cmp_item = ((void *) cmp) - offset;
- if (in_order(cmp_item, link_item) == !reverse)
+ if (in_order(cmp_item, link_item, user) == !reverse)
break;
cmp = cmp->prev;
}
diff --git a/src/test.c b/src/test.c
@@ -11,7 +11,7 @@ struct arg {
};
bool
-test_sort(const void *p1, const void *p2)
+test_sort(const void *p1, const void *p2, void *u)
{
const struct arg *a1 = p1, *a2 = p2;
@@ -41,7 +41,7 @@ main(int argc, const char **argv)
}
list_insertion_sort(&list, atoi(argv[1]), test_sort,
- LIST_OFFSET(struct arg, link));
+ LIST_OFFSET(struct arg, link), NULL);
for (LIST_ITER_ITEMS(&list, item, struct arg, link))
printf("%s\n", item->str);