Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/test/kavl-lite_test.c # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / test / kavl-lite_test.c` 60 lines ยท 1160 bytes Raw Blame History 1 #include 2 #include 3 #include 4 #include 5 #include "kavl-lite.h" 6 7 #define CALLOC(type, num) ((type*)calloc(num, sizeof(type))) 8 9 struct my_node { 10 int key; 11 KAVLL_HEAD(struct my_node) head; 12 }; 13 14 #define my_cmp(p, q) (((p)->key > (q)->key) - ((p)->key < (q)->key)) 15 KAVLL_INIT(my, struct my_node, head, my_cmp) 16 17 void shuffle(int n, char a[]) 18 { 19 int i, j; 20 for (i = n; i > 1; --i) { 21 char tmp; 22 j = (int)(drand48() * i); 23 tmp = a[j]; a[j] = a[i-1]; a[i-1] = tmp; 24 } 25 } 26 27 int main(void) 28 { 29 char buf[256]; 30 int i, n; 31 struct my_node *root = 0; 32 struct my_node *p, *q, t; 33 my_itr_t itr; 34 35 for (i = 33, n = 0; i <= 126; ++i) 36 if (i != '(' && i != ')' && i != '.' && i != ';') 37 buf[n++] = i; 38 shuffle(n, buf); 39 for (i = 0; i < n; ++i) { 40 p = CALLOC(struct my_node, 1); 41 p->key = buf[i]; 42 q = my_insert(&root, p); 43 if (p != q) free(p); 44 } 45 shuffle(n, buf); 46 for (i = 0; i < n/2; ++i) { 47 t.key = buf[i]; 48 q = my_erase(&root, &t); 49 if (q) free(q); 50 } 51 52 my_itr_first(root, &itr); 53 do { 54 const struct my_node *r = kavll_at(&itr); 55 putchar(r->key); 56 free((void*)r); 57 } while (my_itr_next(&itr)); 58 putchar('\n'); 59 return 0; 60 }