1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::inner_prelude::*;
pub type DefaultA = XAXIS;
pub const fn default_axis() -> DefaultA {
XAXIS
}
mod oned;
pub use builder::TreeBuilder;
mod builder;
pub struct SplitterEmpty;
impl Splitter for SplitterEmpty {
#[inline(always)]
fn div(&mut self) -> (Self, Self) {
(SplitterEmpty, SplitterEmpty)
}
#[inline(always)]
fn add(&mut self, _: Self, _: Self) {}
}
pub trait Splitter: Sized {
fn div(&mut self) -> (Self, Self);
fn add(&mut self, a: Self, b: Self);
}
trait Sorter: Copy + Clone + Send + Sync {
fn sort(&self, axis: impl Axis, bots: &mut [impl Aabb]);
}
#[derive(Copy, Clone)]
struct DefaultSorter;
impl Sorter for DefaultSorter {
fn sort(&self, axis: impl Axis, bots: &mut [impl Aabb]) {
crate::util::sweeper_update(axis, bots);
}
}
#[derive(Copy, Clone)]
struct NoSorter;
impl Sorter for NoSorter {
fn sort(&self, _axis: impl Axis, _bots: &mut [impl Aabb]) {}
}
const fn nodes_left(depth: usize, height: usize) -> usize {
let levels = height - 1 - depth;
2usize.rotate_left(levels as u32) - 1
}
#[derive(Copy, Clone, Debug)]
pub enum BinStrat {
Checked,
NotChecked,
}
pub const DEFAULT_NUMBER_ELEM_PER_NODE: usize = 32;
#[derive(Copy, Clone)]
pub struct TreePreBuilder {
height: usize,
}
impl TreePreBuilder {
pub const fn new(num_elements: usize) -> TreePreBuilder {
let height = compute_tree_height_heuristic(num_elements, DEFAULT_NUMBER_ELEM_PER_NODE);
TreePreBuilder {
height,
}
}
pub const fn with_num_elem_in_leaf(
num_elements: usize,
num_elem_leaf: usize,
) -> TreePreBuilder {
let height = compute_tree_height_heuristic(num_elements, num_elem_leaf);
TreePreBuilder {
height,
}
}
pub const fn with_height(height: usize) -> TreePreBuilder {
TreePreBuilder {
height,
}
}
pub fn into_builder<T: Aabb>(self, bots: &mut [T]) -> TreeBuilder<T> {
TreeBuilder::from_prebuilder(bots, self)
}
pub const fn num_nodes(&self) -> usize {
nodes_left(0, self.height)
}
pub const fn get_height(&self) -> usize {
self.height
}
}
#[inline]
const fn compute_tree_height_heuristic(num_bots: usize, num_per_node: usize) -> usize {
if num_bots <= num_per_node {
1
} else {
let (num_bots, num_per_node) = (num_bots as u64, num_per_node as u64);
let a = num_bots / num_per_node;
let a = log_2(a);
(a + 1) as usize
}
}
const fn log_2(x: u64) -> u64 {
const fn num_bits<T>() -> usize {
core::mem::size_of::<T>() * 8
}
num_bits::<u64>() as u64 - x.leading_zeros() as u64 - 1
}
use crate::query::colfind::NotSortedQueries;
use crate::tree::Queries;
pub struct NotSorted<'a, T: Aabb>(Tree<'a, T>);
impl<'a, T: Aabb + Send + Sync> NotSorted<'a, T>
where
T::Num: Send + Sync,
{
pub fn new_par(joiner:impl crate::Joinable,bots: &'a mut [T]) -> NotSorted<'a, T> {
TreeBuilder::new(bots).build_not_sorted_par(joiner)
}
}
impl<'a, T: Aabb> NotSorted<'a, T> {
pub fn new(bots: &'a mut [T]) -> NotSorted<'a, T> {
TreeBuilder::new(bots).build_not_sorted_seq()
}
}
impl<'a, T: Aabb> NotSortedQueries<'a> for NotSorted<'a, T> {
type T = T;
type Num = T::Num;
#[inline(always)]
fn vistr_mut(&mut self) -> VistrMut<Node<'a, T>> {
self.0.vistr_mut()
}
#[inline(always)]
fn vistr(&self) -> Vistr<Node<'a, T>> {
self.0.vistr()
}
}
impl<'a, T: Aabb> NotSorted<'a, T> {
#[inline(always)]
pub fn get_height(&self) -> usize {
self.0.get_height()
}
}