0

I have the following example of a custom specialization of the std::set_union<...> algorithm. I adapted the implementation from from http://en.cppreference.com/w/cpp/algorithm/set_union.

The reason I need to customize the implementation is that I want to create pairs of elements from both sets. In the case where the ranges overlap (the intersection) the output will contain std::pair<*first_iter, *second_iter>

In the place where the elements are unique to the first set, the output will be conceptually std::pair<*first_iter, second_type()> and lastly where the elements are unique to the second set, the output will be std::pair.

The code works, however in my customization, I had to hard code the LoadableFile type in order to default construct elements for either side of the std::pair<> (depending on 2 of the 3 cases for specialization above). Is there some way to access the type info from the Merge template argument to know its type (it is a PairMaker < Loadable, Loadable > and default construct Loadable()'s without resorting to hard coding the specialization?

This is my customization:

// customized set_union that merges elements from both sets
// when the elements are unique in InputIt1 - merge constructs
// a pair with a default constructed type of InputIt1
template<typename InputIt1, typename InputIt2,
    typename OutputIt, typename Compare, 
    typename Merge>
OutputIt custom_set_union(
    InputIt1 first1, InputIt1 last1,
    InputIt2 first2, InputIt2 last2,
    OutputIt d_first, Compare comp,
    Merge merge)
{
    // example implementation taken and modified from cppreference.com
    for (; first1 != last1; ++d_first) {
        // empty second set
        if (first2 == last2) {
            // return std::copy(first1, last1, d_first);
            // equivalent of std::copy(...) from cppreference.com
            while (first1 != last1) {
                //*d_first++ = *first++;
                *d_first++ = merge(*first1++, LoadableFile());
            }
            return d_first;
        }
        if (comp(*first2, *first1)) {
            //*d_first = *first2++;
            *d_first = merge(LoadableFile(), *first2++);
        } else {
            //*d_first = *first1;
            // @JC note added *first2 as merge arg2 - overlapping region
            *d_first = merge(*first1, *first2);
            if (!comp(*first1, *first2))
                ++first2;
            ++first1;
        }
    }
    // return std::copy(first2, last2, d_first);
    // equivalent of std::copy(...) from cppreference.com
    while (first2 != last2) {
        //*d_first++ = *first++;
        *d_first++ = merge(LoadableFile(), *first2++);
    }
    return d_first;
};

In order to provide the required framework to make elements suitable for the OutputIt - I have a Merge class that makes pairs suitable for the OutputIt as follows:

/**
 * PairMaker helper struct to make pairs of objects from
 * 2 different types of sets templated on types A & B
 * must have default constructors - pair types A & B must
 * have default constructors
 */
template<typename A, typename B>
struct PairMaker {
    std::pair<A, B> operator() (const A& a, const B& b) const {
        return std::make_pair(a, b);
    }
};

and in my case to actually use the specialization I call it as follows:

    auto comp = [](const LoadableFile& lhs, const LoadableFile& rhs) {
        return lhs.getRelativePath().filename() < rhs.getRelativePath().filename();
    };

    PairMaker<LoadableFile, LoadableFile> pairMaker;
    std::set<std::pair<LoadableFile,LoadableFile>> resultSet;
    custom_set_union(rLocalFileInfo.cbegin(), rLocalFileInfo.cend(),
        rModuleFileInfo.cbegin(), rModuleFileInfo.cend(), 
        std::inserter(resultSet, resultSet.end()),
        comp, pairMaker);
johnco3
  • 2,401
  • 4
  • 35
  • 67
  • 1
    You may be looking for `iterator_traits::value_type`, or simply `decltype(*first1)` – Igor Tandetnik Aug 28 '14 at 20:52
  • @Igor thanks, that worked, I simply put a std::iterator_traits::value_type inputType2; and then *d_first++ = merge(*first1++, inputType2); and it worked well. – johnco3 Aug 29 '14 at 00:03

0 Answers0