modes:triple: ordering done

This commit is contained in:
2026-04-07 13:55:31 +01:00
parent 3f8181fff5
commit 9a01ed8154
3 changed files with 41 additions and 2 deletions

View File

@@ -5,7 +5,7 @@ Model the concept of a triple (three cards of similar rank).
** DONE Triple::new ** DONE Triple::new
** TODO Hand ** TODO Hand
** TODO Display ** TODO Display
** TODO Ord ** DONE Ord
** TODO Hash ** TODO Hash
** TODO Testing ** TODO Testing
* Backlog :backlog: * Backlog :backlog:

View File

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

View File

@@ -3,7 +3,7 @@ use crate::{
helper::ordered, helper::ordered,
}; };
#[derive(Debug, Copy, Clone)] #[derive(Eq, Debug, Copy, Clone)]
pub struct Triple(Card, Card, Card); pub struct Triple(Card, Card, Card);
impl Triple { 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))
}
}