Skip to content

Commit d9eec66

Browse files
committed
Fix Ord implementation to use lexical ordering
1 parent db453ec commit d9eec66

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/libcore/tuple.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ macro_rules! tuple_impls(
165165
fn eq(&self, other: &($($T),+)) -> bool {
166166
$(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
167167
}
168-
169168
#[inline(always)]
170169
fn ne(&self, other: &($($T),+)) -> bool {
171170
!(*self == *other)
@@ -176,29 +175,30 @@ macro_rules! tuple_impls(
176175
impl<$($T:Ord),+> Ord for ($($T),+) {
177176
#[inline(always)]
178177
fn lt(&self, other: &($($T),+)) -> bool {
179-
$(*self.$get_ref_fn() < *other.$get_ref_fn())&&+
178+
lexical_lt!($(*self.$get_ref_fn(), *other.$get_ref_fn()),+)
180179
}
181-
182180
#[inline(always)]
183-
fn le(&self, other: &($($T),+)) -> bool {
184-
$(*self.$get_ref_fn() <= *other.$get_ref_fn())&&+
185-
}
186-
181+
fn le(&self, other: &($($T),+)) -> bool { !(*other).lt(&(*self)) }
187182
#[inline(always)]
188-
fn ge(&self, other: &($($T),+)) -> bool {
189-
$(*self.$get_ref_fn() >= *other.$get_ref_fn())&&+
190-
}
191-
183+
fn ge(&self, other: &($($T),+)) -> bool { !(*self).lt(other) }
192184
#[inline(always)]
193-
fn gt(&self, other: &($($T),+)) -> bool {
194-
$(*self.$get_ref_fn() > *other.$get_ref_fn())&&+
195-
}
185+
fn gt(&self, other: &($($T),+)) -> bool { (*other).lt(&(*self)) }
196186
}
197187
)+
198188
}
199189
)
200190
)
201191

192+
// Constructs an expression that performs a lexical less-than ordering.
193+
// The values are interleaved, so the macro invocation for
194+
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_lt!(a1, b1, a2, b2, a3, b3)`
195+
macro_rules! lexical_lt(
196+
($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => (
197+
if $a < $b { true } else { lexical_lt!($($rest_a, $rest_b),+) }
198+
);
199+
($a:expr, $b:expr) => ($a < $b);
200+
)
201+
202202
tuple_impls!(
203203
(CloneableTuple2, ImmutableTuple2) {
204204
(n0, n0_ref) -> A { (ref a,_) => a }

0 commit comments

Comments
 (0)