-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample.c
174 lines (144 loc) · 4.53 KB
/
example.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#include "rb.h"
#include "avl.h"
#include "list.h"
#include "hash.h"
// RB TREE EXAMPLE
typedef struct {
int number;
rb_node_t node;
} test_rb_t;
int test_rb_compare(const rb_node_t *lhs, const rb_node_t *rhs, const void *aux) {
const test_rb_t *a = rb_ref(lhs, test_rb_t, node);
const test_rb_t *b = rb_ref(rhs, test_rb_t, node);
if (a->number < b->number) return -1;
if (a->number > b->number) return 1;
return 0;
}
void rb() {
printf(">> RB\n");
rb_tree_t tree;
rb_init(&tree, NULL);
test_rb_t a = { 1 };
test_rb_t b = { 2 };
test_rb_t c = { 3 };
rb_insert(&tree, &a.node, &test_rb_compare);
rb_insert(&tree, &b.node, &test_rb_compare);
rb_insert(&tree, &c.node, &test_rb_compare);
// Display them
rb_node_t *node = rb_head(&tree);
while (node) {
rb_node_t *next = rb_next(node);
rb_node_t *prev = rb_prev(node);
test_rb_t *c = rb_ref(node, test_rb_t, node);
test_rb_t *n = next ? rb_ref(next, test_rb_t, node) : NULL;
test_rb_t *p = prev ? rb_ref(prev, test_rb_t, node) : NULL;
printf("current: %d, next: %d, prev: %d\n",
c->number,
n ? n->number : -1,
p ? p->number : -1);
node = next;
}
}
// AVL TREE EXAMPLE
typedef struct {
int number;
avl_node_t node;
} test_avl_t;
int test_avl_compare(const avl_node_t *lhs, const avl_node_t *rhs, const void *aux) {
const test_avl_t *a = avl_ref(lhs, test_avl_t, node);
const test_avl_t *b = avl_ref(rhs, test_avl_t, node);
if (a->number < b->number) return -1;
if (a->number > b->number) return 1;
return 0;
}
void avl() {
printf(">> AVL\n");
avl_tree_t tree;
avl_init(&tree, NULL);
test_avl_t a = { 1 };
test_avl_t b = { 2 };
test_avl_t c = { 3 };
avl_insert(&tree, &a.node, &test_avl_compare);
avl_insert(&tree, &b.node, &test_avl_compare);
avl_insert(&tree, &c.node, &test_avl_compare);
// Display them
avl_node_t *node = avl_head(&tree);
while (node) {
avl_node_t *next = avl_next(node);
avl_node_t *prev = avl_prev(node);
test_avl_t *c = avl_ref(node, test_avl_t, node);
test_avl_t *n = next ? avl_ref(next, test_avl_t, node) : NULL;
test_avl_t *p = prev ? avl_ref(prev, test_avl_t, node) : NULL;
printf("current: %d, next: %d, prev: %d\n",
c->number,
n ? n->number : -1,
p ? p->number : -1);
node = next;
}
}
// LIST EXAMPLE
typedef struct {
const char *message;
link_t link;
} test_list_t;
void list() {
printf(">> LIST\n");
list_t list;
list_init(&list);
test_list_t a = { "Hello" };
test_list_t b = { "Intrusive" };
test_list_t c = { "World" };
list_push_back(&list, &a.link);
list_push_back(&list, &b.link);
list_push_back(&list, &c.link);
link_t *node = list_head(&list);
while (node) {
link_t *next = list_next(node);
link_t *prev = list_prev(node);
test_list_t *c = list_ref(node, test_list_t, link);
test_list_t *n = next ? list_ref(next, test_list_t, link) : NULL;
test_list_t *p = prev ? list_ref(prev, test_list_t, link) : NULL;
printf("current: %s, next: %s, prev: %s\n",
c->message,
n ? n->message : "(None)",
p ? p->message : "(None)");
node = next;
}
}
typedef struct {
const char *message;
hashnode_t node;
} test_hash_t;
size_t hash_func(const void *key, size_t keylen) {
// One character keys for test, you can implement a more complex hash
// function here
return ((const char *)key)[0];
}
void hash() {
printf(">> HASH\n");
hashtable_t hash;
test_hash_t a = { "Hello" };
test_hash_t b = { "Intrusive" };
test_hash_t c = { "World" };
hashtable_init(&hash, 10, &hash_func);
hashtable_insert(&hash, &a.node, "a", 2);
hashtable_insert(&hash, &b.node, "b", 2);
hashtable_insert(&hash, &c.node, "c", 2);
hashnode_t *ga = hashtable_search(&hash, "a", 2);
hashnode_t *gb = hashtable_search(&hash, "b", 2);
hashnode_t *gc = hashtable_search(&hash, "c", 2);
test_hash_t *aa = hashtable_ref(ga, test_hash_t, node);
test_hash_t *bb = hashtable_ref(gb, test_hash_t, node);
test_hash_t *cc = hashtable_ref(gc, test_hash_t, node);
printf("%s\n", aa->message);
printf("%s\n", bb->message);
printf("%s\n", cc->message);
hashtable_destroy(&hash);
}
int main() {
rb();
avl();
list();
hash();
}