@@ -33,6 +33,7 @@ pub use small_boolean_function::SmallBooleanFunction;
33
33
use std:: collections:: { HashMap , HashSet } ;
34
34
use std:: fmt:: Debug ;
35
35
use std:: ops:: { Add , AddAssign , BitAnd , BitAndAssign , BitXor , BitXorAssign , Mul , MulAssign , Not } ;
36
+ use hackfn:: hackfn;
36
37
use crate :: iterator:: CloseBalancedFunctionIterator ;
37
38
38
39
/// Internal representation of Boolean function
@@ -72,6 +73,8 @@ pub trait BooleanFunctionImpl: Debug {
72
73
73
74
/// Computes the value of the Boolean function for a given input, as a 32-bit unsigned integer.
74
75
///
76
+ /// This is equivalent of calling directly your Boolean function object as a function.
77
+ ///
75
78
/// # Parameters
76
79
/// - `input_bits`: The input value for which to compute the Boolean function value, the least significant bit being the first variable.
77
80
///
@@ -80,6 +83,16 @@ pub trait BooleanFunctionImpl: Debug {
80
83
///
81
84
/// # Panics
82
85
/// If the input value is greater than the maximum input value, and the `unsafe_disable_safety_checks` feature is not enabled.
86
+ ///
87
+ /// # Example
88
+ /// ```rust
89
+ /// use boolean_function::BooleanFunction;
90
+ /// use boolean_function::BooleanFunctionImpl;
91
+ ///
92
+ /// let boolean_function = BooleanFunction::from_hex_string_truth_table("abce1234").unwrap();
93
+ /// assert_eq!(boolean_function.compute_cellular_automata_rule(8), false);
94
+ /// assert_eq!(boolean_function(8), false); // directly as a function
95
+ /// ```
83
96
fn compute_cellular_automata_rule ( & self , input_bits : u32 ) -> bool ;
84
97
85
98
/// Computes the Walsh-Hadamard transform of the Boolean function for a given point.
@@ -890,6 +903,13 @@ impl TryFrom<&str> for BooleanFunction {
890
903
}
891
904
}
892
905
906
+ #[ hackfn]
907
+ impl BooleanFunction {
908
+ fn call ( & self , input_bits : u32 ) -> bool {
909
+ self . compute_cellular_automata_rule ( input_bits)
910
+ }
911
+ }
912
+
893
913
impl BooleanFunction {
894
914
/// Creates a new BooleanFunction from a hexadecimal string representing the truth table.
895
915
///
@@ -1168,6 +1188,12 @@ mod tests {
1168
1188
assert_eq ! ( boolean_function. compute_cellular_automata_rule( 8 ) , false ) ;
1169
1189
assert_eq ! ( boolean_function. compute_cellular_automata_rule( 23 ) , true ) ;
1170
1190
1191
+ assert_eq ! ( boolean_function( 0 ) , false ) ;
1192
+ assert_eq ! ( boolean_function( 1 ) , false ) ;
1193
+ assert_eq ! ( boolean_function( 4 ) , true ) ;
1194
+ assert_eq ! ( boolean_function( 8 ) , false ) ;
1195
+ assert_eq ! ( boolean_function( 23 ) , true ) ;
1196
+
1171
1197
let boolean_function =
1172
1198
BooleanFunction :: from_hex_string_truth_table ( "7969817CC5893BA6AC326E47619F5AD0" )
1173
1199
. unwrap ( ) ;
@@ -1176,6 +1202,12 @@ mod tests {
1176
1202
assert_eq ! ( boolean_function. compute_cellular_automata_rule( 64 ) , false ) ;
1177
1203
assert_eq ! ( boolean_function. compute_cellular_automata_rule( 80 ) , true ) ;
1178
1204
assert_eq ! ( boolean_function. compute_cellular_automata_rule( 100 ) , true ) ;
1205
+
1206
+ assert_eq ! ( boolean_function( 13 ) , false ) ;
1207
+ assert_eq ! ( boolean_function( 62 ) , false ) ;
1208
+ assert_eq ! ( boolean_function( 64 ) , false ) ;
1209
+ assert_eq ! ( boolean_function( 80 ) , true ) ;
1210
+ assert_eq ! ( boolean_function( 100 ) , true ) ;
1179
1211
}
1180
1212
1181
1213
#[ test]
0 commit comments