-
Notifications
You must be signed in to change notification settings - Fork 2
/
treeio.c
163 lines (117 loc) · 2.96 KB
/
treeio.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
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "include/treeio.h"
#include "include/avltree.h"
#define DATA_PATH "data/randnumbers"
#define OUT_PATH "data/timestamps"
#define BILLION 1000000000.0
#define __INIT_DELTA_TIME__ \
double time_d; \
struct timespec start, end; \
#define __START_DELTA_TIME__ \
time_d = 0.0; \
clock_gettime(CLOCK_REALTIME, &start); \
#define __CALC_DELTA_TIME__ \
clock_gettime(CLOCK_REALTIME, &end); \
time_d = ((end).tv_sec - (start).tv_sec) + \
((end).tv_nsec - (start).tv_nsec) \
/ BILLION
#define DELTA_TIME_FORMAT ">> %s: %lf seconds --\n"
#define __print_delta_time(stream) \
fprintf((stream), DELTA_TIME_FORMAT, __func__, time_d); \
static inline void exit_program(int code)
{
msg(CLR_YELLOW EXIT_MSG CLR_RESET);
exit(code);
}
static inline void run_file_test(Node *root)
{
FILE *input = fopen(DATA_PATH, "r");
FILE *output = fopen(OUT_PATH, "w");
if (!input || !output)
return;
int n;
int counter = 0;
double avg_time = 0.0;
__INIT_DELTA_TIME__;
fprintf(output, "---- Inserting nodes ----\n\n");
while (fscanf(input, "%d", &n) == 1) {
__START_DELTA_TIME__;
*root = insert(*root, n);
__CALC_DELTA_TIME__;
__print_delta_time(output);
avg_time += time_d;
counter++;
}
avg_time /= (double) counter;
fprintf(output, "\n\n==== Average insertion time: %lf seconds\n\n", avg_time);
fprintf(output, "\n\n---- Searching nodes ----\n\n");
rewind(input);
avg_time = 0.0;
counter = 0;
while (fscanf(input, "%d", &n) == 1) {
__START_DELTA_TIME__;
search(*root, n);
__CALC_DELTA_TIME__;
__print_delta_time(output);
avg_time += time_d;
counter++;
}
avg_time /= (double) counter;
fprintf(output, "\n\n==== Average search time: %lf seconds\n\n", avg_time);
fclose(input);
fclose(output);
msg(TESTS_COMPLETE_MSG);
}
static inline void insert_nodes(Node *root)
{
int key;
__INIT_DELTA_TIME__;
while (scanf("%d", &key) == 1) {
__START_DELTA_TIME__;
*root = insert(*root, key);
__CALC_DELTA_TIME__;
__print_delta_time(stdout);
}
}
static inline void search_node(Node root)
{
int key;
__INIT_DELTA_TIME__;
if (scanf("%d", &key) != 1)
return;
getchar();
__START_DELTA_TIME__;
Node node = search(root, key);
__CALC_DELTA_TIME__;
__print_delta_time(stdout);
if (node) {
print_node(node);
} else {
msg(NODE_NFOUND_MSG "\n");
}
}
static inline void handle_cmd(char cmd, Node *root)
{
switch (cmd) {
case 'h' : msg(COMMANDS); break;
case 'i' : insert_nodes(root); break;
case 'p' : print(*root, 0); break;
case 's' : search_node(*root); break;
case 'q' : exit_program(EXIT_SUCCESS); break;
case 'r' : clear(*root); break;
case 't' : run_file_test(root); break;
case '\n' : break;
default : throw(UNKNOWN_CMD_ERR); break;
}
}
int main(void)
{
char input;
Node root = NULL;
msg(WELCOME_MSG);
while (scanf("%c", &input) == 1)
handle_cmd(input, &root);
return 0;
}