Skip to content

Commit 91cff90

Browse files
committed
change how complex comparison works
1 parent ee49cdb commit 91cff90

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/algorithm/pervade.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ pub mod complex_im {
637637
}
638638
}
639639

640-
macro_rules! cmp_impl {
640+
macro_rules! eq_impl {
641641
($name:ident $eq:tt $ordering:expr) => {
642642
pub mod $name {
643643
use super::*;
@@ -650,11 +650,9 @@ macro_rules! cmp_impl {
650650
pub fn num_num(a: f64, b: f64) -> u8 {
651651
(b.array_cmp(&a) $eq $ordering) as u8
652652
}
653-
654653
pub fn com_x(a: Complex, b: impl Into<Complex>) -> u8 {
655654
(b.into().array_cmp(&a) $eq $ordering) as u8
656655
}
657-
658656
pub fn x_com(a: impl Into<Complex>, b: Complex) -> u8 {
659657
(b.array_cmp(&a.into()) $eq $ordering) as u8
660658
}
@@ -679,8 +677,56 @@ macro_rules! cmp_impl {
679677
};
680678
}
681679

682-
cmp_impl!(is_eq == std::cmp::Ordering::Equal);
683-
cmp_impl!(is_ne != Ordering::Equal);
680+
macro_rules! cmp_impl {
681+
($name:ident $eq:tt $ordering:expr) => {
682+
pub mod $name {
683+
use super::*;
684+
pub fn always_greater<A, B>(_: A, _: B) -> u8 {
685+
($ordering $eq Ordering::Less).into()
686+
}
687+
pub fn always_less<A, B>(_: A, _: B) -> u8 {
688+
($ordering $eq Ordering::Greater).into()
689+
}
690+
pub fn num_num(a: f64, b: f64) -> u8 {
691+
(b.array_cmp(&a) $eq $ordering) as u8
692+
}
693+
pub fn com_x(a: Complex, b: impl Into<Complex>) -> Complex {
694+
let b = b.into();
695+
Complex::new(
696+
(b.re.array_cmp(&a.re) $eq $ordering) as u8 as f64,
697+
(b.im.array_cmp(&a.im) $eq $ordering) as u8 as f64
698+
)
699+
}
700+
pub fn x_com(a: impl Into<Complex>, b: Complex) -> Complex {
701+
let a = a.into();
702+
Complex::new(
703+
(b.re.array_cmp(&a.re) $eq $ordering) as u8 as f64,
704+
(b.im.array_cmp(&a.im) $eq $ordering) as u8 as f64
705+
)
706+
}
707+
#[cfg(feature = "bytes")]
708+
pub fn byte_num(a: u8, b: f64) -> u8 {
709+
(b.array_cmp(&f64::from(a)) $eq $ordering) as u8
710+
}
711+
#[cfg(feature = "bytes")]
712+
pub fn num_byte(a: f64, b: u8) -> u8 {
713+
(f64::from(b).array_cmp(&a) $eq $ordering) as u8
714+
}
715+
pub fn generic<T: Ord>(a: T, b: T) -> u8 {
716+
(b.cmp(&a) $eq $ordering).into()
717+
}
718+
pub fn same_type<T: ArrayCmp + From<u8>>(a: T, b: T) -> T {
719+
((b.array_cmp(&a) $eq $ordering) as u8).into()
720+
}
721+
pub fn error<T: Display>(a: T, b: T, _env: &Uiua) -> UiuaError {
722+
unreachable!("Comparisons cannot fail, failed to compare {a} and {b}")
723+
}
724+
}
725+
};
726+
}
727+
728+
eq_impl!(is_eq == Ordering::Equal);
729+
eq_impl!(is_ne != Ordering::Equal);
684730
cmp_impl!(is_lt == Ordering::Less);
685731
cmp_impl!(is_le != Ordering::Greater);
686732
cmp_impl!(is_gt == Ordering::Greater);

src/value.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ value_bin_impl!(
12131213
("byte", Byte, Complex, x_com, com_x),
12141214
);
12151215

1216-
macro_rules! cmp_impls {
1216+
macro_rules! eq_impls {
12171217
($($name:ident),*) => {
12181218
$(
12191219
value_bin_impl!(
@@ -1240,7 +1240,35 @@ macro_rules! cmp_impls {
12401240
};
12411241
}
12421242

1243-
cmp_impls!(is_eq, is_ne, is_lt, is_le, is_gt, is_ge);
1243+
macro_rules! cmp_impls {
1244+
($($name:ident),*) => {
1245+
$(
1246+
value_bin_impl!(
1247+
$name,
1248+
// Value comparable
1249+
[Num, same_type],
1250+
[Complex, com_x],
1251+
("bytes", Byte, Byte, same_type, num_num),
1252+
(Char, Char, generic),
1253+
(Box, Box, generic),
1254+
("bytes", Num, Byte, num_byte, num_num),
1255+
("bytes", Byte, Num, byte_num, num_num),
1256+
(Complex, Num, com_x),
1257+
(Num, Complex, x_com),
1258+
("byte", Complex, Byte, com_x),
1259+
("byte", Byte, Complex, x_com),
1260+
// Type comparable
1261+
(Num, Char, always_less),
1262+
("bytes", Byte, Char, always_less),
1263+
(Char, Num, always_greater),
1264+
("bytes", Char, Byte, always_greater),
1265+
);
1266+
)*
1267+
};
1268+
}
1269+
1270+
eq_impls!(is_eq, is_ne);
1271+
cmp_impls!(is_lt, is_le, is_gt, is_ge);
12441272

12451273
impl PartialEq for Value {
12461274
fn eq(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)