Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_trees.h
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
26#ifndef __NILOREA_TREES__
27#define __NILOREA_TREES__
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#include "nilorea/n_common.h"
34#include "nilorea/n_str.h"
35#include "nilorea/n_log.h"
36#include "nilorea/n_list.h"
37#include "nilorea/n_hash.h"
38#include "nilorea/n_3d.h"
39
48 int ival;
50 double fval;
52 void* ptr;
54 char* string;
57};
58
60typedef struct NODE_DATA {
64 int32_t type;
65} NODE_DATA;
66
80
82typedef struct TREE {
86 pthread_rwlock_t rwlock;
88 size_t nb_nodes;
90 size_t height;
91} TREE;
92
94TREE* new_tree();
96TREE_NODE* tree_create_node(NODE_DATA value, void (*destroy_func)(void* ptr));
98int tree_insert_child(TREE_NODE* parent, TREE_NODE* child);
100int tree_delete_node(TREE* tree, TREE_NODE* node);
102void tree_destroy(TREE** tree);
103
110
112typedef union {
113 int i;
114 float f;
115 double d;
117
119typedef struct POINT2D {
125} POINT2D;
126
128typedef struct POINT3D {
136} POINT3D;
137
141typedef void (*print_func)(COORD_VALUE val);
142
162
174
176QUADTREE* create_quadtree(int coord_type);
178QUADTREE_NODE* create_node(COORD_VALUE x, COORD_VALUE y, void* data_ptr);
182void insert(QUADTREE* qt, QUADTREE_NODE** root, COORD_VALUE x, COORD_VALUE y, void* data_ptr);
184void free_quadtree(QUADTREE_NODE* root);
185
195
197typedef struct {
202} OCTREE;
203
205OCTREE* create_octree(int type);
207OCTREE_NODE* create_octree_node(POINT3D point, void* data_ptr);
209void insert_octree(OCTREE* OCTREE, POINT3D point, void* data_ptr);
211void free_octree_node(OCTREE_NODE* node);
214
215#ifdef __cplusplus
216}
217#endif
218
223#endif // header guard
Structure of a generic LIST container.
Definition n_list.h:58
Structure of a generic list node.
Definition n_list.h:43
A box including a string and his lenght.
Definition n_str.h:60
POINT2D point
X,Y point.
Definition n_trees.h:150
print_func print
pointer to print function
Definition n_trees.h:170
compare_func compare
pointer to comparison function
Definition n_trees.h:168
TREE_NODE * root
pointer to first node
Definition n_trees.h:84
pthread_rwlock_t rwlock
mutex for thread safety (optional)
Definition n_trees.h:86
struct QUADTREE_NODE * nw
North-West child.
Definition n_trees.h:154
char * string
char *type
Definition n_trees.h:54
int coord_type
type of coordinate used in the quad tree
Definition n_trees.h:166
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
double d
Definition n_trees.h:115
void * data_ptr
Pointer to additional data, can be NULL.
Definition n_trees.h:191
OCTREE_NODE * root
tree list first node
Definition n_trees.h:199
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
COORD_VALUE x
x coordinate
Definition n_trees.h:122
int32_t type
node type
Definition n_trees.h:64
NODE_DATA data
structure holding values for node
Definition n_trees.h:70
struct QUADTREE_NODE * sw
South-West child.
Definition n_trees.h:158
COORD_VALUE x
x coordinate
Definition n_trees.h:131
float f
Definition n_trees.h:114
COORD_VALUE y
y coordinate
Definition n_trees.h:124
double fval
double type
Definition n_trees.h:50
void * ptr
pointer type
Definition n_trees.h:52
void * data_ptr
Pointer to data, can be NULL.
Definition n_trees.h:152
struct QUADTREE_NODE * se
South-East child.
Definition n_trees.h:160
POINT3D point
Point represented by this node.
Definition n_trees.h:189
struct QUADTREE_NODE * ne
North-East child.
Definition n_trees.h:156
void(* destroy_func)(void *ptr)
value destructor if of type ptr and specified, else a simple free will be used
Definition n_trees.h:72
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
struct OCTREE_NODE * children[8]
Child nodes.
Definition n_trees.h:193
LIST_NODE * parent_list_node
pointer to parent container of the TREE_NODE, LIST_NODE
Definition n_trees.h:76
N_STR * nstr
N_STR *type.
Definition n_trees.h:56
struct TREE_NODE * parent
pointer to parent
Definition n_trees.h:74
size_t height
height of the tree
Definition n_trees.h:90
LIST * children
ordered list of children
Definition n_trees.h:78
int coord_type
Coordinate type for the entire tree.
Definition n_trees.h:201
int(* compare_func)(COORD_VALUE a, COORD_VALUE b)
function pointer types for comparison
Definition n_trees.h:139
void free_octree_node(OCTREE_NODE *node)
recursively 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 with data into the octree
Definition n_trees.c:442
int tree_insert_child(TREE_NODE *parent, TREE_NODE *child)
insert a child node under the given parent node
Definition n_trees.c:85
void free_quadtree(QUADTREE_NODE *root)
recursively free all nodes of a quadtree
Definition n_trees.c:344
TREE * new_tree()
create a new empty n-ary TREE
Definition n_trees.c:40
TREE_NODE * tree_create_node(NODE_DATA value, void(*destroy_func)(void *ptr))
create a TREE_NODE with the given value and optional destructor
Definition n_trees.c:59
void(* print_func)(COORD_VALUE val)
function pointer types for debug print
Definition n_trees.h:141
QUADTREE_NODE * search(QUADTREE *qt, QUADTREE_NODE *root, COORD_VALUE x, COORD_VALUE y)
search for a point in the quadtree, return matching node or NULL
Definition n_trees.c:322
COORD_TYPE
Enum for coordinate types.
Definition n_trees.h:105
void free_octree(OCTREE *OCTREE)
free the entire octree and its root
Definition n_trees.c:467
void tree_destroy(TREE **tree)
destroy a TREE and all its nodes, set pointer to NULL
Definition n_trees.c:154
OCTREE_NODE * create_octree_node(POINT3D point, void *data_ptr)
create a new octree node with position and data pointer
Definition n_trees.c:361
OCTREE * create_octree(int type)
create a new octree for the given coordinate type
Definition n_trees.c:382
int tree_delete_node(TREE *tree, TREE_NODE *node)
delete a TREE_NODE and all its children from the tree
Definition n_trees.c:118
void insert(QUADTREE *qt, QUADTREE_NODE **root, COORD_VALUE x, COORD_VALUE y, void *data_ptr)
insert a point with data into the quadtree
Definition n_trees.c:294
QUADTREE * create_quadtree(int coord_type)
create a new quadtree for the given coordinate type
Definition n_trees.c:229
QUADTREE_NODE * create_node(COORD_VALUE x, COORD_VALUE y, void *data_ptr)
create a new quadtree node with position and data pointer
Definition n_trees.c:270
@ COORD_INT
Definition n_trees.h:106
@ COORD_DOUBLE
Definition n_trees.h:108
@ COORD_FLOAT
Definition n_trees.h:107
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 POINT2D in the 2D space.
Definition n_trees.h:119
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
union of the possibles data values of a TREE node
Definition n_trees.h:46
Simple 3D movement simulation.
Common headers and low-level functions & define.
Hash functions and table.
List structures and definitions.
Generic log system.
N_STR and string function declaration.