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 from string #31

Merged
merged 3 commits into from
Jan 17, 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: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +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``
* Cube constructors: ``pos_cube``, ``neg_cube``, constructor to create cube from string
`#29 <https://github.com/msoeken/kitty/pull/29>`_
`#31 <https://github.com/msoeken/kitty/pull/31>`_

* Cube methods: ``get_bit``, ``get_mask``, ``set_bit``, ``set_mask``, ``clear_bit``, ``clear_mask`` (contributed by Heinz Riener)
`#30 <https://github.com/msoeken/kitty/pull/30>`_
Expand Down
5 changes: 5 additions & 0 deletions docs/cube.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ member functions.
+--------------------------------------+-------------------------------------------------------------------------+
| ``cube(bits, mask)`` | Constructs a cube from bits and mask |
+--------------------------------------+-------------------------------------------------------------------------+
| ``cube(str)`` | Constructs a cube from a string |
+--------------------------------------+-------------------------------------------------------------------------+
| ``num_literals()`` | Returns number of literals |
+--------------------------------------+-------------------------------------------------------------------------+
| ``distance(that)`` | Returns the distance to another cube |
Expand Down Expand Up @@ -52,3 +54,6 @@ member functions.
+--------------------------------------+-------------------------------------------------------------------------+

You can use ``kitty::hash<cube>`` to hash cubes, e.g., in a hash map or set.

.. doxygenclass:: kitty::cube
:members:
35 changes: 35 additions & 0 deletions include/kitty/cube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <functional>
#include <iostream>
#include <string>

#include "hash.hpp"

Expand All @@ -59,6 +60,40 @@ class cube
*/
cube( uint32_t bits, uint32_t mask ) : _bits( bits ), _mask( mask ) {}

/*! \brief Constructs a cube from a string

Each character corresponds to one literal in the cube. Only up to first 32
characters of the string will be considered, since this data structure
cannot represent cubes with more than 32 literals. A '1' in the string
corresponds to a postive literal, a '0' corresponds to a negative literal.
All other characters represent don't care, but it is customary to use '-'.

\param str String representing a cube
*/
explicit cube( const std::string& str )
{
_bits = _mask = 0u;

auto p = str.begin();

for ( uint64_t i = 1; i <= ( uint64_t( 1u ) << 32u ); i <<= 1 )
{
switch ( *p )
{
default: /* don't care */
break;
case '1':
_bits |= i;
/* no break on purpose, jump to 0 and set mask */
case '0':
_mask |= i;
break;
}

if ( ++p == str.end() ) return;
}
}

/*! \brief Returns number of literals */
inline int num_literals() const
{
Expand Down
11 changes: 11 additions & 0 deletions test/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ TEST_F( CubeTest, constructing_and_printing )
EXPECT_EQ( to_string( cube( 0, 2 ), 2 ), "-0" );
}

TEST_F( CubeTest, construction_from_string )
{
EXPECT_EQ( to_string( cube( "010" ), 3 ), "010" );
EXPECT_EQ( to_string( cube( "-1-" ), 3 ), "-1-" );
EXPECT_EQ( to_string( cube( "*00" ), 3 ), "-00" );

EXPECT_EQ( to_string( cube( "" ), 0 ), "" );
EXPECT_EQ( to_string( cube( std::string( 32, '1' ) ), 32 ), std::string( 32, '1' ) );
EXPECT_EQ( to_string( cube( std::string( 33, '1' ) ), 32 ), std::string( 32, '1' ) );
}

TEST_F( CubeTest, distance )
{
EXPECT_EQ( cube().distance( cube( 0, 2 ) ), 1 );
Expand Down