From: MA Beaudet Date: Wed, 10 Nov 2021 19:51:59 +0000 (+0100) Subject: perf(eval): remove unnecessary clones of hands and table X-Git-Url: https://git.beaudet.xyz/?a=commitdiff_plain;h=8b85fc00ad1aa7773c9cae72bc0d9f5a213ad978;p=poker-eval.git perf(eval): remove unnecessary clones of hands and table --- diff --git a/src/evaluator.rs b/src/evaluator.rs index b7e8fa2..9b483ae 100644 --- a/src/evaluator.rs +++ b/src/evaluator.rs @@ -60,46 +60,47 @@ impl Evaluator { } pub fn eval(&self) -> Result, MyError> { - fn parse_args(evaluator: &Evaluator) -> Result<(Vec>, Vec), MyError> { - let hands: Vec> = evaluator - .hands - .clone() - .into_iter() - .map(|cards| cards.into_inner()) - .collect(); + fn parse_args(evaluator: &Evaluator) -> Result<(), MyError> { + let hands = &evaluator.hands; + let table = &evaluator.table; if hands.is_empty() { return Err(MyError::NoHands); } - let table = evaluator.table.clone().into_inner(); - if hands[0].len() == 5 && matches!(evaluator.rules, Rules::Holdem) { - println!("Warning: card number in hands does not match the given Poker rules"); - return Err(MyError::UnmatchedCardsTable); - } - if hands[0].len() == 2 && matches!(evaluator.rules, Rules::Classic) { - println!("Warning: card number in hands does not match the given Poker rules"); - return Err(MyError::UnmatchedCardsTable); + for hand in hands { + if hand.len() == 5 && matches!(evaluator.rules, Rules::Holdem) { + println!("Warning: card number in hands does not match the given Poker rules"); + return Err(MyError::UnmatchedCardsTable); + } + if hand.len() == 2 && matches!(evaluator.rules, Rules::Classic) { + println!("Warning: card number in hands does not match the given Poker rules"); + return Err(MyError::UnmatchedCardsTable); + } } - if matches!(evaluator.rules, Rules::Holdem) && table.len() == 0 { + if matches!(evaluator.rules, Rules::Holdem) && table.is_empty() { return Err(MyError::NoTable); } - Ok((hands, table)) + Ok(()) } - let (hands, table) = parse_args(self)?; + parse_args(&self)?; + + let hands: Vec> = self.hands.iter().map(|h| h.clone().into()).collect(); + let table: Vec = self.table.clone().into(); let result = match self.rules { Rules::Classic => hands .iter() .map(|h| match self.algorithm { - Algorithm::CactusKev => Evaluator::eval_5hand(h.as_slice()).unwrap(), + Algorithm::CactusKev => Evaluator::eval_5hand(h).unwrap(), }) .enumerate() .collect(), Rules::Holdem => hands .into_iter() - .map(|h| match self.algorithm { + .map(|mut h| match self.algorithm { Algorithm::CactusKev => { - Evaluator::eval_7hand([h, table.clone()].concat().as_slice()).unwrap() + h.extend(&table); + Evaluator::eval_7hand(&h).unwrap() } }) .enumerate()