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
/// Sorts a set of two values in increasing order.
///
/// This is a simple utility function that takes two values and returns them
/// as a tuple in sorted order (smallest first). This function is only available
/// in 3D mode (`dim3` feature).
///
/// # Arguments
///
/// * `a` - The first value
/// * `b` - The second value
///
/// # Returns
///
/// A tuple `(min, max)` where `min` is the smaller value and `max` is the larger value.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::utils::sort2;
///
/// let (min, max) = sort2(5.0, 2.0);
/// assert_eq!(min, 2.0);
/// assert_eq!(max, 5.0);
///
/// // Already sorted values remain in the same order
/// let (min, max) = sort2(1.0, 3.0);
/// assert_eq!(min, 1.0);
/// assert_eq!(max, 3.0);
/// # }
/// ```
/// Sorts a set of three values in increasing order.
///
/// This function efficiently sorts three values using a minimal number of comparisons
/// (between 2 and 3 comparisons). The values are passed by reference and references
/// are returned, making this efficient for larger types.
///
/// # Arguments
///
/// * `a` - Reference to the first value
/// * `b` - Reference to the second value
/// * `c` - Reference to the third value
///
/// # Returns
///
/// A tuple of three references `(&min, &mid, &max)` in sorted order.
///
/// # Examples
///
/// ## Basic Sorting
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::utils::sort3;
///
/// let x = 5.0;
/// let y = 2.0;
/// let z = 8.0;
///
/// let (min, mid, max) = sort3(&x, &y, &z);
///
/// assert_eq!(*min, 2.0);
/// assert_eq!(*mid, 5.0);
/// assert_eq!(*max, 8.0);
/// # }
/// ```
///
/// ## All Permutations Work
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::utils::sort3;
///
/// let a = 1.0;
/// let b = 2.0;
/// let c = 3.0;
///
/// // All orderings produce the same sorted result
/// let (min1, mid1, max1) = sort3(&a, &b, &c);
/// let (min2, mid2, max2) = sort3(&c, &a, &b);
/// let (min3, mid3, max3) = sort3(&b, &c, &a);
///
/// assert_eq!(*min1, *min2);
/// assert_eq!(*min1, *min3);
/// assert_eq!(*max1, *max2);
/// assert_eq!(*max1, *max3);
/// # }
/// ```
///
/// ## Finding Median of Three
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::utils::sort3;
///
/// let values = [10.0, 5.0, 7.0];
/// let (_, median, _) = sort3(&values[0], &values[1], &values[2]);
///
/// // The middle value is the median
/// assert_eq!(*median, 7.0);
/// # }
/// ```
///
/// ## Works with Integers
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::utils::sort3;
///
/// let x = 42;
/// let y = 17;
/// let z = 99;
///
/// let (min, mid, max) = sort3(&x, &y, &z);
///
/// assert_eq!(*min, 17);
/// assert_eq!(*mid, 42);
/// assert_eq!(*max, 99);
/// # }
/// ```