From d538ab1600dc3e0fb6a5907c7049be5c7c871ba6 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Tue, 7 Apr 2026 13:55:31 +0100 Subject: [PATCH] modes:triple: ordering done --- big-c.org | 2 +- src/modes/mod.rs | 1 + src/modes/triple.rs | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/big-c.org b/big-c.org index 8e6f135..a37c469 100644 --- a/big-c.org +++ b/big-c.org @@ -5,7 +5,7 @@ Model the concept of a triple (three cards of similar rank). ** DONE Triple::new ** TODO Hand ** TODO Display -** TODO Ord +** DONE Ord ** TODO Hash ** TODO Testing * Backlog :backlog: diff --git a/src/modes/mod.rs b/src/modes/mod.rs index 42a2a65..1861b74 100644 --- a/src/modes/mod.rs +++ b/src/modes/mod.rs @@ -1,5 +1,6 @@ pub mod pair; pub mod single; +pub mod triple; #[derive(Eq, Ord, PartialEq, PartialOrd, Debug, Copy, Clone)] pub enum Footstool { diff --git a/src/modes/triple.rs b/src/modes/triple.rs index 1009931..9a43856 100644 --- a/src/modes/triple.rs +++ b/src/modes/triple.rs @@ -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 { + Some(self.cmp(other)) + } +} +