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
use PartialOrd;
use mem;
use Deref;
/// A pair of elements sorted in increasing order.
///
/// This structure guarantees that the first element is always less than or equal to
/// the second element. It is useful for representing edges, connections, or any
/// unordered pair where you want canonical ordering (e.g., ensuring that edge (A, B)
/// and edge (B, A) are treated as the same edge).
///
/// The sorted pair implements `Deref` to `(T, T)` for convenient access to the elements.
///
/// # Examples
///
/// ## Creating a Sorted Pair
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// # use parry2d::utils::SortedPair;
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// # use parry3d::utils::SortedPair;
///
/// // Create a pair - the elements will be sorted automatically
/// let pair1 = SortedPair::new(5, 2);
/// let pair2 = SortedPair::new(2, 5);
///
/// // Both pairs are equal because they contain the same sorted elements
/// assert_eq!(pair1, pair2);
///
/// // Access elements via dereferencing
/// assert_eq!(pair1.0, 2);
/// assert_eq!(pair1.1, 5);
/// # }
/// # }
/// ```
///
/// ## Using as HashMap Keys
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// # use parry2d::utils::SortedPair;
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// # use parry3d::utils::SortedPair;
/// use std::collections::HashMap;
///
/// let mut edge_weights = HashMap::new();
///
/// // These represent the same edge, so they'll map to the same entry
/// edge_weights.insert(SortedPair::new(1, 3), 10.0);
/// edge_weights.insert(SortedPair::new(3, 1), 20.0); // Overwrites previous
///
/// assert_eq!(edge_weights.len(), 1);
/// assert_eq!(edge_weights.get(&SortedPair::new(1, 3)), Some(&20.0));
/// # }
/// # }
/// ```
///
/// ## Representing Graph Edges
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// # use parry2d::utils::SortedPair;
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// # use parry3d::utils::SortedPair;
///
/// // Represent undirected edges in a graph
/// let edges = vec![
/// SortedPair::new(0, 1), // Edge between vertices 0 and 1
/// SortedPair::new(1, 2), // Edge between vertices 1 and 2
/// SortedPair::new(2, 0), // Edge between vertices 2 and 0
/// ];
///
/// // Check if a specific edge exists (order doesn't matter)
/// let query_edge = SortedPair::new(2, 1);
/// assert!(edges.contains(&query_edge));
/// # }
/// # }
/// ```
///
/// ## Ordering
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// # use parry2d::utils::SortedPair;
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// # use parry3d::utils::SortedPair;
///
/// let pair1 = SortedPair::new(1, 5);
/// let pair2 = SortedPair::new(2, 3);
/// let pair3 = SortedPair::new(1, 6);
///
/// // Pairs are compared lexicographically (first element, then second)
/// assert!(pair1 < pair2); // (1, 5) < (2, 3)
/// assert!(pair1 < pair3); // (1, 5) < (1, 6)
/// # }
/// # }
/// ```
;
// TODO: can we avoid these manual impls of Hash/PartialEq/Eq for the archived types?