2 * Copyright (C) 2024 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
23 struct tree_entry
*children
[2];
24 struct tree_entry
**parent
;
25 uchar_efficient_t color
;
26 uchar_efficient_t idx
;
33 #define RB_IDX_RIGHT 1
37 struct tree_entry
*root
;
40 static inline void tree_init(struct tree
*root
)
45 static inline bool tree_is_empty(struct tree
*root
)
51 void tree_verify_node(struct tree_entry
*);
53 #define tree_verify_node(n) ((void)0)
56 struct tree_insert_position
{
58 struct tree_entry
**link
;
61 static inline struct tree_entry
*tree_find_for_insert(
63 int (*compare
)(const struct tree_entry
*, uintptr_t),
65 struct tree_insert_position
*pos
)
67 struct tree_entry
*p
= root
->root
;
69 pos
->idx
= RB_IDX_ROOT
;
70 pos
->link
= &root
->root
;
80 /*__asm__("" : "=r"(cc) : "0"(cc));*/
83 pos
->link
= &p
->children
[cc
];
90 static inline struct tree_entry
*tree_find(
92 int (*compare
)(const struct tree_entry
*, uintptr_t),
95 return tree_find_for_insert(root
, compare
, key
, NULL
);
98 static inline struct tree_entry
*tree_find_next(
100 int (*compare
)(const struct tree_entry
*, uintptr_t),
103 struct tree_entry
*p
= root
->root
;
104 struct tree_entry
*last_candidate
= NULL
;
115 return last_candidate
;
118 static inline struct tree_entry
*tree_find_best(
120 int (*compare
)(const struct tree_entry
*, uintptr_t),
123 struct tree_entry
*p
= root
->root
;
124 struct tree_entry
*last_candidate
= NULL
;
137 return last_candidate
;
140 void attr_fastcall
tree_insert_after_find_impl(struct tree_entry
*, uchar_efficient_t idx
, struct tree_entry
**link
);
142 static inline void tree_insert_after_find(struct tree_entry
*p
, const struct tree_insert_position
*pos
)
144 tree_insert_after_find_impl(p
, (uchar_efficient_t
)pos
->idx
, pos
->link
);
147 void attr_fastcall
tree_delete(struct tree_entry
*);
149 struct tree_entry
* attr_fastcall
tree_first(struct tree
*);
150 struct tree_entry
* attr_fastcall
tree_next(struct tree_entry
*);
151 struct tree_entry
* attr_fastcall
tree_last(struct tree
*);
153 static inline struct tree_entry
*tree_any(struct tree
*root
)