]> git.kaiwu.me - klib.git/commitdiff
use more robust comparison in examples
authorHeng Li <lh3@me.com>
Fri, 20 Apr 2018 12:09:28 +0000 (08:09 -0400)
committerHeng Li <lh3@me.com>
Fri, 20 Apr 2018 12:10:41 +0000 (08:10 -0400)
kavl.h
test/kavl_test.c

diff --git a/kavl.h b/kavl.h
index ee6c10512f53d930817359981ae262340dac3800..1041b86a264e5d17e6d6189f9b2a9adbe9f34d9b 100644 (file)
--- a/kavl.h
+++ b/kavl.h
@@ -34,7 +34,7 @@ struct my_node {
   char key;
   KAVL_HEAD(struct my_node) head;
 };
-#define my_cmp(p, q) ((p)->key - (q)->key)
+#define my_cmp(p, q) (((q)->key < (p)->key) - ((p)->key < (q)->key))
 KAVL_INIT(my, struct my_node, head, my_cmp)
 
 int main(void) {
index 74123ceb6641b31dcfe16676456c0e9478290980..e0a82211b0a7cb43e785f8bd366f924a41cdf872 100644 (file)
@@ -11,7 +11,7 @@ struct my_node {
        KAVL_HEAD(struct my_node) head;
 };
 
-#define my_cmp(p, q) ((p)->key - (q)->key)
+#define my_cmp(p, q) (((p)->key > (q)->key) - ((p)->key < (q)->key))
 KAVL_INIT(my, struct my_node, head, my_cmp)
 
 int check(struct my_node *p, int *hh)