From: Christian Grothoff Date: Wed, 25 Jan 2017 13:41:00 +0000 (+0100) Subject: add generic insertion sort logic X-Git-Tag: taler-0.2.1~310 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=0b6a79bd69c8fbeb64fa0be14647fd2ee61ef043;p=oweals%2Fgnunet.git add generic insertion sort logic --- 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 +/** + * Insertion sort of @a element into DLL from @a head to @a tail + * sorted by @a comparator. + * + * @param TYPE element type of the elements, i.e. `struct ListElement` + * @param comparator function like memcmp() to compare elements; takes + * three arguments, the @a comparator_cls and two elements, + * returns an `int` (-1, 0 or 1) + * @param comparator_cls closure for @a comparator + * @param[in,out] head head of DLL + * @param[in,out] tail tail of DLL + * @param element element to insert + */ +#define GNUNET_CONTAINER_DLL_insert_sorted(TYPE,comparator,comparator_cls,head,tail,element) do { \ + if ( (NULL == head) || \ + (0 < comparator (comparator_cls, \ + element, \ + head)) ) \ + { \ + /* insert at head, e;e,emt < head */ \ + GNUNET_CONTAINER_DLL_insert (head, \ + tail, \ + element); \ + } \ + else \ + { \ + TYPE *pos; \ + \ + for (pos = head; \ + NULL != pos; \ + pos = pos->next) \ + if (0 < \ + comparator (comparator_cls, \ + element, \ + pos)) \ + break; /* element < pos */ \ + if (NULL == pos) /* => element > tail */ \ + GNUNET_CONTAINER_DLL_insert_tail (head, \ + tail, \ + element); \ + else /* prev < element < pos */ \ + GNUNET_CONTAINER_DLL_insert_after (head, \ + tail, \ + element, \ + pos->prev); \ + } \ + } while (0) + + /* ******************** Heap *************** */