From d9678f004029d7f4ce5d68c5e8f9c1a08afd0423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Wed, 20 Nov 2019 15:16:52 +0100 Subject: [PATCH] Fix the ordering functions of the Number type --- meilidb-core/src/number.rs | 47 +++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/meilidb-core/src/number.rs b/meilidb-core/src/number.rs index ff3419008..7817be3e5 100644 --- a/meilidb-core/src/number.rs +++ b/meilidb-core/src/number.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::fmt; use std::num::{ParseFloatError, ParseIntError}; use std::str::FromStr; @@ -5,7 +6,7 @@ use std::str::FromStr; use ordered_float::OrderedFloat; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash)] pub enum Number { Unsigned(u64), Signed(i64), @@ -39,6 +40,50 @@ impl FromStr for Number { } } +impl PartialEq for Number { + fn eq(&self, other: &Number) -> bool { + self.cmp(other) == Ordering::Equal + } +} + +impl Eq for Number {} + +impl PartialOrd for Number { + fn partial_cmp(&self, other: &Number) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Number { + fn cmp(&self, other: &Self) -> Ordering { + use Number::{Float, Signed, Unsigned}; + + match (*self, *other) { + (Unsigned(a), Unsigned(b)) => a.cmp(&b), + (Unsigned(a), Signed(b)) => { + if b < 0 { + Ordering::Greater + } else { + a.cmp(&(b as u64)) + } + } + (Unsigned(a), Float(b)) => (OrderedFloat(a as f64)).cmp(&b), + (Signed(a), Unsigned(b)) => { + if a < 0 { + Ordering::Less + } else { + (a as u64).cmp(&b) + } + } + (Signed(a), Signed(b)) => a.cmp(&b), + (Signed(a), Float(b)) => OrderedFloat(a as f64).cmp(&b), + (Float(a), Unsigned(b)) => a.cmp(&OrderedFloat(b as f64)), + (Float(a), Signed(b)) => a.cmp(&OrderedFloat(b as f64)), + (Float(a), Float(b)) => a.cmp(&b), + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct ParseNumberError { uint_error: ParseIntError,