aboutsummaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-01-25 14:41:00 +0100
committerChristian Grothoff <christian@grothoff.org>2017-01-25 14:41:00 +0100
commit0b6a79bd69c8fbeb64fa0be14647fd2ee61ef043 (patch)
tree3336e901bf4aefdbcb26a07c0cbc8907efa9450c /src/include
parentd3880dd89a6aa28902c95708d9ab2126c96b57c1 (diff)
downloadgnunet-0b6a79bd69c8fbeb64fa0be14647fd2ee61ef043.tar.gz
gnunet-0b6a79bd69c8fbeb64fa0be14647fd2ee61ef043.zip
add generic insertion sort logic
Diffstat (limited to 'src/include')
-rw-r--r--src/include/gnunet_container_lib.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/include/gnunet_container_lib.h b/src/include/gnunet_container_lib.h
index f3aaa943b..d2f8c5d9c 100644
--- a/src/include/gnunet_container_lib.h
+++ b/src/include/gnunet_container_lib.h
@@ -2073,6 +2073,55 @@ GNUNET_CONTAINER_multihashmap32_iterator_destroy (struct GNUNET_CONTAINER_MultiH
2073 2073
2074 2074
2075 2075
2076/**
2077 * Insertion sort of @a element into DLL from @a head to @a tail
2078 * sorted by @a comparator.
2079 *
2080 * @param TYPE element type of the elements, i.e. `struct ListElement`
2081 * @param comparator function like memcmp() to compare elements; takes
2082 * three arguments, the @a comparator_cls and two elements,
2083 * returns an `int` (-1, 0 or 1)
2084 * @param comparator_cls closure for @a comparator
2085 * @param[in,out] head head of DLL
2086 * @param[in,out] tail tail of DLL
2087 * @param element element to insert
2088 */
2089#define GNUNET_CONTAINER_DLL_insert_sorted(TYPE,comparator,comparator_cls,head,tail,element) do { \
2090 if ( (NULL == head) || \
2091 (0 < comparator (comparator_cls, \
2092 element, \
2093 head)) ) \
2094 { \
2095 /* insert at head, e;e,emt < head */ \
2096 GNUNET_CONTAINER_DLL_insert (head, \
2097 tail, \
2098 element); \
2099 } \
2100 else \
2101 { \
2102 TYPE *pos; \
2103 \
2104 for (pos = head; \
2105 NULL != pos; \
2106 pos = pos->next) \
2107 if (0 < \
2108 comparator (comparator_cls, \
2109 element, \
2110 pos)) \
2111 break; /* element < pos */ \
2112 if (NULL == pos) /* => element > tail */ \
2113 GNUNET_CONTAINER_DLL_insert_tail (head, \
2114 tail, \
2115 element); \
2116 else /* prev < element < pos */ \
2117 GNUNET_CONTAINER_DLL_insert_after (head, \
2118 tail, \
2119 element, \
2120 pos->prev); \
2121 } \
2122 } while (0)
2123
2124
2076/* ******************** Heap *************** */ 2125/* ******************** Heap *************** */
2077 2126
2078 2127