1 | #include "avl.h" |
---|
2 | #include "avl-private.h" |
---|
3 | |
---|
4 | extern "C" { |
---|
5 | int strcmp(const char *, const char *); |
---|
6 | } |
---|
7 | |
---|
8 | int main(){ |
---|
9 | // operations: |
---|
10 | // find(tree(K, V) *, K) |
---|
11 | // int empty(tree(K, V) *); |
---|
12 | // tree(K, V) * insert(tree(K, V) *, K, V); |
---|
13 | // int remove(tree(K, V) **, K); |
---|
14 | |
---|
15 | // int -> int |
---|
16 | tree(int, int) * imap = create(-1, (int)0); |
---|
17 | insert(&imap, 12, 13); |
---|
18 | insert(&imap, 2, 3); |
---|
19 | assert( height(imap) == 2 ); |
---|
20 | |
---|
21 | printf("%d %d %d\n", *find(imap, 2), *find(imap, 12), *find(imap, -1)); |
---|
22 | |
---|
23 | remove(&imap, -1); |
---|
24 | delete(imap); |
---|
25 | |
---|
26 | // int -> char * |
---|
27 | tree(int, char *) * smap = create(-1, "baz"); |
---|
28 | insert(&smap, 12, "bar"); |
---|
29 | insert(&smap, 2, "foo"); |
---|
30 | assert( height(smap) == 2 ); |
---|
31 | |
---|
32 | printf("%s %s %s\n", *find(smap, 2), *find(smap, 12), *find(smap, -1)); |
---|
33 | |
---|
34 | remove(&smap, -2); |
---|
35 | delete(smap); |
---|
36 | |
---|
37 | // char* -> char* |
---|
38 | struct c_str { char *str; }; // wraps a C string |
---|
39 | int ?<?(c_str a, c_str b) { |
---|
40 | return strcmp(a.str,b.str) < 0; |
---|
41 | } |
---|
42 | tree(c_str, char *) * ssmap = create((c_str){"queso"}, "cheese"); |
---|
43 | insert(&ssmap, (c_str){"foo"}, "bar"); |
---|
44 | insert(&ssmap, (c_str){"hello"}, "world"); |
---|
45 | assert( height(ssmap) == 2 ); |
---|
46 | |
---|
47 | printf("%s %s %s\n", *find(ssmap, (c_str){"hello"}), *find(ssmap, (c_str){"foo"}), *find(ssmap, (c_str){"queso"})); |
---|
48 | |
---|
49 | remove(&ssmap, (c_str){"foo"}); |
---|
50 | delete(ssmap); |
---|
51 | } |
---|