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 * 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
27#ifndef __NILOREA_TREES__
28#define __NILOREA_TREES__
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include "nilorea/n_common.h"
35#include "nilorea/n_str.h"
36#include "nilorea/n_log.h"
37#include "nilorea/n_list.h"
38#include "nilorea/n_hash.h"
39#include "nilorea/n_3d.h"
40
49 int ival;
51 double fval;
53 void* ptr;
55 char* string;
58};
59
61typedef struct NODE_DATA {
65 int32_t type;
66} NODE_DATA;
67
81
83typedef struct TREE {
87 pthread_rwlock_t rwlock;
89 size_t nb_nodes;
91 size_t height;
92} TREE;
93
95TREE* new_tree();
97TREE_NODE* tree_create_node(NODE_DATA value, void (*destroy_func)(void* ptr));
99int tree_insert_child(TREE_NODE* parent, TREE_NODE* child);
101int tree_delete_node(TREE* tree, TREE_NODE* node);
103void tree_destroy(TREE** tree);
104
111
113typedef union {
114 int i;
115 float f;
116 double d;
118
120typedef struct POINT2D {
126} POINT2D;
127
129typedef struct POINT3D {
137} POINT3D;
138
142typedef void (*print_func)(COORD_VALUE val);
143
163
175
177QUADTREE* create_quadtree(int coord_type);
179QUADTREE_NODE* create_node(COORD_VALUE x, COORD_VALUE y, void* data_ptr);
183void insert(QUADTREE* qt, QUADTREE_NODE** root, COORD_VALUE x, COORD_VALUE y, void* data_ptr);
185void free_quadtree(QUADTREE_NODE* root);
186
196
198typedef struct {
203} OCTREE;
204
206OCTREE* create_octree(int type);
208OCTREE_NODE* create_octree_node(POINT3D point, void* data_ptr);
210void insert_octree(OCTREE* OCTREE, POINT3D point, void* data_ptr);
212void free_octree_node(OCTREE_NODE* node);
215
216#ifdef __cplusplus
217}
218#endif
219
224#endif // header guard
Structure of a generic LIST container.
Definition n_list.h:59
Structure of a generic list node.
Definition n_list.h:44
A box including a string and his lenght.
Definition n_str.h:61
POINT2D point
X,Y point.
Definition n_trees.h:151
print_func print
pointer to print function
Definition n_trees.h:171
compare_func compare
pointer to comparison function
Definition n_trees.h:169
TREE_NODE * root
pointer to first node
Definition n_trees.h:85
pthread_rwlock_t rwlock
mutex for thread safety (optional)
Definition n_trees.h:87
struct QUADTREE_NODE * nw
North-West child.
Definition n_trees.h:155
char * string
char *type
Definition n_trees.h:55
int coord_type
type of coordinate used in the quad tree
Definition n_trees.h:167
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
double d
Definition n_trees.h:116
void * data_ptr
Pointer to additional data, can be NULL.
Definition n_trees.h:192
OCTREE_NODE * root
tree list first node
Definition n_trees.h:200
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
COORD_VALUE x
x coordinate
Definition n_trees.h:123
int32_t type
node type
Definition n_trees.h:65
NODE_DATA data
structure holding values for node
Definition n_trees.h:71
struct QUADTREE_NODE * sw
South-West child.
Definition n_trees.h:159
COORD_VALUE x
x coordinate
Definition n_trees.h:132
float f
Definition n_trees.h:115
COORD_VALUE y
y coordinate
Definition n_trees.h:125
double fval
double type
Definition n_trees.h:51
void * ptr
pointer type
Definition n_trees.h:53
void * data_ptr
Pointer to data, can be NULL.
Definition n_trees.h:153
struct QUADTREE_NODE * se
South-East child.
Definition n_trees.h:161
POINT3D point
Point represented by this node.
Definition n_trees.h:190
struct QUADTREE_NODE * ne
North-East child.
Definition n_trees.h:157
void(* destroy_func)(void *ptr)
value destructor if of type ptr and specified, else a simple free will be used
Definition n_trees.h:73
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
struct OCTREE_NODE * children[8]
Child nodes.
Definition n_trees.h:194
LIST_NODE * parent_list_node
pointer to parent container of the TREE_NODE, LIST_NODE
Definition n_trees.h:77
N_STR * nstr
N_STR *type.
Definition n_trees.h:57
struct TREE_NODE * parent
pointer to parent
Definition n_trees.h:75
size_t height
height of the tree
Definition n_trees.h:91
LIST * children
ordered list of children
Definition n_trees.h:79
int coord_type
Coordinate type for the entire tree.
Definition n_trees.h:202
int(* compare_func)(COORD_VALUE a, COORD_VALUE b)
function pointer types for comparison
Definition n_trees.h:140
void free_octree_node(OCTREE_NODE *node)
recursively 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 with data into the octree
Definition n_trees.c:443
int tree_insert_child(TREE_NODE *parent, TREE_NODE *child)
insert a child node under the given parent node
Definition n_trees.c:86
void free_quadtree(QUADTREE_NODE *root)
recursively free all nodes of a quadtree
Definition n_trees.c:345
TREE * new_tree()
create a new empty n-ary TREE
Definition n_trees.c:41
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:60
void(* print_func)(COORD_VALUE val)
function pointer types for debug print
Definition n_trees.h:142
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:323
COORD_TYPE
Enum for coordinate types.
Definition n_trees.h:106
void free_octree(OCTREE *OCTREE)
free the entire octree and its root
Definition n_trees.c:468
void tree_destroy(TREE **tree)
destroy a TREE and all its nodes, set pointer to NULL
Definition n_trees.c:155
OCTREE_NODE * create_octree_node(POINT3D point, void *data_ptr)
create a new octree node with position and data pointer
Definition n_trees.c:362
OCTREE * create_octree(int type)
create a new octree for the given coordinate type
Definition n_trees.c:383
int tree_delete_node(TREE *tree, TREE_NODE *node)
delete a TREE_NODE and all its children from the tree
Definition n_trees.c:119
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:295
QUADTREE * create_quadtree(int coord_type)
create a new quadtree for the given coordinate type
Definition n_trees.c:230
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:271
@ COORD_INT
Definition n_trees.h:107
@ COORD_DOUBLE
Definition n_trees.h:109
@ COORD_FLOAT
Definition n_trees.h:108
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 POINT2D in the 2D space.
Definition n_trees.h:120
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
union of the possibles data values of a TREE node
Definition n_trees.h:47
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.