Skip to content

Commit ede2d3a

Browse files
authored
Merge pull request #54 from msoeken/assignment
Assignment operator.
2 parents 2721a9b + 2549e0d commit ede2d3a

6 files changed

+112
-9
lines changed

docs/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ v0.5 (not yet released)
1010
* Type traits: ``is_truth_table``
1111
`#53 <https://github.com/msoeken/kitty/pull/53>`_
1212

13+
* Assignment operator for truth tables
14+
`#54 <https://github.com/msoeken/kitty/pull/54>`_
15+
1316
v0.4 (May 4, 2018)
1417
------------------
1518

docs/truth_table_data_structures.rst

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ following public member functions.
4040
+-----------------------------------+----------------------------------------------------------------------------------+
4141
| ``crend()`` | Constant reverse end iterator to bits. |
4242
+-----------------------------------+----------------------------------------------------------------------------------+
43+
| ``operator=(other)`` | Assignment operator. |
44+
+-----------------------------------+----------------------------------------------------------------------------------+
4345
| ``mask_bits()`` | Masks the number of valid truth table bits. |
4446
+-----------------------------------+----------------------------------------------------------------------------------+
4547

@@ -89,6 +91,8 @@ following public member functions.
8991
+--------------------------+---------------------------------------------------------------------------------+
9092
| ``crend()`` | Constant reverse end iterator to bits. |
9193
+--------------------------+---------------------------------------------------------------------------------+
94+
| ``operator=(other)`` | Assignment operator. |
95+
+--------------------------+---------------------------------------------------------------------------------+
9296
| ``mask_bits()`` | Masks the number of valid truth table bits. |
9397
+--------------------------+---------------------------------------------------------------------------------+
9498

include/kitty/dynamic_truth_table.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
#pragma once
3434

3535
#include <cstdint>
36+
#include <type_traits>
3637
#include <vector>
3738

3839
#include "detail/constants.hpp"
40+
#include "traits.hpp"
3941

4042
namespace kitty
4143
{
@@ -128,6 +130,28 @@ struct dynamic_truth_table
128130
*/
129131
inline auto crend() const noexcept { return _bits.crend(); }
130132

133+
/*! \brief Assign other truth table.
134+
135+
This replaces the current truth table with another truth table. The truth
136+
table type is arbitrary. The vector of bits is resized accordingly.
137+
138+
\param other Other truth table
139+
*/
140+
template<class TT, typename = std::enable_if_t<is_truth_table<TT>::value>>
141+
dynamic_truth_table& operator=( const TT& other )
142+
{
143+
_bits.resize( other.num_blocks() );
144+
std::copy( other.begin(), other.end(), begin() );
145+
_num_vars = other.num_vars();
146+
147+
if ( _num_vars < 6 )
148+
{
149+
mask_bits();
150+
}
151+
152+
return *this;
153+
}
154+
131155
/*! Masks the number of valid truth table bits.
132156
133157
If the truth table has less than 6 variables, it may not use all
@@ -148,4 +172,7 @@ struct dynamic_truth_table
148172
int _num_vars;
149173
/*! \endcond */
150174
};
175+
176+
template<>
177+
struct is_truth_table<kitty::dynamic_truth_table> : std::true_type {};
151178
} // namespace kitty

include/kitty/static_truth_table.hpp

+43
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <cstdint>
3737

3838
#include "detail/constants.hpp"
39+
#include "traits.hpp"
3940

4041
namespace kitty
4142
{
@@ -120,6 +121,25 @@ struct static_truth_table<NumVars, true>
120121
*/
121122
inline auto crend() const noexcept { return ( &_bits ) + 1; }
122123

124+
/*! \brief Assign other truth table if number of variables match.
125+
126+
This replaces the current truth table with another truth table, if `other`
127+
has the same number of variables. Otherwise, the truth table is not
128+
changed.
129+
130+
\param other Other truth table
131+
*/
132+
template<class TT, typename = std::enable_if_t<is_truth_table<TT>::value>>
133+
static_truth_table<NumVars>& operator=( const TT& other )
134+
{
135+
if ( other.num_vars() == num_vars() )
136+
{
137+
std::copy( other.begin(), other.end(), begin() );
138+
}
139+
140+
return *this;
141+
}
142+
123143
/*! Masks the number of valid truth table bits.
124144
125145
If the truth table has less than 6 variables, it may not use all
@@ -220,6 +240,25 @@ struct static_truth_table<NumVars, false>
220240
*/
221241
inline auto crend() const noexcept { return _bits.crend(); }
222242

243+
/*! \brief Assign other truth table if number of variables match.
244+
245+
This replaces the current truth table with another truth table, if `other`
246+
has the same number of variables. Otherwise, the truth table is not
247+
changed.
248+
249+
\param other Other truth table
250+
*/
251+
template<class TT, typename = std::enable_if_t<is_truth_table<TT>::value>>
252+
static_truth_table<NumVars>& operator=( const TT& other )
253+
{
254+
if ( other.num_vars() == num_vars() )
255+
{
256+
std::copy( other.begin(), other.end(), begin() );
257+
}
258+
259+
return *this;
260+
}
261+
223262
/*! Masks the number of valid truth table bits.
224263
225264
We know that we will have at least 7 variables in this data
@@ -232,4 +271,8 @@ struct static_truth_table<NumVars, false>
232271
std::array<uint64_t, NumBlocks> _bits;
233272
/*! \endcond */
234273
};
274+
275+
template<int NumVars>
276+
struct is_truth_table<kitty::static_truth_table<NumVars>> : std::true_type {};
277+
235278
} // namespace kitty

include/kitty/traits.hpp

-9
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,10 @@
3434

3535
#include <type_traits>
3636

37-
#include "dynamic_truth_table.hpp"
38-
#include "static_truth_table.hpp"
39-
4037
namespace kitty
4138
{
4239

4340
template<class TT>
4441
struct is_truth_table : std::false_type {};
4542

46-
template<>
47-
struct is_truth_table<kitty::dynamic_truth_table> : std::true_type {};
48-
49-
template<int NumVars>
50-
struct is_truth_table<kitty::static_truth_table<NumVars>> : std::true_type {};
51-
5243
}

test/operations.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,38 @@ TEST_F( OperationsTest, majority_odd_conjecture )
746746

747747
EXPECT_EQ( maj, ternary_majority( nth<n>( n - 1 ), f1_e, f2_e ) );
748748
}
749+
750+
TEST_F( OperationsTest, copy_truth_tables )
751+
{
752+
kitty::dynamic_truth_table tt1( 3 ), tt2( 7 );
753+
kitty::static_truth_table<3> tt3;
754+
kitty::static_truth_table<7> tt4;
755+
756+
kitty::create_random( tt1 );
757+
kitty::create_random( tt2 );
758+
kitty::create_random( tt3 );
759+
kitty::create_random( tt4 );
760+
761+
kitty::dynamic_truth_table tt;
762+
763+
tt = tt1;
764+
EXPECT_EQ( tt, tt1 );
765+
tt = tt2;
766+
EXPECT_EQ( tt, tt2 );
767+
tt = tt3;
768+
EXPECT_TRUE( std::equal( tt.begin(), tt.end(), tt3.begin() ) );
769+
tt = tt4;
770+
EXPECT_TRUE( std::equal( tt.begin(), tt.end(), tt4.begin() ) );
771+
772+
static_truth_table<3> tt_s3;
773+
tt_s3 = tt1;
774+
EXPECT_TRUE( std::equal( tt_s3.begin(), tt_s3.end(), tt1.begin() ) );
775+
tt_s3 = tt3;
776+
EXPECT_EQ( tt_s3, tt3 );
777+
778+
static_truth_table<7> tt_s7;
779+
tt_s7 = tt2;
780+
EXPECT_TRUE( std::equal( tt_s7.begin(), tt_s7.end(), tt2.begin() ) );
781+
tt_s7 = tt4;
782+
EXPECT_EQ( tt_s7, tt4 );
783+
}

0 commit comments

Comments
 (0)