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 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include <stdio.h>
28#include <errno.h>
29#include <string.h>
30#include <sys/types.h>
31
32#include "nilorea/n_common.h"
33#include "nilorea/n_log.h"
34#include "nilorea/n_str.h"
35#include "nilorea/n_trees.h"
36
37#ifndef __windows__
38#include <sys/wait.h>
39#endif
40
41void usage(void) {
42 fprintf(stderr,
43 " -v version\n"
44 " -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
45 " -h help\n");
46}
47
48void process_args(int argc, char** argv) {
49 int getoptret = 0,
50 log_level = LOG_DEBUG; /* default log level */
51
52 /* Arguments optionnels */
53 /* -v version
54 * -V log level
55 * -h help
56 */
57 while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
58 switch (getoptret) {
59 case 'v':
60 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
61 exit(1);
62 case 'V':
63 if (!strcmp("LOG_NULL", optarg))
65 else if (!strcmp("LOG_NOTICE", optarg))
67 else if (!strcmp("LOG_INFO", optarg))
69 else if (!strcmp("LOG_ERR", optarg))
71 else if (!strcmp("LOG_DEBUG", optarg))
73 else {
74 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
75 exit(-1);
76 }
77 break;
78 default:
79 case '?': {
80 if (optopt == 'V') {
81 fprintf(stderr, "\n Missing log level\n");
82 } else if (optopt == 'p') {
83 fprintf(stderr, "\n Missing port\n");
84 } else if (optopt != 's') {
85 fprintf(stderr, "\n Unknow missing option %c\n", optopt);
86 }
87 usage();
88 exit(1);
89 }
90 case 'h': {
91 usage();
92 exit(1);
93 }
94 }
95 }
97} /* void process_args( ... ) */
98
99int main(int argc, char** argv) {
100 /* processing args and set log_level */
101 process_args(argc, argv);
102
103 /* n-ary tree: new_tree, tree_create_node, tree_insert_child, tree_delete_node, tree_destroy */
104 n_log(LOG_NOTICE, "--- N-ary Tree ---");
105 TREE* tree = new_tree();
106 __n_assert(tree, return 1);
107
108 NODE_DATA root_data;
109 root_data.type = 0;
110 root_data.value.ival = 1;
111 tree->root = tree_create_node(root_data, NULL);
112
113 NODE_DATA child1_data;
114 child1_data.type = 0;
115 child1_data.value.ival = 10;
116 TREE_NODE* child1 = tree_create_node(child1_data, NULL);
117 tree_insert_child(tree->root, child1);
118
119 NODE_DATA child2_data;
120 child2_data.type = 0;
121 child2_data.value.ival = 20;
122 TREE_NODE* child2 = tree_create_node(child2_data, NULL);
123 tree_insert_child(tree->root, child2);
124
125 NODE_DATA grandchild_data;
126 grandchild_data.type = 0;
127 grandchild_data.value.ival = 100;
128 TREE_NODE* grandchild = tree_create_node(grandchild_data, NULL);
129 tree_insert_child(child1, grandchild);
130
131 n_log(LOG_INFO, "Tree root: %d", tree->root->data.value.ival);
132 n_log(LOG_INFO, "Tree nb_nodes: %zu", tree->nb_nodes);
133
134 tree_delete_node(tree, grandchild);
135 n_log(LOG_INFO, "After delete grandchild, nb_nodes: %zu", tree->nb_nodes);
136
137 tree_destroy(&tree);
138 n_log(LOG_NOTICE, "N-ary tree destroyed");
139
140 /* quadtree: create_quadtree, create_node, insert, search, free_quadtree */
141 n_log(LOG_NOTICE, "--- Quad Tree ---");
143
144 int data1 = 100;
145 int data2 = 200;
146
147 COORD_VALUE x1, y1, x2, y2;
148 x1.i = 5;
149 y1.i = 5;
150 x2.i = 9;
151 y2.i = 7;
152
153 insert(qt, &(qt->root), x1, y1, &data1);
154 insert(qt, &(qt->root), x2, y2, &data2);
155
156 /* create_node used directly */
157 COORD_VALUE x3, y3;
158 x3.i = 3;
159 y3.i = 3;
160 int data3 = 300;
161 QUADTREE_NODE* manual_node = create_node(x3, y3, &data3);
162 n_log(LOG_INFO, "create_node: data=%d", *(int*)manual_node->data_ptr);
163 Free(manual_node);
164
165 QUADTREE_NODE* result = search(qt, qt->root, x1, y1);
166 if (result && result->data_ptr) {
167 printf("Found node at (");
168 qt->print(result->x);
169 printf(", ");
170 qt->print(result->y);
171 printf(") with data: %d\n", *(int*)result->data_ptr);
172 } else {
173 printf("Node not found or has no data.\n");
174 }
175
176 free_quadtree(qt->root);
177 free(qt);
178
179 /* octree: create_octree, create_octree_node, insert_octree, free_octree */
180 n_log(LOG_NOTICE, "--- Octree ---");
182 __n_assert(ot, return 1);
183
184 int oct_data1 = 500;
185 int oct_data2 = 600;
186 POINT3D p1;
187 p1.x.i = 1;
188 p1.y.i = 2;
189 p1.z.i = 3;
190 POINT3D p2;
191 p2.x.i = 5;
192 p2.y.i = 6;
193 p2.z.i = 7;
194
195 insert_octree(ot, p1, &oct_data1);
196 insert_octree(ot, p2, &oct_data2);
197 n_log(LOG_INFO, "Inserted 2 octree points");
198
199 /* create_octree_node and free_octree_node */
200 POINT3D p3;
201 p3.x.i = 10;
202 p3.y.i = 11;
203 p3.z.i = 12;
204 OCTREE_NODE* oct_node = create_octree_node(p3, NULL);
205 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);
206 free_octree_node(oct_node);
207
208 free_octree(ot);
209 n_log(LOG_NOTICE, "Octree destroyed");
210
211 exit(0);
212}
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
print_func print
pointer to print function
Definition n_trees.h:170
TREE_NODE * root
pointer to first node
Definition n_trees.h:84
size_t nb_nodes
number of nodes in the tree
Definition n_trees.h:88
COORD_VALUE z
z coordinate
Definition n_trees.h:135
COORD_VALUE y
Y coordinate.
Definition n_trees.h:148
QUADTREE_NODE * root
tree list first node
Definition n_trees.h:172
union NODE_DATA_TYPES value
node value
Definition n_trees.h:62
int32_t type
node type
Definition n_trees.h:64
NODE_DATA data
structure holding values for node
Definition n_trees.h:70
COORD_VALUE x
x coordinate
Definition n_trees.h:131
void * data_ptr
Pointer to data, can be NULL.
Definition n_trees.h:152
POINT3D point
Point represented by this node.
Definition n_trees.h:189
int ival
integral type
Definition n_trees.h:48
COORD_VALUE y
y coordinate
Definition n_trees.h:133
COORD_VALUE x
X coordinate.
Definition n_trees.h:146
void free_octree_node(OCTREE_NODE *node)
recursive function to free an OCTREE node and its children
Definition n_trees.c:454
void insert_octree(OCTREE *octree, POINT3D point, void *data_ptr)
Insert a point into the OCTREE.
Definition n_trees.c:442
int tree_insert_child(TREE_NODE *parent, TREE_NODE *child)
insert a child node into the parent node
Definition n_trees.c:85
void free_quadtree(QUADTREE_NODE *root)
Function to free the quad tree.
Definition n_trees.c:344
TREE * new_tree()
create a new TREE
Definition n_trees.c:40
TREE_NODE * tree_create_node(NODE_DATA value, void(*destroy_func)(void *ptr))
create a TREE node
Definition n_trees.c:59
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:322
void free_octree(OCTREE *octree)
free the OCTREE
Definition n_trees.c:467
void tree_destroy(TREE **tree)
destroy a TREE
Definition n_trees.c:154
OCTREE_NODE * create_octree_node(POINT3D point, void *data_ptr)
create and OCTREE node
Definition n_trees.c:361
OCTREE * create_octree(int type)
Create a new OCTREE with a specified coordinate type.
Definition n_trees.c:382
int tree_delete_node(TREE *tree, TREE_NODE *node)
delete a TREE node
Definition n_trees.c:118
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:294
QUADTREE * create_quadtree(int coord_type)
Function to create a new quad tree.
Definition n_trees.c:229
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:270
@ COORD_INT
Definition n_trees.h:106
structure of a TREE node data
Definition n_trees.h:60
structure of an OCTREE
Definition n_trees.h:197
structure of an OCTREE node
Definition n_trees.h:187
Structure for a POINT3D in the 3D space.
Definition n_trees.h:128
structure of a quad tree
Definition n_trees.h:164
structure of a quad tree node
Definition n_trees.h:144
structure of a TREE
Definition n_trees.h:82
structure of a n-ary TREE node
Definition n_trees.h:68
Union to store the coordinate values.
Definition n_trees.h:112
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.
trees module headers