28
28
\brief Implements functions to print truth tables
29
29
30
30
\author Mathias Soeken
31
+ \author Rassul Bairamkulov
31
32
*/
32
33
33
34
#pragma once
45
46
#include " karnaugh_map.hpp"
46
47
#include " operations.hpp"
47
48
#include " constructors.hpp"
49
+ #include " isop.hpp"
48
50
49
51
namespace kitty
50
52
{
@@ -478,4 +480,91 @@ std::string anf_to_expression( const ternary_truth_table<TT>& anf )
478
480
return anf_to_expression ( anf._bits );
479
481
}
480
482
483
+ /* ! \brief Creates an expression in the sum of products format.
484
+
485
+ Does not perform any optimizations. Useful when constructing GENLIB-compatible expressions
486
+
487
+ \param tt Truth table
488
+ \param os Output stream
489
+ */
490
+ template <typename TT, typename = std::enable_if_t <is_completely_specified_truth_table<TT>::value>>
491
+ void print_sop_expression ( TT tt, std::ostream& os = std::cout )
492
+ {
493
+ /* compare to constants */
494
+ if ( is_const0 ( tt ) )
495
+ {
496
+ os << " CONST0" ;
497
+ return ;
498
+ }
499
+ else if ( is_const0 ( ~tt ) )
500
+ {
501
+ os << " CONST1" ;
502
+ return ;
503
+ }
504
+
505
+ /* extract product terms */
506
+ auto cubes = kitty::isop ( tt );
507
+
508
+ bool first_cube = true ; // Controls insertion of '|'. It's false for the first cube to avoid leading '|'.
509
+
510
+ /* write product terms */
511
+ for ( auto cube : cubes )
512
+ {
513
+ auto bits = cube._bits ;
514
+ auto mask = cube._mask ;
515
+
516
+ bool brackets = __builtin_popcount ( mask ) > 1 ;
517
+
518
+ if ( !first_cube )
519
+ {
520
+ os << ' |' ;
521
+ }
522
+
523
+ if ( brackets )
524
+ {
525
+ os << ' (' ;
526
+ }
527
+
528
+ bool first_literal = true ;
529
+ for ( auto i = 0u ; i < tt.num_vars (); ++i )
530
+ {
531
+ if ( mask & 1 )
532
+ {
533
+ if ( !first_literal )
534
+ {
535
+ os << ' &' ;
536
+ }
537
+ if ( !( bits & 1 ) )
538
+ {
539
+ os << ' !' ;
540
+ }
541
+ os << static_cast <char >( ' a' + i );
542
+ first_literal = false ;
543
+ }
544
+ bits >>= 1 ;
545
+ mask >>= 1 ;
546
+ }
547
+
548
+ if ( brackets )
549
+ {
550
+ os << ' )' ;
551
+ }
552
+ first_cube = false ;
553
+ }
554
+ }
555
+
556
+ /* ! \brief Creates an expression in the sum of products format.
557
+
558
+ Does not perform any optimizations. Useful when constructing GENLIB-compatible expressions
559
+
560
+ \param tt Truth table
561
+ */
562
+ template <typename TT, typename = std::enable_if_t <is_completely_specified_truth_table<TT>::value>>
563
+ std::string to_sop_expression ( TT tt )
564
+ {
565
+ std::stringstream os;
566
+ print_sop_expression ( tt, os ); // Use the function to write into the stringstream
567
+ return os.str (); // Convert the stringstream to string and return it
568
+ }
569
+
481
570
} /* namespace kitty */
0 commit comments