aboutsummaryrefslogtreecommitdiff
path: root/bpt_test.c
blob: 9f571528f3607b85b405a0b75898f36a80366ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}