Evaluate in place expressions

This solution gives similar performance results than the tree based one,
mostly because of the intermediate vectors.
This commit is contained in:
2023-03-05 15:02:33 +01:00
parent ceefe1776d
commit b49d7b7995
4 changed files with 196 additions and 16 deletions

View File

@@ -1,7 +1,6 @@
use crate::expr::are_valid_expressions;
use crate::expr::is_valid_guess_of_tokens;
use crate::game::Hand;
use crate::lexer::lexer;
use crate::parser::parse;
use crate::lexer::{lexer_reuse, Token};
use crate::tile::{Digit, Operator, Tile};
use itertools::Itertools;
@@ -37,6 +36,7 @@ pub fn generate_valid_combinations(hand: &Hand) -> Vec<Vec<Tile>> {
}
let mut trial: Vec<Tile> = Vec::with_capacity(numbers.len() + operators.len() + 1);
let mut tokens: Vec<Token> = Vec::with_capacity(numbers.len() + operators.len() + 1);
// Generate all possible permutations, with an increasing number of tiles
for nb_digits in 2..=numbers.len() {
@@ -47,14 +47,12 @@ pub fn generate_valid_combinations(hand: &Hand) -> Vec<Vec<Tile>> {
for nb_operators in 0..=(nb_digits - 2) {
for operators in operators.iter().permutations(nb_operators) {
merge_expression(&digits, &operators, equals_idx, &mut trial);
if let Ok(tokens) = lexer(&trial) {
if let Ok(expressions) = parse(&tokens) {
if are_valid_expressions(&expressions) {
combinations.push(trial.clone());
}
}
lexer_reuse(&trial, &mut tokens);
if is_valid_guess_of_tokens(&tokens) {
combinations.push(trial.clone());
}
trial.clear();
tokens.clear();
}
}
}