Replace per-node measure functions with a single measure function + per-node context (#490)
1//! Allocator-flexible data types
2
3// When std is enabled, prefer those types
4#[cfg(feature = "std")]
5pub(crate) use self::std::*;
6
7// When alloc but not std is enabled, use those types
8#[cfg(all(feature = "alloc", not(feature = "std")))]
9pub(crate) use self::alloc::*;
10
11// When neither alloc or std is enabled, use a heapless fallback
12#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
13pub(crate) use self::core::*;
14
15/// For when `std` is enabled
16#[cfg(feature = "std")]
17mod std {
18 /// An allocation-backend agnostic vector type
19 pub(crate) type Vec<A> = std::vec::Vec<A>;
20 /// A vector of child nodes
21 pub(crate) type ChildrenVec<A> = std::vec::Vec<A>;
22 #[cfg(feature = "grid")]
23 /// A vector of grid tracks
24 pub(crate) type GridTrackVec<A> = std::vec::Vec<A>;
25
26 /// Creates a new vector with the capacity for the specified number of items before it must be resized
27 #[must_use]
28 pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
29 Vec::with_capacity(capacity)
30 }
31
32 /// Rounds to the nearest whole number
33 #[must_use]
34 pub(crate) fn round(value: f32) -> f32 {
35 value.round()
36 }
37
38 /// Computes the absolute value
39 #[must_use]
40 pub(crate) fn abs(value: f32) -> f32 {
41 value.abs()
42 }
43
44 /// Returns the largest of two f32 values
45 pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
46 std::cmp::max_by(a, b, |a, b| a.total_cmp(b))
47 }
48
49 /// Returns the smallest of two f32 values
50 pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
51 std::cmp::min_by(a, b, |a, b| a.total_cmp(b))
52 }
53}
54
55/// For when `alloc` but not `std` is enabled
56#[cfg(all(feature = "alloc", not(feature = "std")))]
57mod alloc {
58 extern crate alloc;
59 use core::cmp::Ordering;
60
61 /// An allocation-backend agnostic vector type
62 pub(crate) type Vec<A> = alloc::vec::Vec<A>;
63 /// A vector of child nodes
64 pub(crate) type ChildrenVec<A> = alloc::vec::Vec<A>;
65 #[cfg(feature = "grid")]
66 /// A vector of grid tracks
67 pub(crate) type GridTrackVec<A> = alloc::vec::Vec<A>;
68
69 /// Creates a new vector with the capacity for the specified number of items before it must be resized
70 #[must_use]
71 pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
72 Vec::with_capacity(capacity)
73 }
74
75 /// Rounds to the nearest whole number
76 #[must_use]
77 pub(crate) fn round(value: f32) -> f32 {
78 num_traits::float::FloatCore::round(value)
79 }
80
81 /// Computes the absolute value
82 #[must_use]
83 pub(crate) fn abs(value: f32) -> f32 {
84 num_traits::float::FloatCore::abs(value)
85 }
86
87 /// Returns the largest of two f32 values
88 pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
89 match a.total_cmp(&b) {
90 Ordering::Greater | Ordering::Equal => a,
91 Ordering::Less => b,
92 }
93 }
94
95 /// Returns the smallest of two f32 values
96 pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
97 match a.total_cmp(&b) {
98 Ordering::Less => a,
99 Ordering::Greater | Ordering::Equal => b,
100 }
101 }
102}
103
104/// For when neither `alloc` nor `std` is enabled
105#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
106mod core {
107 use core::cmp::Ordering;
108
109 /// The maximum number of nodes in the tree
110 pub const MAX_NODE_COUNT: usize = 256;
111 /// The maximum number of children of any given node
112 pub const MAX_CHILD_COUNT: usize = 16;
113 #[cfg(feature = "grid")]
114 /// The maximum number of children of any given node
115 pub const MAX_GRID_TRACKS: usize = 16;
116
117 /// An allocation-backend agnostic vector type
118 pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
119 /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
120 pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
121 #[cfg(feature = "grid")]
122 /// A vector of grid tracks
123 pub(crate) type GridTrackVec<A> = arrayvec::ArrayVec<A, MAX_GRID_TRACKS>;
124
125 /// Creates a new map with the capacity for the specified number of items before it must be resized
126 ///
127 /// This vector cannot be resized.
128 #[must_use]
129 pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
130 arrayvec::ArrayVec::new()
131 }
132
133 /// Rounds to the nearest whole number
134 #[inline]
135 #[must_use]
136 pub(crate) fn round(value: f32) -> f32 {
137 num_traits::float::FloatCore::round(value)
138 }
139
140 /// Computes the absolute value
141 #[inline]
142 #[must_use]
143 pub(crate) fn abs(value: f32) -> f32 {
144 num_traits::float::FloatCore::abs(value)
145 }
146
147 /// Returns the largest of two f32 values
148 pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
149 match a.total_cmp(&b) {
150 Ordering::Greater | Ordering::Equal => a,
151 Ordering::Less => b,
152 }
153 }
154
155 /// Returns the smallest of two f32 values
156 pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
157 match a.total_cmp(&b) {
158 Ordering::Less => a,
159 Ordering::Greater | Ordering::Equal => b,
160 }
161 }
162}