9 #define CALLOC(type, num) ((type*)calloc(num, sizeof(type)))
14 KRMQ_HEAD(struct my_node
) head
;
17 #define my_cmp(p, q) (((p)->key > (q)->key) - ((p)->key < (q)->key))
18 #define my_lt2(p, q) ((p)->val < (q)->val)
19 KRMQ_INIT(my
, struct my_node
, head
, my_cmp
, my_lt2
)
21 int check(struct my_node
*p
, int *hh
, int *min
)
23 *hh
= 0, *min
= INT_MAX
;
25 int c
= 1, h
[2] = {0, 0}, m
[2] = {INT_MAX
, INT_MAX
};
27 if (p
->head
.p
[0]) c
+= check(p
->head
.p
[0], &h
[0], &m
[0]);
28 if (p
->head
.p
[1]) c
+= check(p
->head
.p
[1], &h
[1], &m
[1]);
29 *min
= *min
< m
[0]? *min
: m
[0];
30 *min
= *min
< m
[1]? *min
: m
[1];
31 *hh
= (h
[0] > h
[1]? h
[0] : h
[1]) + 1;
32 if (*min
!= p
->head
.s
->val
)
33 fprintf(stderr
, "min %d != %d at %c\n", *min
, p
->head
.s
->val
, p
->key
);
34 if (h
[1] - h
[0] != (int)p
->head
.balance
)
35 fprintf(stderr
, "%d - %d != %d at %c\n", h
[1], h
[0], p
->head
.balance
, p
->key
);
36 if (c
!= (int)p
->head
.size
)
37 fprintf(stderr
, "%d != %d at %c\n", p
->head
.size
, c
, p
->key
);
42 int check_rmq(const struct my_node
*root
, int lo
, int hi
)
44 struct my_node s
, t
, *p
, *q
;
47 s
.key
= lo
, t
.key
= hi
;
48 p
= krmq_rmq(my
, root
, &s
, &t
);
49 krmq_interval(my
, root
, &s
, 0, &q
);
50 if (p
== 0) return -1;
51 krmq_itr_find(my
, root
, q
, &itr
);
53 const struct my_node
*r
= krmq_at(&itr
);
54 if (r
->key
> hi
) break;
55 //fprintf(stderr, "%c\t%d\n", r->key, r->val);
56 if (r
->val
< min
) min
= r
->val
;
57 } while (krmq_itr_next(my
, &itr
));
58 assert((min
== INT_MAX
&& p
== 0) || (min
< INT_MAX
&& p
));
59 if (min
!= p
->val
) fprintf(stderr
, "rmq_min %d != %d\n", p
->val
, min
);
63 int print_tree(const struct my_node
*p
)
67 if (p
->head
.p
[0] || p
->head
.p
[1]) {
69 if (p
->head
.p
[0]) c
+= print_tree(p
->head
.p
[0]);
72 if (p
->head
.p
[1]) c
+= print_tree(p
->head
.p
[1]);
76 printf("%c:%d/%d", p
->key
, p
->val
, p
->head
.s
->val
);
80 void check_and_print(struct my_node
*root
)
83 check(root
, &h
, &min
);
88 void shuffle(int n
, char a
[])
91 for (i
= n
; i
> 1; --i
) {
93 j
= (int)(drand48() * i
);
94 tmp
= a
[j
]; a
[j
] = a
[i
-1]; a
[i
-1] = tmp
;
102 struct my_node
*root
= 0;
103 struct my_node
*p
, *q
, t
;
108 for (i
= 33, n
= 0; i
<= 126; ++i
)
109 if (i
!= '(' && i
!= ')' && i
!= '.' && i
!= ';')
112 for (i
= 0; i
< n
; ++i
) {
113 p
= CALLOC(struct my_node
, 1);
116 q
= krmq_insert(my
, &root
, p
, &cnt
);
118 check(root
, &h
, &min
);
122 for (i
= 0; i
< n
/2; ++i
) {
124 //fprintf(stderr, "i=%d, key=%c, n/2=%d\n", i, t.key, n/2);
125 q
= krmq_erase(my
, &root
, &t
, 0);
127 check(root
, &h
, &min
);
129 check_and_print(root
);
131 check_rmq(root
, '0', '9');
132 check_rmq(root
, '!', '~');
133 check_rmq(root
, 'A', 'Z');
134 check_rmq(root
, 'F', 'G');
135 check_rmq(root
, 'a', 'z');
136 for (i
= 0; i
< n
; ++i
) {
138 lo
= (int)(drand48() * n
);
139 hi
= (int)(drand48() * n
);
140 check_rmq(root
, lo
, hi
);
143 krmq_itr_first(my
, root
, &itr
);
145 const struct my_node
*r
= krmq_at(&itr
);
147 } while (krmq_itr_next(my
, &itr
));
149 krmq_free(struct my_node
, head
, root
, free
);