Skip to content

Commit a9b6d81

Browse files
committed
performance improvement & bump 0.0.3
1 parent a1d8a85 commit a9b6d81

File tree

4 files changed

+18
-73
lines changed

4 files changed

+18
-73
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "boolean_function"
33
description = "Mathematical analysis of Boolean functions"
4-
version = "0.0.2"
4+
version = "0.0.3"
55
edition = "2021"
66
rust-version = "1.56.0"
77
authors = ["Thomas Prévost"]

src/big_boolean_function.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use itertools::{enumerate, Itertools};
1010
use num_bigint::BigUint;
1111
use num_integer::binomial;
1212
use num_traits::{One, Zero};
13-
use std::any::Any;
1413
use std::ops::{BitXor, BitXorAssign, Not};
1514

1615
/// Struct representing a boolean function with a big truth table.
@@ -332,14 +331,6 @@ impl BooleanFunctionImpl for BigBooleanFunction {
332331
fn biguint_truth_table(&self) -> BigUint {
333332
self.truth_table.clone()
334333
}
335-
336-
fn as_any(&self) -> &dyn Any {
337-
self
338-
}
339-
340-
fn as_any_mut(&mut self) -> &mut dyn Any {
341-
self
342-
}
343334
}
344335

345336
/// In-place XOR operator for Boolean functions truth tables.

src/lib.rs

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use gen_combinations::CombinationIterator;
3030
use num_bigint::BigUint;
3131
use num_traits::{Num, ToPrimitive};
3232
pub use small_boolean_function::SmallBooleanFunction;
33-
use std::any::Any;
3433
use std::collections::{HashMap, HashSet};
3534
use std::fmt::Debug;
3635
use std::ops::{BitXor, BitXorAssign, Not};
@@ -49,7 +48,7 @@ pub enum BooleanFunctionType {
4948
///
5049
/// You could use this trait via the [BooleanFunction] type, which encapsulates the [BooleanFunctionImpl] trait.
5150
#[enum_dispatch]
52-
pub trait BooleanFunctionImpl: Debug + Any {
51+
pub trait BooleanFunctionImpl: Debug {
5352
/// Variable count of the Boolean function.
5453
fn variables_count(&self) -> usize;
5554

@@ -63,7 +62,6 @@ pub trait BooleanFunctionImpl: Debug + Any {
6362
/// Maximum input value for the Boolean function, as unsigned 32-bit integer.
6463
///
6564
/// This is equal to $2^n - 1$, where $n$ is the number of variables.
66-
#[inline]
6765
fn get_max_input_value(&self) -> u32 {
6866
(1 << self.variables_count()) - 1
6967
}
@@ -623,16 +621,6 @@ pub trait BooleanFunctionImpl: Debug + Any {
623621
self.biguint_truth_table().to_u64()
624622
}
625623

626-
/// Returns Boolean function as `dyn Any` object.
627-
///
628-
/// See [core::any::Any]
629-
fn as_any(&self) -> &dyn Any;
630-
631-
/// Returns Boolean function as mutable `dyn Any` object.
632-
///
633-
/// See [core::any::Any]
634-
fn as_any_mut(&mut self) -> &mut dyn Any;
635-
636624
// TODO almost bent, mul (and tt), iterate on values
637625
}
638626

@@ -662,57 +650,32 @@ impl BitXorAssign for BooleanFunction {
662650
panic!("{}", XOR_DIFFERENT_VAR_COUNT_PANIC_MSG);
663651
}
664652
match (
665-
self.get_boolean_function_type(),
666-
rhs.get_boolean_function_type(),
653+
self,
654+
rhs,
667655
) {
668-
(BooleanFunctionType::Small, BooleanFunctionType::Small) => {
669-
let self_small_boolean_function = self
670-
.as_any_mut()
671-
.downcast_mut::<SmallBooleanFunction>()
672-
.unwrap();
673-
let rhs_small_boolean_function =
674-
rhs.as_any().downcast_ref::<SmallBooleanFunction>().unwrap();
675-
*self_small_boolean_function ^= *rhs_small_boolean_function;
676-
}
677-
(BooleanFunctionType::Small, BooleanFunctionType::Big) => {
678-
let self_small_boolean_function = self
679-
.as_any_mut()
680-
.downcast_mut::<SmallBooleanFunction>()
681-
.unwrap();
656+
(BooleanFunction::Small(self_small_boolean_function), BooleanFunction::Small(rhs_small_boolean_function)) => {
657+
*self_small_boolean_function ^= rhs_small_boolean_function;
658+
},
659+
(BooleanFunction::Small(self_small_boolean_function), BooleanFunction::Big(rhs_big_boolean_function)) => {
682660
let rhs_small_boolean_function = SmallBooleanFunction::from_truth_table(
683-
rhs.as_any()
684-
.downcast_ref::<BigBooleanFunction>()
685-
.unwrap()
661+
rhs_big_boolean_function
686662
.biguint_truth_table()
687663
.to_u64()
688664
.unwrap(),
689665
self_num_variables,
690666
)
691667
.unwrap();
692668
*self_small_boolean_function ^= rhs_small_boolean_function;
693-
}
694-
(BooleanFunctionType::Big, BooleanFunctionType::Small) => {
695-
let self_big_boolean_function = self
696-
.as_any_mut()
697-
.downcast_mut::<BigBooleanFunction>()
698-
.unwrap();
699-
let rhs_small_boolean_function = BigBooleanFunction::from_truth_table(
700-
rhs.as_any()
701-
.downcast_ref::<SmallBooleanFunction>()
702-
.unwrap()
703-
.biguint_truth_table(),
669+
},
670+
(BooleanFunction::Big(self_big_boolean_function), BooleanFunction::Small(rhs_small_boolean_function)) => {
671+
let rhs_big_boolean_function = BigBooleanFunction::from_truth_table(
672+
rhs_small_boolean_function.biguint_truth_table(),
704673
rhs_num_variables,
705674
);
706-
*self_big_boolean_function ^= rhs_small_boolean_function;
707-
}
708-
(BooleanFunctionType::Big, BooleanFunctionType::Big) => {
709-
let self_big_boolean_function = self
710-
.as_any_mut()
711-
.downcast_mut::<BigBooleanFunction>()
712-
.unwrap();
713-
let rhs_big_boolean_function =
714-
rhs.as_any().downcast_ref::<BigBooleanFunction>().unwrap();
715-
*self_big_boolean_function ^= rhs_big_boolean_function.clone();
675+
*self_big_boolean_function ^= rhs_big_boolean_function;
676+
},
677+
(BooleanFunction::Big(self_big_boolean_function), BooleanFunction::Big(rhs_big_boolean_function)) => {
678+
*self_big_boolean_function ^= rhs_big_boolean_function;
716679
}
717680
}
718681
}
@@ -880,7 +843,7 @@ impl BooleanFunction {
880843
)?)
881844
.into());
882845
}
883-
Ok((BigBooleanFunction::from_truth_table(truth_table.clone(), num_variables)).into())
846+
Ok(BigBooleanFunction::from_truth_table(truth_table.clone(), num_variables).into())
884847
}
885848
}
886849

src/small_boolean_function.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use fast_boolean_anf_transform::fast_bool_anf_transform_unsigned;
1111
use itertools::{enumerate, Itertools};
1212
use num_bigint::BigUint;
1313
use num_integer::binomial;
14-
use std::any::Any;
1514
use std::ops::{BitXor, BitXorAssign, Not};
1615

1716
/// Struct representing a boolean function with a big truth table.
@@ -354,14 +353,6 @@ impl BooleanFunctionImpl for SmallBooleanFunction {
354353
fn biguint_truth_table(&self) -> BigUint {
355354
BigUint::from(self.truth_table)
356355
}
357-
358-
fn as_any(&self) -> &dyn Any {
359-
self
360-
}
361-
362-
fn as_any_mut(&mut self) -> &mut dyn Any {
363-
self
364-
}
365356
}
366357

367358
/// In-place XOR operator for Boolean functions truth tables.

0 commit comments

Comments
 (0)