Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_trees.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include <sys/types.h>
32
33#include "nilorea/n_common.h"
34#include "nilorea/n_log.h"
35#include "nilorea/n_str.h"
36#include "nilorea/n_trees.h"
37
38#ifndef __windows__
39#include <sys/wait.h>
40#endif
41
42void usage(void) {
43 fprintf(stderr,
44 " -v version\n"
45 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
46 " -h help\n");
47}
48
49void process_args(int argc, char** argv) {
50 int getoptret = 0,
51 log_level = LOG_DEBUG; /* default log level */
52
53 /* Arguments optionnels */
54 /* -v version
55 * -V log level
56 * -h help
57 */
58 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
59 switch (getoptret) {
60 case 'v':
61 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
62 exit(1);
63 case 'V':
64 if (!strcmp("LOG_NULL", optarg))
66 else if (!strcmp("LOG_NOTICE", optarg))
68 else if (!strcmp("LOG_INFO", optarg))
70 else if (!strcmp("LOG_ERR", optarg))
72 else if (!strcmp("LOG_DEBUG", optarg))
74 else {
75 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
76 exit(-1);
77 }
78 break;
79 default:
80 case '?': {
81 if (optopt == 'V') {
82 fprintf(stderr, "\n Missing log level\n");
83 } else if (optopt == 'p') {
84 fprintf(stderr, "\n Missing port\n");
85 } else if (optopt != 's') {
86 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
87 }
88 usage();
89 exit(1);
90 }
91 case 'h': {
92 usage();
93 exit(1);
94 }
95 }
96 }
98} /* void process_args( ... ) */
99
100int main(int argc, char** argv) {
101 /* processing args and set log_level */
102 process_args(argc, argv);
103
104 /* n-ary tree: new_tree, tree_create_node, tree_insert_child, tree_delete_node, tree_destroy */
105 n_log(LOG_NOTICE, "--- N-ary Tree ---");
106 TREE* tree = new_tree();
107 __n_assert(tree, return 1);
108
109 NODE_DATA root_data;
110 root_data.type = 0;
111 root_data.value.ival = 1;
112 tree->root = tree_create_node(root_data, NULL);
113
114 NODE_DATA child1_data;
115 child1_data.type = 0;
116 child1_data.value.ival = 10;
117 TREE_NODE* child1 = tree_create_node(child1_data, NULL);
118 tree_insert_child(tree->root, child1);
119
120 NODE_DATA child2_data;
121 child2_data.type = 0;
122 child2_data.value.ival = 20;
123 TREE_NODE* child2 = tree_create_node(child2_data, NULL);
124 tree_insert_child(tree->root, child2);
125
126 NODE_DATA grandchild_data;
127 grandchild_data.type = 0;
128 grandchild_data.value.ival = 100;
129 TREE_NODE* grandchild = tree_create_node(grandchild_data, NULL);
130 tree_insert_child(child1, grandchild);
131
132 n_log(LOG_INFO, "Tree root: %d", tree->root->data.value.ival);
133 n_log(LOG_INFO, "Tree nb_nodes: %zu", tree->nb_nodes);
134
135 tree_delete_node(tree, grandchild);
136 n_log(LOG_INFO, "After delete grandchild, nb_nodes: %zu", tree->nb_nodes);
137
138 tree_destroy(&tree);
139 n_log(LOG_NOTICE, "N-ary tree destroyed");
140
141 /* quadtree: create_quadtree, create_node, insert, search, free_quadtree */
142 n_log(LOG_NOTICE, "--- Quad Tree ---");
144
145 int data1 = 100;
146 int data2 = 200;
147
148 COORD_VALUE x1, y1, x2, y2;
149 x1.i = 5;
150 y1.i = 5;
151 x2.i = 9;
152 y2.i = 7;
153
154 insert(qt, &(qt->root), x1, y1, &data1);
155 insert(qt, &(qt->root), x2, y2, &data2);
156
157 /* create_node used directly */
158 COORD_VALUE x3, y3;
159 x3.i = 3;
160 y3.i = 3;
161 int data3 = 300;
162 QUADTREE_NODE* manual_node = create_node(x3, y3, &data3);
163 n_log(LOG_INFO, "create_node: data=%d", *(int*)manual_node->data_ptr);
164 Free(manual_node);
165
166 QUADTREE_NODE* result = search(qt, qt->root, x1, y1);
167 if (result && result->data_ptr) {
168 printf("Found node at (");
169 qt->print(result->x);
170 printf(", ");
171 qt->print(result->y);
172 printf(") with data: %d\n", *(int*)result->data_ptr);
173 } else {
174 printf("Node not found or has no data.\n");
175 }
176
177 free_quadtree(qt->root);
178 free(qt);
179
180 /* octree: create_octree, create_octree_node, insert_octree, free_octree */
181 n_log(LOG_NOTICE, "--- Octree ---");
183 __n_assert(ot, return 1);
184
185 int oct_data1 = 500;
186 int oct_data2 = 600;
187 POINT3D p1;
188 p1.x.i = 1;
189 p1.y.i = 2;
190 p1.z.i = 3;
191 POINT3D p2;
192 p2.x.i = 5;
193 p2.y.i = 6;
194 p2.z.i = 7;
195
196 insert_octree(ot, p1, &oct_data1);
197 insert_octree(ot, p2, &oct_data2);
198 n_log(LOG_INFO, "Inserted 2 octree points");
199
200 /* create_octree_node and free_octree_node */
201 POINT3D p3;
202 p3.x.i = 10;
203 p3.y.i = 11;
204 p3.z.i = 12;
205 OCTREE_NODE* oct_node = create_octree_node(p3, NULL);
206 n_log(LOG_INFO, "create_octree_node at (%d,%d,%d)", oct_node->point.x.i, oct_node->point.y.i, oct_node->point.z.i);
207 free_octree_node(oct_node);
208
209 free_octree(ot);
210 n_log(LOG_NOTICE, "Octree destroyed");
211
212 exit(0);
213}
static void usage(void)
int main(void)
void process_args(int argc, char **argv)
Definition ex_common.c:48
int getoptret
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:60
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
print_func print
pointer to print function
Definition n_trees.h:171
TREE_NODE * root
pointer to first node
Definition n_trees.h:85
size_t nb_nodes
number of nodes in the tree
Definition n_trees.h:89
COORD_VALUE z
z coordinate
Definition n_trees.h:136
COORD_VALUE y
Y coordinate.
Definition n_trees.h:149
QUADTREE_NODE * root
tree list first node
Definition n_trees.h:173
union NODE_DATA_TYPES value
node value
Definition n_trees.h:63
int32_t type
node type
Definition n_trees.h:65
NODE_DATA data
structure holding values for node
Definition n_trees.h:71
COORD_VALUE x
x coordinate
Definition n_trees.h:132
void * data_ptr
Pointer to data, can be NULL.
Definition n_trees.h:153
POINT3D point
Point represented by this node.
Definition n_trees.h:190
int ival
integral type
Definition n_trees.h:49
COORD_VALUE y
y coordinate
Definition n_trees.h:134
COORD_VALUE x
X coordinate.
Definition n_trees.h:147
void free_octree_node(OCTREE_NODE *node)
recursive function to free an OCTREE node and its children
Definition n_trees.c:455
void insert_octree(OCTREE *octree, POINT3D point, void *data_ptr)
Insert a point into the OCTREE.
Definition n_trees.c:443
int tree_insert_child(TREE_NODE *parent, TREE_NODE *child)
insert a child node into the parent node
Definition n_trees.c:86
void free_quadtree(QUADTREE_NODE *root)
Function to free the quad tree.
Definition n_trees.c:345
TREE * new_tree()
create a new TREE
Definition n_trees.c:41
TREE_NODE * tree_create_node(NODE_DATA value, void(*destroy_func)(void *ptr))
create a TREE node
Definition n_trees.c:60
QUADTREE_NODE * search(QUADTREE *qt, QUADTREE_NODE *root, COORD_VALUE x, COORD_VALUE y)
Function to search for a point in the quad tree.
Definition n_trees.c:323
void free_octree(OCTREE *octree)
free the OCTREE
Definition n_trees.c:468
void tree_destroy(TREE **tree)
destroy a TREE
Definition n_trees.c:155
OCTREE_NODE * create_octree_node(POINT3D point, void *data_ptr)
create and OCTREE node
Definition n_trees.c:362
OCTREE * create_octree(int type)
Create a new OCTREE with a specified coordinate type.
Definition n_trees.c:383
int tree_delete_node(TREE *tree, TREE_NODE *node)
delete a TREE node
Definition n_trees.c:119
void insert(QUADTREE *qt, QUADTREE_NODE **root, COORD_VALUE x, COORD_VALUE y, void *data_ptr)
Function to insert a point into the quad tree.
Definition n_trees.c:295
QUADTREE * create_quadtree(int coord_type)
Function to create a new quad tree.
Definition n_trees.c:230
QUADTREE_NODE * create_node(COORD_VALUE x, COORD_VALUE y, void *data_ptr)
function to create a new quad tree node
Definition n_trees.c:271
@ COORD_INT
Definition n_trees.h:107
structure of a TREE node data
Definition n_trees.h:61
structure of an OCTREE
Definition n_trees.h:198
structure of an OCTREE node
Definition n_trees.h:188
Structure for a POINT3D in the 3D space.
Definition n_trees.h:129
structure of a quad tree
Definition n_trees.h:165
structure of a quad tree node
Definition n_trees.h:145
structure of a TREE
Definition n_trees.h:83
structure of a n-ary TREE node
Definition n_trees.h:69
Union to store the coordinate values.
Definition n_trees.h:113
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.
trees module headers