-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex1.c
225 lines (174 loc) · 5.18 KB
/
ex1.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Simple AVL tree - example
*
* Copyright 2022 Ian Pilcher <[email protected]>
*/
#include <assert.h>
#include <inttypes.h>
#include <savl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct employee {
char *family_name;
char *given_name;
struct savl_node name_node;
struct savl_node number_node;
uint32_t employee_number;
};
/*
* Macros can handle both const and non-const pointers
*/
#define EMPLOYEE_FROM_NAME_NODE(n) \
((n) ? SAVL_NODE_CONTAINER((n), struct employee, name_node) : NULL)
#define EMPLOYEE_FROM_NUMBER_NODE(n) \
((n) ? SAVL_NODE_CONTAINER((n), struct employee, number_node) : NULL)
static struct savl_node *employees_by_name;
static struct savl_node *employees_by_number;
static int cmp_names(const union savl_key k, const struct savl_node *const n)
{
const struct employee *key, *node;
int result;
key = k.p;
node = EMPLOYEE_FROM_NAME_NODE(n);
result = strcmp(key->family_name, node->family_name);
if (result != 0)
return result;
return strcmp(key->given_name, node->given_name);
}
static int cmp_numbers(const union savl_key k, const struct savl_node *const n)
{
struct employee *node;
uint32_t key;
key = k.u;
node = EMPLOYEE_FROM_NUMBER_NODE(n);
/* Subtraction might underflow */
return (key > node->employee_number) - (key < node->employee_number);
}
static void free_by_name(struct savl_node *const n)
{
struct employee *emp;
emp = EMPLOYEE_FROM_NAME_NODE(n);
free(emp->given_name);
free(emp->family_name);
free(emp);
}
static _Bool add_employee(struct employee *emp)
{
struct savl_node *existing;
union savl_key key;
/* Check for an existing employee with the same number */
key.u = emp->employee_number;
existing = savl_get(employees_by_number, cmp_numbers, key);
if (existing != NULL) {
fprintf(stderr, "Employee #%" PRIu32 " already exists!\n",
emp->employee_number);
return 0;
}
/* Try to add the employee to the by-name tree */
key.p = emp;
existing = savl_try_add(&employees_by_name, cmp_names, key,
&emp->name_node);
if (existing != NULL) {
fprintf(stderr, "Employee %s, %s already exists!\n",
emp->family_name, emp->given_name);
return 0;
}
/* Actually add the employee to the by-number tree */
key.u = emp->employee_number;
existing = savl_try_add(&employees_by_number, cmp_numbers, key,
&emp->number_node);
assert(existing == NULL);
return 1;
}
static struct employee *get_by_name(char *restrict const family_name,
char *restrict const given_name)
{
/* cmp_names() wants an employee struct as its key */
struct employee emp;
union savl_key key;
struct savl_node *node;
emp.family_name = family_name;
emp.given_name = given_name;
key.p = &emp;
node = savl_get(employees_by_name, cmp_names, key);
return EMPLOYEE_FROM_NAME_NODE(node);
}
static struct employee *get_by_number(const uint32_t employee_number)
{
union savl_key key;
struct savl_node *node;
key.u = employee_number;
node = savl_get(employees_by_number, cmp_numbers, key);
return EMPLOYEE_FROM_NUMBER_NODE(node);
}
static char *checked_strdup(const char *const s)
{
char *new;
new = strdup(s);
if (new == NULL) {
fputs("Memory allocation failure\n", stderr);
abort();
}
return new;
}
/* Some test data */
static const struct {
const char *family_name;
const char *given_name;
uint32_t employee_number;
}
employees[] = {
{ "Oldrich", "Sharif", 5403298 },
{ "Uno", "Eleri", 498302 },
{ "Lykos", "Paavali", 4890 },
{ "Villum", "Irmina", 498302 }, /* DUPLICATE EMPLOYEE NUMBER */
{ "Feivush", "Georg", 49803 },
{ "Zumra", "Kehina", 4123 },
{ "Feivush", "Georg", 98021 }, /* DUPLICATE NAME */
{ "Mahmut", "Sif", 509 },
{ "Chidimma", "Pankaj", 874189 }
};
int main(void)
{
struct employee *emp;
struct savl_node *n;
unsigned int i;
/* Load the data */
for (i = 0; i < sizeof employees / sizeof employees[0]; ++i) {
emp = calloc(1, sizeof *emp);
if (emp == NULL) {
fputs("Memory allocation failure\n", stderr);
abort();
}
emp->family_name = checked_strdup(employees[i].family_name);
emp->given_name = checked_strdup(employees[i].given_name);
emp->employee_number = employees[i].employee_number;
if (!add_employee(emp))
free_by_name(&emp->name_node);
}
puts("\nList of employees by name:");
for (n = savl_first(employees_by_name); n != NULL; n = savl_next(n)) {
emp = EMPLOYEE_FROM_NAME_NODE(n);
printf(" %s, %s: %" PRIu32 "\n", emp->family_name,
emp->given_name, emp->employee_number);
}
puts("\nList of employees by number:");
for (n = savl_first(employees_by_number); n != NULL; n = savl_next(n)) {
emp = EMPLOYEE_FROM_NUMBER_NODE(n);
printf(" %" PRIu32 ": %s, %s\n", emp->employee_number,
emp->family_name, emp->given_name);
}
emp = get_by_name("Feivush", "Georg"); /* Should check for NULL */
printf("\nGeorg Feivush's employee number is %" PRIu32 "\n",
emp->employee_number);
emp = get_by_number(4890); /* Should check for NULL */
printf("\nEmployee number 4890 is %s %s\n",
emp->given_name, emp->family_name);
putchar('\n');
savl_free(&employees_by_name, free_by_name);
/* No need to free by number; nodes were embedded in employee structs */
employees_by_number = NULL;
return 0;
}