-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrange.c
More file actions
88 lines (75 loc) · 1.66 KB
/
Copy pathrange.c
File metadata and controls
88 lines (75 loc) · 1.66 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stddef.h>
#include "range.h"
static inline int
range_search_internal(range_root_t root, uint64_t start, uint64_t end, range_t **parent)
{
int n = 0;
range_t *r = *root;
while (r) {
*parent = r;
if (r->end <= start)
n = 1, r = r->right;
else if (end <= r->start)
n = -1, r = r->left;
else
return 0;
}
return n;
}
range_t *
range_search(range_root_t root, uint64_t start, uint64_t end)
{
range_t *parent = NULL;
int n = range_search_internal(root, start, end, &parent);
return (n == 0 ? parent : NULL);
}
range_t *
range_insert(range_root_t root, range_t *range)
{
range_t *parent = NULL;
int n = range_search_internal(root, range->start, range->end, &parent);
if (parent != NULL) {
if (n < 0)
parent->left = range;
else if (n > 0)
parent->right = range;
else
return parent;
} else
*root = range;
return NULL;
}
static int depth = 0;
void
do_range_print(range_t *range)
{
if (range->left) {
depth++; do_range_print(range->left); depth--;
}
printf("%*c%08lx-%08lx\n", 4 * depth, ' ', range->start, range->end);
if (range->right) {
depth++; do_range_print(range->right); depth--;
}
}
void
range_print(range_root_t root)
{
depth = 0;
do_range_print(*root);
}
#ifdef TEST
#include <stdio.h>
#include "range.h"
range_t *root = NULL;
int main()
{
range_t stack = { .left = NULL, .right = NULL, .start = 0xb000, .end = 0xc000 };
range_t code = { .left = NULL, .right = NULL, .start = 0x4000, .end = 0x4100 };
range_t data = { .left = NULL, .right = NULL, .start = 0x4100, .end = 0x4400 };
range_insert(&root, &stack);
range_insert(&root, &code);
range_insert(&root, &data);
range_print(root);
}
#endif