-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrb.c
114 lines (100 loc) · 2.29 KB
/
rb.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "rbtree.h"
#include "comm.h"
//static uint64_t buf[MAX_TEST_NUM];
struct test_node
{
uint64_t index;
uint64_t key;
struct rb_node rb;
};
static struct test_node test_node[MAX_TEST_NUM];
static void rb_insert__(struct test_node *node, struct rb_root *root)
{
struct rb_node **new = &root->rb_node, *parent = NULL;
uint64_t key = node->key;
while (*new) {
parent = *new;
if (key < rb_entry(parent, struct test_node, rb)->key)
new = &parent->rb_left;
else
new = &parent->rb_right;
}
rb_link_node(&node->rb, parent, new);
rb_insert_color(&node->rb, root);
}
static inline void rb_erase__(struct test_node *node, struct rb_root *root)
{
rb_erase(&node->rb, root);
}
static inline struct test_node *rb_find__(uint64_t key, struct rb_root *root)
{
struct rb_node **new = &root->rb_node, *parent = NULL;
while (*new) {
parent = *new;
struct test_node *entry = rb_entry(parent, struct test_node, rb);
uint64_t entry_key = entry->key;
if (key == entry_key)
return entry;
if (key < entry->key)
new = &parent->rb_left;
else
new = &parent->rb_right;
}
return NULL;
}
void find_entry(uint64_t entry, struct rb_root *root)
{
struct test_node *data = rb_find__(entry, root);
assert(data && data->key == entry);
}
void delete_entry(uint64_t entry, struct rb_root *root)
{
struct test_node *data = rb_find__(entry, root);
rb_erase__(data, root);
}
void find(uint64_t *data, int num, struct rb_root *root)
{
for (int i = 0; i < num; ++i)
{
find_entry(data[i], root);
}
}
void delete(uint64_t *data, int num, struct rb_root *root)
{
for (int i = 0; i < num; ++i)
{
delete_entry(data[i], root);
}
}
void insert(uint64_t *data, int num, struct rb_root *root)
{
for (int i = 0; i < num; ++i)
{
rb_insert__(&test_node[i], root);
}
}
int main(int argc, char *argv[])
{
int num = atoi(argv[1]);
uint64_t *data = read_test_num(num);
assert(data);
uint64_t *data_r = read_rand_num(num);
assert(data_r);
struct rb_root root = RB_ROOT;
for (int i = 0; i < num; ++i)
{
test_node[i].index = i;
test_node[i].key = data[i];
}
time_begin();
insert(data, num, &root);
find(data_r, num, &root);
delete(data_r, num, &root);
int diff = time_diff();
printf("[%d] ", diff);
return 0;
}