Compute content size, padding and border for each node (#573)
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 #[inline(always)]
35 pub(crate) fn round(value: f32) -> f32 {
36 value.round()
37 }
38
39 /// Computes the absolute value
40 #[must_use]
41 #[inline(always)]
42 pub(crate) fn abs(value: f32) -> f32 {
43 value.abs()
44 }
45
46 /// Returns the largest of two f32 values
47 #[inline(always)]
48 pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
49 a.max(b)
50 }
51
52 /// Returns the smallest of two f32 values
53 #[inline(always)]
54 pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
55 a.min(b)
56 }
57}
58
59/// For when `alloc` but not `std` is enabled
60#[cfg(all(feature = "alloc", not(feature = "std")))]
61mod alloc {
62 extern crate alloc;
63 use core::cmp::Ordering;
64
65 /// An allocation-backend agnostic vector type
66 pub(crate) type Vec<A> = alloc::vec::Vec<A>;
67 /// A vector of child nodes
68 pub(crate) type ChildrenVec<A> = alloc::vec::Vec<A>;
69 #[cfg(feature = "grid")]
70 /// A vector of grid tracks
71 pub(crate) type GridTrackVec<A> = alloc::vec::Vec<A>;
72
73 /// Creates a new vector with the capacity for the specified number of items before it must be resized
74 #[must_use]
75 pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
76 Vec::with_capacity(capacity)
77 }
78
79 /// Rounds to the nearest whole number
80 #[must_use]
81 #[inline(always)]
82 pub(crate) fn round(value: f32) -> f32 {
83 num_traits::float::FloatCore::round(value)
84 }
85
86 /// Computes the absolute value
87 #[must_use]
88 #[inline(always)]
89 pub(crate) fn abs(value: f32) -> f32 {
90 num_traits::float::FloatCore::abs(value)
91 }
92
93 /// Returns the largest of two f32 values
94 #[inline(always)]
95 pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
96 a.max(b)
97 }
98
99 /// Returns the smallest of two f32 values
100 #[inline(always)]
101 pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
102 a.min(b)
103 }
104}
105
106/// For when neither `alloc` nor `std` is enabled
107#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
108mod core {
109 use core::cmp::Ordering;
110
111 /// The maximum number of nodes in the tree
112 pub const MAX_NODE_COUNT: usize = 256;
113 /// The maximum number of children of any given node
114 pub const MAX_CHILD_COUNT: usize = 16;
115 #[cfg(feature = "grid")]
116 /// The maximum number of children of any given node
117 pub const MAX_GRID_TRACKS: usize = 16;
118
119 /// An allocation-backend agnostic vector type
120 pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
121 /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
122 pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
123 #[cfg(feature = "grid")]
124 /// A vector of grid tracks
125 pub(crate) type GridTrackVec<A> = arrayvec::ArrayVec<A, MAX_GRID_TRACKS>;
126
127 /// Creates a new map with the capacity for the specified number of items before it must be resized
128 ///
129 /// This vector cannot be resized.
130 #[must_use]
131 pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
132 arrayvec::ArrayVec::new()
133 }
134
135 /// Rounds to the nearest whole number
136 #[inline]
137 #[must_use]
138 #[inline(always)]
139 pub(crate) fn round(value: f32) -> f32 {
140 num_traits::float::FloatCore::round(value)
141 }
142
143 /// Computes the absolute value
144 #[inline]
145 #[must_use]
146 #[inline(always)]
147 pub(crate) fn abs(value: f32) -> f32 {
148 num_traits::float::FloatCore::abs(value)
149 }
150
151 /// Returns the largest of two f32 values
152 #[inline(always)]
153 pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
154 a.max(b)
155 }
156
157 /// Returns the smallest of two f32 values
158 #[inline(always)]
159 pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
160 a.min(b)
161 }
162}