Skip to content

Commit 845c443

Browse files
authored
Merge pull request #42 from msoeken/shrink
Shrink truth tables.
2 parents 6243d35 + dba682c commit 845c443

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

docs/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ v0.4 (not yet released)
77
* Constructors: ``create_from_clauses``, ``create_characteristic``
88
`#41 <https://github.com/msoeken/kitty/pull/41>`_
99

10+
* Operations: ``shrink_to``
11+
`#42 <https://github.com/msoeken/kitty/pull/42>`_
12+
1013
* CNF generation: ``cnf_characteristic``
1114
`#41 <https://github.com/msoeken/kitty/pull/41>`_
1215

docs/operations.rst

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Combination and manipulation
3737
min_base_inplace
3838
expand_inplace
3939
extend_to
40+
shrink_to
4041
shift_left_inplace
4142
shift_left
4243
shift_right_inplace

include/kitty/operations.hpp

+41-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ inline TT unary_not_if( const TT& tt, bool cond )
6060

6161
/*! \brief Bitwise AND of two truth tables */
6262
template<typename TT>
63+
6364
inline TT binary_and( const TT& first, const TT& second )
6465
{
6566
return binary_operation( first, second, std::bit_and<>() );
@@ -700,7 +701,7 @@ void extend_to( TT& tt, const TTFrom& from )
700701

701702
/*! \brief Extends smaller truth table to larger static one
702703
703-
This is a special version of extend_to that has the truth table as a return
704+
This is a special version of `extend_to` that has the truth table as a return
704705
value. It only works for creating static truth tables. The template
705706
parameter `NumVars` must be equal or larger to the number of variables in
706707
`from`.
@@ -715,6 +716,45 @@ inline static_truth_table<NumVars> extend_to( const TTFrom& from )
715716
return tt;
716717
}
717718

719+
/*! \brief Shrinks larger truth table to smaller one
720+
721+
The function expects that the most significant bits, which are cut off, are
722+
not in the functional support of the original function. Only then it is
723+
ensured that the resulting function is equivalent.
724+
725+
\param tt Smaller truth table to create
726+
\param from Larger truth table to copy from
727+
*/
728+
template<typename TT, typename TTFrom>
729+
void shrink_to( TT& tt, const TTFrom& from )
730+
{
731+
assert( tt.num_vars() <= from.num_vars() );
732+
733+
std::copy( from.begin(), from.begin() + tt.num_blocks(), tt.begin() );
734+
735+
if ( tt.num_vars() < 6 )
736+
{
737+
tt.mask_bits();
738+
}
739+
}
740+
741+
/*! \brief Shrinks larger truth table to smaller static one
742+
743+
This is a special version of `shrink_to` that has the truth table as a return
744+
value. It only works for creating static truth tables. The template
745+
parameter `NumVars` must be equal or smaller to the number of variables in
746+
`from`.
747+
748+
\param from Smaller truth table to copy from
749+
*/
750+
template<int NumVars, typename TTFrom>
751+
inline static_truth_table<NumVars> shrink_to( const TTFrom& from )
752+
{
753+
static_truth_table<NumVars> tt;
754+
shrink_to( tt, from );
755+
return tt;
756+
}
757+
718758
/*! \brief Left-shift truth table
719759
720760
Drops overflowing most-significant bits and fills up least-significant bits

test/operations.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,21 @@ TEST_F( OperationsTest, extend_to_same )
494494
EXPECT_EQ( tt1, tt2 );
495495
}
496496

497+
TEST_F( OperationsTest, shrink_to )
498+
{
499+
EXPECT_EQ( shrink_to<2>( from_hex<3>( "aa" ) ), from_hex<2>( "a" ) );
500+
EXPECT_EQ( shrink_to<2>( from_hex<3>( "1a" ) ), from_hex<2>( "a" ) );
501+
EXPECT_EQ( shrink_to<2>( from_hex<2>( "8" ) ), from_hex<2>( "8" ) );
502+
503+
EXPECT_EQ( shrink_to<5>( from_hex<7>( "cafecafecafecafecafecafecafecafe" ) ), from_hex<5>( "cafecafe") );
504+
505+
EXPECT_EQ( shrink_to<2>( from_hex( 3, "aa" ) ), from_hex<2>( "a" ) );
506+
EXPECT_EQ( shrink_to<2>( from_hex( 3, "1a" ) ), from_hex<2>( "a" ) );
507+
EXPECT_EQ( shrink_to<2>( from_hex( 2, "8" ) ), from_hex<2>( "8" ) );
508+
509+
EXPECT_EQ( shrink_to<5>( from_hex( 7, "cafecafecafecafecafecafecafecafe" ) ), from_hex<5>( "cafecafe") );
510+
}
511+
497512
TEST_F( OperationsTest, shift_left )
498513
{
499514
EXPECT_EQ( shift_left( from_hex<3>( "e8"), 1 ), from_hex<3>( "d0" ) );

0 commit comments

Comments
 (0)