Skip to content

Commit

Permalink
Merge pull request #50 from JayXon/op_len
Browse files Browse the repository at this point in the history
support 0 length binary operator
  • Loading branch information
JayXon authored Jun 16, 2024
2 parents dd271e4 + 38b9739 commit 6490659
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
15 changes: 8 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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);
}
Expand All @@ -415,7 +415,8 @@ fn validate_input() {
}

assert!(
INPUTS.iter().map(|i| i.min_uses as usize).sum::<usize>() * 2 <= MAX_LENGTH + 1,
INPUTS.iter().map(|i| i.min_uses as usize).sum::<usize>() * (1 + MIN_BINARY_OP_LEN)
<= MAX_LENGTH + 1,
"The minimum uses requirement will never be met"
);

Expand Down
21 changes: 15 additions & 6 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,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() {
Expand All @@ -521,8 +523,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
};

0 comments on commit 6490659

Please sign in to comment.