-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud_test.c
429 lines (322 loc) · 12.3 KB
/
crud_test.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <stdio.h>
#include "kdtree.h"
#include <sys/time.h>
#include <math.h>
#include <time.h>
/*Copyright 2020, by the California Institute of Technology.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File: kdtree.c
* Author: anaim (aryan.e.naim@jpl.nasa.gov)
*
* Example of libray usage and test of add,search(single node_, delete, update
* and traverse.
*
* IMPORTANT: Must call kdtree_alloc() & kdtree_init() once before using the API.
*
* In order to add points call kd_tree_add_points()
* In order to update a point call kd_tree_update_point()
* In order to search for a single point call kd_tree_search_data_point()
* In order to delete a point call kd_tree_delete_data_point()
* In order to traverse the entire tree for debugging call
* kd_tree_in_order_traversal() the call kd_tree_print_data_for_debug().
* In order to cleanup call kdtree_free().
*/
int main(int argc, char **argv) {
/*assuming 3 dimensions*/
int max_rows = 1000;
/*assuming 1000 datapoints*/
int max_cols = 3;
kdtree_t* kdtree = NULL;
/*allocate heap memory */
kdtree = kdtree_alloc(max_rows, max_cols);
assert(kdtree);
printf("alloc ok \n");
/*init heap memory*/
kdtree_init(kdtree);
printf("init ok \n");
/*enable extra debugging output */
kd_tree_is_debug_on(1);
/*set rebuild thresh hold, to rebalance the tree when tree reaches that
threshold. Default is 2 anyways.*/
kd_tree_set_rebuild_threshold(2);
/*insert some points...*/
printf("Adding kd_tree nodes & Search:\n");
// float point [] = {-100, -100, -100};
float point [] ={-53.148998,0.000000,9.000000};
/*call kd_tree_add_points*/
kd_tree_node* root = kd_tree_get_root();
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n ", point[0], point[1], point[2]);
}
point[0] = 7.999700;
point[1] = 0.069812;
point[2] = 8.000000;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found\n ", point[0], point[1], point[2]);
}
point[0] =7.998780;
point[1] = 0.139619;
point[2] = 8.000000;
kd_tree_add_points(&root,point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found\n ", point[0], point[1], point[2]);
}
point[0] = 7.997260;
point[1] = 0.209416;
point[2] = 8.000000;
kd_tree_add_points(&root,point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n", point[0], point[1], point[2]);
}
point[0] = 7.995130;
point[1] = 0.279196;
point[2] = 8.000000;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n ", point[0], point[1], point[2]);
}
point[0] = 7.992390;
point[1] = 0.348955;
point[2] = 8.000000;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n", point[0], point[1], point[2]);
}
point[0] = 8.987670;
point[1] = 0.471024;
point[2] = 9.000000;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n ", point[0], point[1], point[2]);
}
point[0] = 8.983210;
point[1] = 0.549437;
point[2] = 9.000000;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n ", point[0], point[1], point[2]);
}
point[0] = 7.980510;
point[1] = 0.558052;
point[2] = 8.000000;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n ", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n ", point[0], point[1], point[2]);
}
point[0] = 3;
point[1] = 3;
point[2] = 3;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n", point[0], point[1], point[2]);
}
point[0] = 4;
point[1] = 4;
point[2] = 4;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n", point[0], point[1], point[2]);
}
point[0] = 5;
point[1] = 5;
point[2] = 5;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found!\n", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n", point[0], point[1], point[2]);
}
point[0] = 100;
point[1] = 100;
point[2] = 100;
kd_tree_add_points(&root, point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, point)) {
printf("%f,%f,%f Found\n", point[0], point[1], point[2]);
} else {
printf("%f,%f,%f Not Found!\n", point[0], point[1], point[2]);
}
printf("Search of NON-existent data:\n");
float query_point [] = {10, 10, 10};
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n! ", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found\n! ",
query_point[0], query_point[1], query_point[2]);
}
/*-20,-20,-20 is not added so it should not be found */
query_point [0] = -20;
query_point [1] = -20;
query_point [2] = -20;
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found!\n",
query_point[0], query_point[1], query_point[2]);
}
query_point [0] = -101;
query_point [1] = -101;
query_point [2] = -101;
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found!\n",
query_point[0], query_point[1], query_point[2]);
}
query_point [0] = -1000;
query_point [1] = -1000;
query_point [2] = -1000;
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found!\n",
query_point[0], query_point[1], query_point[2]);
}
/*After delete & change - can we find previously found nodes? */
int column_dimensions = 3;
int result_size = kd_tree_in_order_traversal(root, column_dimensions);
printf("***In order traverse iterative :\n");
kd_tree_print_data_for_debug(node_knn_result_space, column_dimensions, result_size);
/*delete nodes {-1,-1,-1}*/
query_point [0] = 1000;
query_point [1] = 1000;
query_point [2] = 1000;
printf("Delete {%f,%f,%f} \n! ", query_point[0], query_point[1], query_point[2]);
kd_tree_delete_data_point(root, query_point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found!\n", query_point[0], query_point[1], query_point[2]);
}
result_size = kd_tree_in_order_traversal(root, column_dimensions);
printf("***In order traverse iterative :\n");
kd_tree_print_data_for_debug(node_knn_result_space, column_dimensions, result_size);
result_size = kd_tree_in_order_traversal(root, column_dimensions);
printf("***In order traverse iterative :\n");
kd_tree_print_data_for_debug(node_knn_result_space, column_dimensions, result_size);
query_point [0] = 0;
query_point [1] = 0;
query_point [2] = 0;
printf("Delete {%f,%f,%f} \n! ", query_point[0], query_point[1], query_point[2]);
kd_tree_delete_data_point(root, query_point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found!\n", query_point[0], query_point[1], query_point[2]);
}
printf("current_number_of_kd_tree_nodes=%d\n:", kd_tree_get_current_number_of_kd_tree_nodes());
query_point [0] = -1;
query_point [1] = -1;
query_point [2] = -1;
printf("Delete {%f,%f,%f} \n! ", query_point[0], query_point[1], query_point[2]);
kd_tree_delete_data_point(root, query_point);
root = kd_tree_get_root();
if (kd_tree_search_data_point(root, query_point)) {
printf("%f,%f,%f Found\n", query_point[0], query_point[1], query_point[2]);
} else {
printf("%f,%f,%f Not Found!\n", query_point[0], query_point[1], query_point[2]);
}
printf("current_number_of_kd_tree_nodes=%d\n:", kd_tree_get_current_number_of_kd_tree_nodes());
result_size = kd_tree_in_order_traversal(root, column_dimensions);
printf("In order traverse iterative :\n");
kd_tree_print_data_for_debug(node_knn_result_space, column_dimensions, result_size);
printf("\n");
/*start update*/
printf("Attempting to update:\n");
query_point [0] = 100;
query_point [1] = 100;
query_point [2] = 100;
float new_data [] = {200, 200, 200};
printf("update {%f,%f,%f} to {%f,%f,%f}", query_point[0],
query_point[1],
query_point[2],
new_data[0],
new_data[1],
new_data[2]);
int flag = kd_tree_update_point(root, query_point, new_data);
root = kd_tree_get_root();
if (flag) {
printf("Update was successful\n");
} else {
printf("Update was NOT successful!\n");
}
query_point [0] = -100;
query_point [1] = -100;
query_point [2] = -100;
new_data[0] = -200;
new_data[1] = -200;
new_data[2] = -200;
printf("update {%f,%f,%f} to {%f,%f,%f}", query_point[0],
query_point[1],
query_point[2],
new_data[0],
new_data[1],
new_data[2]);
flag = kd_tree_update_point(root, query_point, new_data);
root = kd_tree_get_root();
if (flag) {
printf("Update was successful\n");
} else {
printf("Update was NOT successful!\n");
}
printf("current_number_of_kd_tree_nodes=%d\n:", kd_tree_get_current_number_of_kd_tree_nodes());
result_size = kd_tree_in_order_traversal(root, column_dimensions);
printf("In order traverse iterative :\n");
kd_tree_print_data_for_debug(node_knn_result_space, column_dimensions, result_size);
/*end update*/
kdtree_free(kdtree);
printf("free ok \n");
}