Use slotmap instead of bespoke arena, limiting to one parent only (#196)
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 [`Box`] type
19 pub(crate) type Box<A> = std::boxed::Box<A>;
20 /// An allocation-backend agnostic vector type
21 pub(crate) type Vec<A> = std::vec::Vec<A>;
22 /// A vector of child nodes
23 pub(crate) type ChildrenVec<A> = std::vec::Vec<A>;
24
25 /// Creates a new vector with the capacity for the specified number of items before it must be resized
26 #[must_use]
27 pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
28 Vec::with_capacity(capacity)
29 }
30
31 /// Rounds to the nearest whole number
32 #[must_use]
33 pub(crate) fn round(value: f32) -> f32 {
34 value.round()
35 }
36
37 /// Computes the absolute value
38 #[must_use]
39 pub(crate) fn abs(value: f32) -> f32 {
40 value.abs()
41 }
42}
43
44/// For when `alloc` but not `std` is enabled
45#[cfg(all(feature = "alloc", not(feature = "std")))]
46mod alloc {
47 extern crate alloc;
48
49 /// An allocation-backend agnostic `Box` type
50 pub(crate) type Box<A> = alloc::boxed::Box<A>;
51 /// An allocation-backend agnostic vector type
52 pub(crate) type Vec<A> = alloc::vec::Vec<A>;
53 /// A vector of child nodes
54 pub(crate) type ChildrenVec<A> = alloc::vec::Vec<A>;
55
56 /// Creates a new vector with the capacity for the specified number of items before it must be resized
57 #[must_use]
58 pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
59 Vec::with_capacity(capacity)
60 }
61
62 /// Rounds to the nearest whole number
63 #[must_use]
64 pub(crate) fn round(value: f32) -> f32 {
65 num_traits::float::FloatCore::round(value)
66 }
67
68 /// Computes the absolute value
69 #[must_use]
70 pub(crate) fn abs(value: f32) -> f32 {
71 num_traits::float::FloatCore::abs(value)
72 }
73}
74
75/// For when neither `alloc` nor `std` is enabled
76#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
77mod core {
78 /// The maximum number of nodes in the tree
79 pub const MAX_NODE_COUNT: usize = 256;
80 /// The maximum number of children of any given node
81 pub const MAX_CHILD_COUNT: usize = 16;
82
83 /// An allocation-backend agnostic vector type
84 pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
85 /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
86 pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
87
88 /// Creates a new map with the capacity for the specified number of items before it must be resized
89 ///
90 /// This vector cannot be resized.
91 #[must_use]
92 pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
93 arrayvec::ArrayVec::new()
94 }
95
96 /// Rounds to the nearest whole number
97 #[inline]
98 #[must_use]
99 pub(crate) fn round(value: f32) -> f32 {
100 num_traits::float::FloatCore::round(value)
101 }
102
103 /// Computes the absolute value
104 #[inline]
105 #[must_use]
106 pub(crate) fn abs(value: f32) -> f32 {
107 num_traits::float::FloatCore::abs(value)
108 }
109}