How do I join two or more merge plans?

I want to create an associative sequence of two types boost::fusion::map

. The types contained in one of the cards can exist in the other, and if so, I want to end up with only one type with that key in the resulting sequence. That is, I want the keys to be unique after joining.

A regular join operation allows duplicate keys, so this doesn't seem like a solution. Does anyone know how I can achieve this?

// Here is what I've got:
using namespace boost::fusion;
map<
  pair<int, int>,
  pair<double, int>> Map1;

map<
  pair<bool, int>,
  pair<double, int>> Map2;

// I want to join Map1 with Map2 such that I have
static_assert(std::is_same<Map3, map<
  pair<int, int>,
  pair<double, int>,
  pair<bool, int>>>::value, "");

      

+3


source to share


1 answer


You may have to manually root out the deception: in the complete C ++ 14 Live On Coliru suite

auto r = 
    as_map(
        fold(
            fold(m1, m2, [](auto accum, auto elem) { return erase_key<typename decltype(elem)::first_type>(accum); }),
            m1, 
            [](auto accum, auto elem) { return insert(accum, boost::fusion::end(accum), elem); }
        )); 

      

It's funky. If you replace it with functors instead of lambdas, you end up similarly:

auto r = 
    as_map(
        fold(
            fold(m1, m2, erase_corresponding()), 
            m1, 
            insert_helper()
        ));

      



A simple Live On Coliru implementation still relies on preliminary C ++ 1y support:

 struct erase_corresponding {
    template<typename T, typename U> 
        auto operator()(T map, U elem) const {
            return boost::fusion::erase_key<typename U::first_type>(map);
        }
};

struct insert_helper {
    template<typename T, typename U> 
        auto operator()(T map, U elem) const {
            return boost::fusion::insert(map, boost::fusion::end(map), elem);
        }
};

      

However, to make the whole thing C ++ 03 proof you need to specify it with RESULT_OF (which I leave as an exercise for the reader)

+3


source







All Articles