modes:triple: ordering done

This commit is contained in:
2026-04-07 13:55:31 +01:00
committed by oreodave
parent d8235b6edd
commit d538ab1600
3 changed files with 41 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
pub mod pair;
pub mod single;
pub mod triple;
#[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Copy, Clone)]
pub enum Footstool {

View File

@@ -3,7 +3,7 @@ use crate::{
helper::ordered,
};
#[derive(Debug, Copy, Clone)]
#[derive(Eq, Debug, Copy, Clone)]
pub struct Triple(Card, Card, Card);
impl Triple {
@@ -30,3 +30,41 @@ impl Triple {
}
}
}
use std::cmp::Ordering;
impl Ord for Triple {
fn cmp(&self, other: &Self) -> Ordering {
/*
Like pairs, we'll do a high-to-low comparison. Since we're dealing with
3 items we'll need to compute potentially 3 comparisons.
*/
let Triple(s1, s2, s3) = self;
let Triple(o1, o2, o3) = other;
let cmp = s3.cmp(o3);
if cmp != Ordering::Equal {
cmp
} else {
let cmp = s2.cmp(o2);
if cmp != Ordering::Equal {
cmp
} else {
s1.cmp(o1)
}
}
}
}
impl PartialEq for Triple {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl PartialOrd for Triple {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}