aboutsummaryrefslogtreecommitdiff
path: root/bpt_test.c
blob: 225baa0d3d0e36f3a94350e02be1cd1792eacdee (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
#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_mapping(bpt_key_t key, void *value, void *user_data) {
  printf(" %d -> %s\n", key, value);
}

void print_tree(bpt_t b) {
  bpt_for_mappings(b, print_mapping, NULL);
}

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;
}