From 1f96d76c74ed4619179479a438987cba0d964e90 Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Fri, 22 Nov 2024 14:17:04 +0100 Subject: [PATCH] Refactoring --- src/number.rs | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/number.rs b/src/number.rs index 8591b0a..8f12f43 100644 --- a/src/number.rs +++ b/src/number.rs @@ -427,35 +427,32 @@ impl Ord for Number { } else if self.sign == Sign::Positif && other.sign == Sign::Negatif { return Ordering::Greater; } - if self.digits.len() < other.digits.len() { - if self.sign == Sign::Positif { - return Ordering::Less; - } else if self.sign == Sign::Negatif { - return Ordering::Greater; - } - } else if self.digits.len() > other.digits.len() { - if self.sign == Sign::Positif { - return Ordering::Greater; - } else if self.sign == Sign::Negatif { - return Ordering::Less; + match self.digits.len().cmp(&other.digits.len()) { + Ordering::Less => match self.sign { + Sign::Positif => Ordering::Less, + Sign::Negatif => Ordering::Greater, + }, + Ordering::Greater => match self.sign { + Sign::Positif => Ordering::Greater, + Sign::Negatif => Ordering::Less, + }, + Ordering::Equal => { + for pair in zip(&self.digits, &other.digits) { + return match pair.0.cmp(pair.1) { + Ordering::Less => match self.sign { + Sign::Positif => Ordering::Less, + Sign::Negatif => Ordering::Greater, + }, + Ordering::Greater => match self.sign { + Sign::Positif => Ordering::Greater, + Sign::Negatif => Ordering::Less, + }, + Ordering::Equal => continue, + }; + } + Ordering::Equal } } - for pair in zip(&self.digits, &other.digits) { - if pair.0 < pair.1 { - if self.sign == Sign::Positif { - return Ordering::Less; - } else if self.sign == Sign::Negatif { - return Ordering::Greater; - } - } else if pair.0 > pair.1 { - if self.sign == Sign::Positif { - return Ordering::Greater; - } else if self.sign == Sign::Negatif { - return Ordering::Less; - } - } - } - Ordering::Equal } }