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
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
#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)
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