]> git.ozlabs.org Git - ccan/commitdiff
avl: Use definitions from order module
authorDavid Gibson <david@gibson.dropbear.id.au>
Thu, 18 Jun 2015 09:30:18 +0000 (19:30 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Thu, 18 Jun 2015 09:30:18 +0000 (19:30 +1000)
The AvlCompare type defined in the avl module is identical in signature to
the compare function used by qsort() and bsearch().  That has a common
definition in the new order module, so use that rather than defining its
own.

In addition use the standard comparison functions from order where possible
for the avl test code.

Cc: Joey Adams <joeyadams3.14159@gmail.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/avl/_info
ccan/avl/avl.c
ccan/avl/avl.h
ccan/avl/test/run.c

index 83893ddb468fffa59f24980b488472f0d946caca..c7fc98a46ff2f97327d9c92518a550651c255432 100644 (file)
@@ -33,7 +33,7 @@
  * 
  * int main(void)
  * {
- *     AVL          *avl = avl_new((AvlCompare) strcmp);
+ *     AVL          *avl = avl_new((total_order_noctx_cb) strcmp);
  *     AvlIter       i;
  *     struct tally *tally;
  *     char          line[256];
@@ -75,7 +75,7 @@ int main(int argc, char *argv[])
                return 1;
 
        if (strcmp(argv[1], "depends") == 0) {
-               /* Nothing */
+               printf("ccan/order\n");
                return 0;
        }
 
index 001ea438eb9f4c15967ce775e9d70711dada66be..d1647f3779b77790ff4dd225a16d417fdce9ea3a 100644 (file)
@@ -62,7 +62,7 @@ static int sign(int cmp)
        return 1;
 }
 
-AVL *avl_new(AvlCompare compare)
+AVL *avl_new(total_order_noctx_cb compare)
 {
        AVL *avl = malloc(sizeof(*avl));
        
index 4cbe08b1a285e0bce6deb4546178597b322e9096..304de7c2b12266e56194034f6c4e6cd88b3e1938 100644 (file)
 #include <stdbool.h>
 #include <stddef.h>
 
+#include <ccan/order/order.h>
+
 typedef struct AVL           AVL;
 typedef struct AvlNode       AvlNode;
 typedef struct AvlIter       AvlIter;
 
-typedef int (*AvlCompare)(const void *, const void *);
-
-AVL *avl_new(AvlCompare compare);
+AVL *avl_new(total_order_noctx_cb compare);
        /* Create a new AVL tree sorted with the given comparison function. */
 
 void avl_free(AVL *avl);
@@ -106,7 +106,7 @@ void avl_iter_next(AvlIter *iter);
 /***************** Internal data structures ******************/
 
 struct AVL {
-       AvlCompare  compare;
+       total_order_noctx_cb compare;
        AvlNode    *root;
        size_t      count;
 };
index 3ca88d9965c521a60d8d70337e6e3ccd37c06d04..f621a327aa80075229e6b76838ea810617253132 100644 (file)
@@ -132,18 +132,6 @@ struct test_item {
        size_t   insert_id; /* needed because qsort is not a stable sort */
 };
 
-static int compare_uint32_t(const void *ap, const void *bp)
-{
-       uint32_t a = *(const uint32_t *)ap;
-       uint32_t b = *(const uint32_t *)bp;
-       
-       if (a < b)
-               return -1;
-       if (a > b)
-               return 1;
-       return 0;
-}
-
 static int compare_test_item(const void *ap, const void *bp)
 {
        const struct test_item *a = *(void**)ap, *b = *(void**)bp;
@@ -266,7 +254,7 @@ static void benchmark(size_t max_per_trial, size_t round_count, bool random_coun
                }
                scramble(test_item, count, sizeof(*test_item));
                
-               avl = avl_new(compare_uint32_t);
+               avl = avl_new(order_u32_noctx);
                
                clear_stats();
                printf("   Inserting %zu items...\n", count);