From 38b9739d19b91f116c616ad2c016342d39251476 Mon Sep 17 00:00:00 2001 From: Sen Jiang Date: Sat, 15 Jun 2024 21:44:09 -0700 Subject: [PATCH] support 0 length binary operator --- src/main.rs | 15 ++++++++------- src/operator.rs | 21 +++++++++++++++------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 89a7491..511279c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ fn can_use_required_vars(var_count: VarCount, length: usize) -> bool { .zip(INPUTS.iter()) .map(|(&c, i)| i.min_uses - std::cmp::min(c, i.min_uses)) .sum(); - length + missing_uses as usize * 2 <= MAX_LENGTH + length + missing_uses as usize * (1 + MIN_BINARY_OP_LEN) <= MAX_LENGTH } fn is_leaf_expr(op_idx: OpIndex, length: usize) -> bool { @@ -104,7 +104,7 @@ fn save(level: &mut CacheLevel, expr: Expr, n: usize, cache: &Cache, hashset_cac } if n > MAX_CACHE_LENGTH { - for dfs_len in n + 2..=MAX_LENGTH { + for dfs_len in n + 1 + MIN_BINARY_OP_LEN..=MAX_LENGTH { find_binary_expressions(level, cache, hashset_cache, dfs_len, n, &expr); } if n + 1 <= MAX_LENGTH { @@ -192,7 +192,7 @@ fn find_binary_expressions_left( k: usize, er: &Expr, ) { - seq!(op_len in 1..=5 { + seq!(op_len in 0..=5 { if n <= k + op_len { return; }; @@ -210,7 +210,7 @@ fn find_binary_expressions( k: usize, e1: &Expr, ) { - seq!(op_len in 1..=5 { + seq!(op_len in 0..=5 { if n <= k + op_len { return; }; @@ -357,7 +357,7 @@ fn find_expressions_multithread( let cache = &mut_cache; let hashset_cache = &mut_hashset_cache; - let mut cn = (1..n - 1) + let mut cn = (1..n - MIN_BINARY_OP_LEN) .into_par_iter() .flat_map(|k| { cache[k].par_iter().map(move |r| { @@ -395,7 +395,7 @@ fn find_expressions(cache: &mut Cache, hashset_cache: &mut HashSetCache, n: usiz find_variables_and_literals(&mut cn, n); find_parens_expressions(&mut cn, cache, hashset_cache, n); find_unary_expressions(&mut cn, cache, hashset_cache, n); - for k in 1..n - 1 { + for k in 1..n - MIN_BINARY_OP_LEN { for r in &cache[k] { find_binary_expressions_left(&mut cn, cache, hashset_cache, n, k, r); } @@ -415,7 +415,8 @@ fn validate_input() { } assert!( - INPUTS.iter().map(|i| i.min_uses as usize).sum::() * 2 <= MAX_LENGTH + 1, + INPUTS.iter().map(|i| i.min_uses as usize).sum::() * (1 + MIN_BINARY_OP_LEN) + <= MAX_LENGTH + 1, "The minimum uses requirement will never be met" ); diff --git a/src/operator.rs b/src/operator.rs index efd68fd..87b3754 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -503,7 +503,9 @@ const fn gen_index_tables() -> ( (binary_table, unary_table) } -const fn gen_op_name_table() -> [&'static str; 256] { +pub const OP_BINARY_INDEX_TABLE: [OpIndex; BINARY_OPERATORS.len()] = gen_index_tables().0; +pub const OP_UNARY_INDEX_TABLE: [OpIndex; UNARY_OPERATORS.len()] = gen_index_tables().1; +pub const OP_NAME_TABLE: [&'static str; 256] = { let mut table = [""; 256]; let mut i: usize = 0; while i < OP_BINARY_INDEX_TABLE.len() { @@ -517,8 +519,15 @@ const fn gen_op_name_table() -> [&'static str; 256] { } table[OP_INDEX_PARENS.as_index()] = "("; table -} - -pub const OP_BINARY_INDEX_TABLE: [OpIndex; BINARY_OPERATORS.len()] = gen_index_tables().0; -pub const OP_UNARY_INDEX_TABLE: [OpIndex; UNARY_OPERATORS.len()] = gen_index_tables().1; -pub const OP_NAME_TABLE: [&'static str; 256] = gen_op_name_table(); +}; +pub const MIN_BINARY_OP_LEN: usize = { + let mut min_len = usize::MAX; + let mut i = 0; + while i < OP_BINARY_INDEX_TABLE.len() { + if BINARY_OPERATORS[i].name.len() < min_len { + min_len = BINARY_OPERATORS[i].name.len(); + } + i += 1; + } + min_len +};