Blender  V2.93
BLI_dlrbTree.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* Double-Linked Red-Black Tree Implementation:
31  *
32  * This is simply a Red-Black Tree implementation whose nodes can later
33  * be arranged + retrieved as elements in a Double-Linked list (i.e. ListBase).
34  * The Red-Black Tree implementation is based on the methods defined by Wikipedia.
35  */
36 
37 /* ********************************************** */
38 /* Data Types and Type Defines */
39 
40 /* Base Structs --------------------------------- */
41 
42 /* Basic Layout for a Node */
43 typedef struct DLRBT_Node {
44  /* ListBase capabilities */
45  struct DLRBT_Node *next, *prev;
46 
47  /* Tree Associativity settings */
48  struct DLRBT_Node *left, *right;
49  struct DLRBT_Node *parent;
50 
51  char tree_col;
52  /* ... for nice alignment, next item should usually be a char too... */
54 
55 /* Red/Black defines for tree_col */
56 typedef enum eDLRBT_Colors {
60 
61 /* -------- */
62 
63 /* The Tree Data */
64 typedef struct DLRBT_Tree {
65  /* ListBase capabilities */
66  void *first, *last; /* these should be based on DLRBT_Node-s */
67 
68  /* Root Node */
69  void *root; /* this should be based on DLRBT_Node-s */
71 
72 /* Callback Types --------------------------------- */
73 
74 /* Return -1, 0, 1 for whether the given data is less than,
75  * equal to, or greater than the given node.
76  * - node: <DLRBT_Node> the node to compare to.
77  * - data: pointer to the relevant data or values stored in the bit-pattern.
78  * dependent on the function.
79  */
80 typedef short (*DLRBT_Comparator_FP)(void *node, void *data);
81 
82 /* Return a new node instance wrapping the given data
83  * - data: Pointer to the relevant data to create a subclass of node from
84  */
85 typedef DLRBT_Node *(*DLRBT_NAlloc_FP)(void *data);
86 
87 /* Update an existing node instance accordingly to be in sync with the given data.
88  * - node: <DLRBT_Node> the node to update.
89  * - data: Pointer to the relevant data or values stored in the bit-pattern.
90  * dependent on the function.
91  */
92 typedef void (*DLRBT_NUpdate_FP)(void *node, void *data);
93 
94 /* ********************************************** */
95 /* Public API */
96 
97 /* ADT Management ------------------------------- */
98 
99 /* Create a new tree, and initialize as necessary */
101 
102 /* Initializes some given trees */
104 
105 /* Free some tree */
107 
108 /* Make sure the tree's Double-Linked list representation is valid */
110 
111 /* Searching ------------------------------------ */
112 
113 /* Find the node which matches or is the closest to the requested node */
115 
116 /* Find the node which exactly matches the required data */
118  DLRBT_Comparator_FP cmp_cb,
119  void *search_data);
120 
121 /* Find the node which occurs immediately before the best matching node */
123  DLRBT_Comparator_FP cmp_cb,
124  void *search_data);
125 
126 /* Find the node which occurs immediately after the best matching node */
128  DLRBT_Comparator_FP cmp_cb,
129  void *search_data);
130 
131 /* Check whether there is a node matching the requested node */
132 short BLI_dlrbTree_contains(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, void *search_data);
133 
134 /* Node Operations (Managed) --------------------- */
135 /* These methods automate the process of adding/removing nodes from the BST,
136  * using the supplied data and callbacks
137  */
138 
139 /* Add the given data to the tree, and return the node added */
140 // NOTE: for duplicates, the update_cb is called (if available), and the existing node is returned
142  DLRBT_Comparator_FP cmp_cb,
143  DLRBT_NAlloc_FP new_cb,
145  void *data);
146 
147 /* Remove the given element from the tree and balance again */
148 // FIXME: this is not implemented yet...
149 // void BLI_dlrbTree_remove(DLRBT_Tree *tree, DLRBT_Node *node);
150 
151 /* Node Operations (Manual) --------------------- */
152 /* These methods require custom code for creating BST nodes and adding them to the
153  * tree in special ways, such that the node can then be balanced.
154  *
155  * It is recommended that these methods are only used where the other method is too cumbersome...
156  */
157 
158 /* Balance the tree after the given node has been added to it
159  * (using custom code, in the Binary Tree way).
160  */
162 
163 /* ********************************************** */
164 
165 #ifdef __cplusplus
166 }
167 #endif
struct DLRBT_Node DLRBT_Node
DLRBT_Node *(* DLRBT_NAlloc_FP)(void *data)
Definition: BLI_dlrbTree.h:85
void BLI_dlrbTree_init(DLRBT_Tree *tree)
Definition: DLRB_tree.c:40
DLRBT_Tree * BLI_dlrbTree_new(void)
Definition: DLRB_tree.c:33
DLRBT_Node * BLI_dlrbTree_add(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, DLRBT_NAlloc_FP new_cb, DLRBT_NUpdate_FP update_cb, void *data)
Definition: DLRB_tree.c:545
void(* DLRBT_NUpdate_FP)(void *node, void *data)
Definition: BLI_dlrbTree.h:92
void BLI_dlrbTree_linkedlist_sync(DLRBT_Tree *tree)
Definition: DLRB_tree.c:113
DLRBT_Node * BLI_dlrbTree_search(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, void *search_data)
Definition: DLRB_tree.c:131
short(* DLRBT_Comparator_FP)(void *node, void *data)
Definition: BLI_dlrbTree.h:80
short BLI_dlrbTree_contains(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, void *search_data)
Definition: DLRB_tree.c:287
eDLRBT_Colors
Definition: BLI_dlrbTree.h:56
@ DLRBT_BLACK
Definition: BLI_dlrbTree.h:57
@ DLRBT_RED
Definition: BLI_dlrbTree.h:58
void BLI_dlrbTree_insert(DLRBT_Tree *tree, DLRBT_Node *node)
Definition: DLRB_tree.c:526
struct DLRBT_Tree DLRBT_Tree
DLRBT_Node * BLI_dlrbTree_search_exact(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, void *search_data)
Definition: DLRB_tree.c:177
void BLI_dlrbTree_free(DLRBT_Tree *tree)
Definition: DLRB_tree.c:66
DLRBT_Node * BLI_dlrbTree_search_next(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, void *search_data)
Definition: DLRB_tree.c:256
DLRBT_Node * BLI_dlrbTree_search_prev(DLRBT_Tree *tree, DLRBT_Comparator_FP cmp_cb, void *search_data)
Definition: DLRB_tree.c:225
OperationNode * node
void * tree
static void update_cb(PBVHNode *node, void *rebuild)
Definition: sculpt_undo.c:120
struct DLRBT_Node * parent
Definition: BLI_dlrbTree.h:49
struct DLRBT_Node * next
Definition: BLI_dlrbTree.h:45
struct DLRBT_Node * left
Definition: BLI_dlrbTree.h:48
struct DLRBT_Node * right
Definition: BLI_dlrbTree.h:48
char tree_col
Definition: BLI_dlrbTree.h:51
struct DLRBT_Node * prev
Definition: BLI_dlrbTree.h:45
void * last
Definition: BLI_dlrbTree.h:66
void * root
Definition: BLI_dlrbTree.h:69
void * first
Definition: BLI_dlrbTree.h:66