Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cube constructors. #29

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ v0.3 (not yet released)
* Constructors: ``create_from_chain``
`#28 <https://github.com/msoeken/kitty/pull/28>`_

* Cube constructors: ``pos_cube``, ``neg_cube``
`#29 <https://github.com/msoeken/kitty/pull/29>`_

v0.2 (December 21, 2017)
------------------------

Expand Down
54 changes: 29 additions & 25 deletions docs/cube.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,34 @@ e.g., ``isop`` and ``esop_from_optimum_pkrm``.
The class :cpp:class:`kitty::cube` provides the following public
member functions.

+--------------------------------------+---------------------------------------------------------------+
| Function | Description |
+======================================+===============================================================+
| ``cube()`` | Constructs the empty cube |
+--------------------------------------+---------------------------------------------------------------+
| ``cube(bits, mask)`` | Constructs a cube from bits and mask |
+--------------------------------------+---------------------------------------------------------------+
| ``num_literals()`` | Returns number of literals |
+--------------------------------------+---------------------------------------------------------------+
| ``distance(that)`` | Returns the distance to another cube |
+--------------------------------------+---------------------------------------------------------------+
| ``operator==(that)`` | Checks whether two cubes are equivalent |
+--------------------------------------+---------------------------------------------------------------+
| ``operator!=(that)`` | Checks whether two cubes are not equivalent |
+--------------------------------------+---------------------------------------------------------------+
| ``merge(that)`` | Merges two cubes of distance-1 |
+--------------------------------------+---------------------------------------------------------------+
| ``add_literal(var_index, polarity)`` | Add literal to cube |
+--------------------------------------+---------------------------------------------------------------+
| ``remove_literal(var_index)`` | Removes literal from cube |
+--------------------------------------+---------------------------------------------------------------+
| ``nth_var_cube(var_index)`` | Constructs the elementary cube representing a single variable |
+--------------------------------------+---------------------------------------------------------------+
| ``print(length, os)`` | Prints a cube |
+--------------------------------------+---------------------------------------------------------------+
+--------------------------------------+-------------------------------------------------------------------------+
| Function | Description |
+======================================+=========================================================================+
| ``cube()`` | Constructs the empty cube |
+--------------------------------------+-------------------------------------------------------------------------+
| ``cube(bits, mask)`` | Constructs a cube from bits and mask |
+--------------------------------------+-------------------------------------------------------------------------+
| ``num_literals()`` | Returns number of literals |
+--------------------------------------+-------------------------------------------------------------------------+
| ``distance(that)`` | Returns the distance to another cube |
+--------------------------------------+-------------------------------------------------------------------------+
| ``operator==(that)`` | Checks whether two cubes are equivalent |
+--------------------------------------+-------------------------------------------------------------------------+
| ``operator!=(that)`` | Checks whether two cubes are not equivalent |
+--------------------------------------+-------------------------------------------------------------------------+
| ``merge(that)`` | Merges two cubes of distance-1 |
+--------------------------------------+-------------------------------------------------------------------------+
| ``add_literal(var_index, polarity)`` | Add literal to cube |
+--------------------------------------+-------------------------------------------------------------------------+
| ``remove_literal(var_index)`` | Removes literal from cube |
+--------------------------------------+-------------------------------------------------------------------------+
| ``nth_var_cube(var_index)`` | Constructs the elementary cube representing a single variable |
+--------------------------------------+-------------------------------------------------------------------------+
| ``pos_cube(k)`` | Constructs the elementary cube containing the first k positive literals |
+--------------------------------------+-------------------------------------------------------------------------+
| ``neg_cube(k)`` | Constructs the elementary cube containing the first k negative literals |
+--------------------------------------+-------------------------------------------------------------------------+
| ``print(length, os)`` | Prints a cube |
+--------------------------------------+-------------------------------------------------------------------------+

You can use ``kitty::hash<cube>`` to hash cubes, e.g., in a hash map or set.
14 changes: 14 additions & 0 deletions include/kitty/cube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ class cube
return cube( _bits, _bits );
}

/*! \brief Constructs the elementary cube containing the first k positive literals */
static cube pos_cube( uint8_t k )
{
const uint32_t _bits = ( uint64_t( 1 ) << k ) - 1;
return cube( _bits, _bits );
}

/*! \brief Constructs the elementary cube containing the first k negative literals */
static cube neg_cube( uint8_t k )
{
const uint32_t _bits = ( uint64_t( 1 ) << k ) - 1;
return cube( 0u, _bits );
}

/*! \brief Prints a cube */
inline void print( unsigned length = 32u, std::ostream& os = std::cout ) const
{
Expand Down
8 changes: 8 additions & 0 deletions test/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ TEST_F( CubeTest, distance )
EXPECT_EQ( cube().distance( cube( 0, 2 ) ), 1 );
}

TEST_F( CubeTest, pos_neg_cube )
{
for ( auto i = 0; i <= 32; ++i )
{
EXPECT_EQ( to_string( cube::pos_cube( i ), i ), std::string( i, '1' ) );
EXPECT_EQ( to_string( cube::neg_cube( i ), i ), std::string( i, '0' ) );
}
}