From 257ad41d50d443b986580cf067fef3465299d8ab Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Wed, 31 Aug 2011 18:26:07 +0200 Subject: Initial commit. --- bpt_test.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 bpt_test.c (limited to 'bpt_test.c') diff --git a/bpt_test.c b/bpt_test.c new file mode 100644 index 0000000..9f57152 --- /dev/null +++ b/bpt_test.c @@ -0,0 +1,64 @@ +#include "stdio.h" +#include "stdlib.h" +#include "bitmapped_patricia_tree.h" + +void print_deallocation(bpt_key_t key, void *value) { + printf("Deallocated: %s\n", value); +} + +void print_tree(bpt_t b) { + int i; + for (i = 0; i < 10; i++) { + if (bpt_has_key(b, i)) { + printf(" %d -> %s\n", i, bpt_get(b, i)); + } + } +} + +bpt_t bpt_assoc_and_release(bpt_t bpt, bpt_key_t key, void *value) { + bpt_t new_bpt = bpt_assoc(bpt, key, value); + bpt_release(bpt); + return new_bpt; +} + +bpt_t bpt_dissoc_and_release(bpt_t bpt, bpt_key_t key) { + bpt_t new_bpt = bpt_dissoc(bpt, key); + bpt_release(bpt); + return new_bpt; +} + +int main() { + bpt_t b1, b2; + + b1 = bpt_assoc (NULL, 0, "zero"); + b1 = bpt_assoc_and_release(b1, 1, "one"); + b1 = bpt_assoc_and_release(b1, 2, "two"); + b1 = bpt_assoc_and_release(b1, 3, "three"); + b1 = bpt_assoc_and_release(b1, 4, "four"); + bpt_seal(b1); + + b2 = bpt_assoc (b1, 0, "null"); + b2 = bpt_assoc_and_release(b2, 3, "a triple"); + + b1 = bpt_assoc_and_release(b1, 5, "five"); + + printf("Map 1:\n"); + print_tree(b1); + printf("\nMap 2:\n"); + print_tree(b2); + + bpt_set_dealloc_hook(b1, 0, print_deallocation); + bpt_set_dealloc_hook(b1, 1, print_deallocation); + bpt_set_dealloc_hook(b1, 2, print_deallocation); + bpt_set_dealloc_hook(b1, 3, print_deallocation); + bpt_set_dealloc_hook(b1, 4, print_deallocation); + bpt_set_dealloc_hook(b1, 5, print_deallocation); + + bpt_set_dealloc_hook(b2, 0, print_deallocation); + bpt_set_dealloc_hook(b2, 3, print_deallocation); + + bpt_release(b1); + bpt_release(b2); + + return EXIT_SUCCESS; +} -- cgit v1.2.3