Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare tuples for equivalent types disregarding type order?

I'm looking for a way to compare two tuples to see if they contain the same types.
The order of the types does not matter. As long as there is a one to one mapping between the types of the two tuples, I will consider them equivalent.

Here is a small test I have set up.
I am having trouble implementing equivalent_types():

#include <iostream>
#include <utility>
#include <tuple>
#include <functional>

template <typename T, typename U>
bool equivalent_types(T t, U u){
    return (std::tuple_size<T>::value == std::tuple_size<U>::value);
    //&& same types regardless of order
}


int main() {

    //these tuples have the same size and hold the same types.
    //regardless of the type order, I consider them equivalent.  
    std::tuple<int,float,char,std::string> a;
    std::tuple<std::string,char,int,float> b;

    std::cout << equivalent_types(a,b) << '\n'; //should be true
    std::cout << equivalent_types(b,a) << '\n'; //should be true

    //examples that do not work:  

    //missing a type (not enough types)
    std::tuple<std::string,char,int> c;

    //duplicate type (too many types)
    std::tuple<std::string,char,int,float,float> d;

    //wrong type
    std::tuple<bool,char,int,float> e;

    std::cout << equivalent_types(a,c) << '\n'; //should be false
    std::cout << equivalent_types(a,d) << '\n'; //should be false
    std::cout << equivalent_types(a,e) << '\n'; //should be false
}
like image 788
Trevor Hickey Avatar asked Oct 18 '16 21:10

Trevor Hickey


People also ask

How do you compare two tuples?

Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on.

How do you compare tuples to a list?

Tuples are compared position by position means the first item of a tuple compared with the first item of the second tuple and so on. If all the items will equal then the result will be true.

How do you check if two tuples are equal in Python?

Practical Data Science using Python When it is required to check if two list of tuples are identical, the '==' operator is used. The '==' operator checks to see if two iterables are equal or not.


1 Answers

By counting types of both tuples, you may do something like:

template <typename T, typename Tuple>
struct type_counter;

template <typename T, typename ... Ts>
struct type_counter<T, std::tuple<Ts...>> :
    std::integral_constant<std::size_t, (... + std::is_same<T, Ts>::value)> {};

template <typename Tuple1, typename Tuple2, std::size_t... Is>
constexpr bool equivalent_types(const Tuple1&, const Tuple2&, std::index_sequence<Is...>)
{
    return (...
            && (type_counter<std::tuple_element_t<Is, Tuple1>, Tuple1>::value
               == type_counter<std::tuple_element_t<Is, Tuple1>, Tuple2>::value));
}

template <typename Tuple1, typename Tuple2>
constexpr bool equivalent_types(const Tuple1& t1, const Tuple2& t2)
{
    constexpr auto s1 = std::tuple_size<Tuple1>::value;
    constexpr auto s2 = std::tuple_size<Tuple2>::value;

    return s1 == s2
      && equivalent_types(t1, t2, std::make_index_sequence<std::min(s1, s2)>());
}

Demo C++17
Demo C++14

I use c++17 for folding expression but it can be rewritten as constexpr function easily.

like image 126
Jarod42 Avatar answered Oct 14 '22 17:10

Jarod42