Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_trees.c

Nilorea Library tree test.

Nilorea Library tree test

Author
Castagnier Mickael
Version
1.0
Date
03/01/2025
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#ifndef __windows__
#include <sys/wait.h>
#endif
void usage(void) {
fprintf(stderr,
" -v version\n"
" -V log level: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n"
" -h help\n");
}
void process_args(int argc, char** argv) {
int getoptret = 0,
log_level = LOG_DEBUG; /* default log level */
/* Arguments optionnels */
/* -v version
* -V log level
* -h help
*/
while ((getoptret = getopt(argc, argv, "hvV:")) != EOF) {
switch (getoptret) {
case 'v':
fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
exit(1);
case 'V':
if (!strcmp("LOG_NULL", optarg))
else if (!strcmp("LOG_NOTICE", optarg))
else if (!strcmp("LOG_INFO", optarg))
else if (!strcmp("LOG_ERR", optarg))
else if (!strcmp("LOG_DEBUG", optarg))
else {
fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
exit(-1);
}
break;
default:
case '?': {
if (optopt == 'V') {
fprintf(stderr, "\n Missing log level\n");
} else if (optopt == 'p') {
fprintf(stderr, "\n Missing port\n");
} else if (optopt != 's') {
fprintf(stderr, "\n Unknow missing option %c\n", optopt);
}
usage();
exit(1);
}
case 'h': {
usage();
exit(1);
}
}
}
} /* void process_args( ... ) */
int main(int argc, char** argv) {
/* processing args and set log_level */
process_args(argc, argv);
/* n-ary tree: new_tree, tree_create_node, tree_insert_child, tree_delete_node, tree_destroy */
n_log(LOG_NOTICE, "--- N-ary Tree ---");
TREE* tree = new_tree();
__n_assert(tree, return 1);
NODE_DATA root_data;
root_data.type = 0;
root_data.value.ival = 1;
tree->root = tree_create_node(root_data, NULL);
NODE_DATA child1_data;
child1_data.type = 0;
child1_data.value.ival = 10;
TREE_NODE* child1 = tree_create_node(child1_data, NULL);
tree_insert_child(tree->root, child1);
NODE_DATA child2_data;
child2_data.type = 0;
child2_data.value.ival = 20;
TREE_NODE* child2 = tree_create_node(child2_data, NULL);
tree_insert_child(tree->root, child2);
NODE_DATA grandchild_data;
grandchild_data.type = 0;
grandchild_data.value.ival = 100;
TREE_NODE* grandchild = tree_create_node(grandchild_data, NULL);
tree_insert_child(child1, grandchild);
n_log(LOG_INFO, "Tree root: %d", tree->root->data.value.ival);
n_log(LOG_INFO, "Tree nb_nodes: %zu", tree->nb_nodes);
tree_delete_node(tree, grandchild);
n_log(LOG_INFO, "After delete grandchild, nb_nodes: %zu", tree->nb_nodes);
tree_destroy(&tree);
n_log(LOG_NOTICE, "N-ary tree destroyed");
/* quadtree: create_quadtree, create_node, insert, search, free_quadtree */
n_log(LOG_NOTICE, "--- Quad Tree ---");
int data1 = 100;
int data2 = 200;
COORD_VALUE x1, y1, x2, y2;
x1.i = 5;
y1.i = 5;
x2.i = 9;
y2.i = 7;
insert(qt, &(qt->root), x1, y1, &data1);
insert(qt, &(qt->root), x2, y2, &data2);
/* create_node used directly */
COORD_VALUE x3, y3;
x3.i = 3;
y3.i = 3;
int data3 = 300;
QUADTREE_NODE* manual_node = create_node(x3, y3, &data3);
n_log(LOG_INFO, "create_node: data=%d", *(int*)manual_node->data_ptr);
Free(manual_node);
QUADTREE_NODE* result = search(qt, qt->root, x1, y1);
if (result && result->data_ptr) {
printf("Found node at (");
qt->print(result->x);
printf(", ");
qt->print(result->y);
printf(") with data: %d\n", *(int*)result->data_ptr);
} else {
printf("Node not found or has no data.\n");
}
free(qt);
/* octree: create_octree, create_octree_node, insert_octree, free_octree */
n_log(LOG_NOTICE, "--- Octree ---");
__n_assert(ot, return 1);
int oct_data1 = 500;
int oct_data2 = 600;
POINT3D p1;
p1.x.i = 1;
p1.y.i = 2;
p1.z.i = 3;
POINT3D p2;
p2.x.i = 5;
p2.y.i = 6;
p2.z.i = 7;
insert_octree(ot, p1, &oct_data1);
insert_octree(ot, p2, &oct_data2);
n_log(LOG_INFO, "Inserted 2 octree points");
/* create_octree_node and free_octree_node */
POINT3D p3;
p3.x.i = 10;
p3.y.i = 11;
p3.z.i = 12;
OCTREE_NODE* oct_node = create_octree_node(p3, NULL);
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);
free_octree_node(oct_node);
n_log(LOG_NOTICE, "Octree destroyed");
exit(0);
}
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