msoeken / kitty

C++ truth table library
MIT License
50 stars 77 forks source link

operator== on truth tables? #88

Open mfernan2 opened 4 years ago

mfernan2 commented 4 years ago

static_truth_table and dynamic_truth_table do not implement operator== and operator!=, so code like tt1 == tt2 does not work. It seems you still can compare truth tables with something like the following (untested) code:

bool eq = true;
for (auto it = tt1.begin(), it2 = tt2.begin(); ; ++it, ++it2) {
  if (it == tt1.end()) {
    eq &= it2 == tt2.end();
    break;
  } else if (it2 == tt2.end()) {
    eq = false;
    break;
  }
  eq &= *it == *it2;
}

Is it possible to implement operator== and operator!= on these classes to avoid having to do this? It should be possible to do some template-based specialization to optimize these too.

OTOH perhaps I've misunderstood something and equality comparison makes no sense on these classes. In which case, I would be interested to learn of this too.

msoeken commented 4 years ago

These functions are implemented in the header kitty/operators.hpp.

mfernan2 commented 4 years ago

Ah thanks! I did not even notice these. FWIW I ended up implementing something pretty similar but using SFINAE to ensure the functions are only callable with truth tables:

template<typename T1, typename T2,
  // make this only compile for Kitty’s truth table types
  typename std::enable_if<kitty::is_truth_table<T1>::value && kitty::is_truth_table<T2>::value>::type* = nullptr>
static bool equal(const T1 &x, const T2 &y) {
...
mfernan2 commented 4 years ago

Actually I just realized why I ran into this issue in the first place. Kitty does not implement operator== for a dynamic truth table compared to a static truth table. I.e.:

template<int NumVars>
bool operator==( const static_truth_table<NumVars>& first, const dynamic_truth_table& second );

Would it be possible to implement a comparator that handles this case as well? I do not seem to have the ability to re-open this issue.

msoeken commented 4 years ago

That's a good point.