# hpx/parallel/container_algorithms/set_union.hpp¶

See Public API for a list of names and headers that are part of the public HPX API.

namespace `hpx`
namespace `ranges`

Functions

template<typename `ExPolicy`, typename `Iter1`, typename `Sent1`, typename `Iter2`, typename `Sent2`, typename `Iter3`, typename `Pred` = detail::less, typename `Proj1` = util::projection_identity, typename `Proj2` = util::projection_identity>
util::detail::algorithm_result<ExPolicy, ranges::set_union_result<Iter1, Iter2, Iter3>>::type `set_union`(ExPolicy &&policy, Iter1 first1, Sent1 last1, Iter2 first2, Sent2 last2, Iter3 dest, Pred &&op = Pred(), Proj1 &&proj1 = Proj1(), Proj2 &&proj2 = Proj2())

Constructs a sorted range beginning at dest consisting of all elements present in one or both sorted ranges [first1, last1) and [first2, last2). This algorithm expects both input ranges to be sorted with the given binary predicate f.

If some element is found

m times in [first1, last1) and n times in [first2, last2), then all m elements will be copied from [first1, last1) to dest, preserving order, and then exactly std::max(n-m, 0) elements will be copied from [first2, last2) to dest, also preserving order.
Note

Complexity: At most 2*(N1 + N2 - 1) comparisons, where N1 is the length of the first sequence and N2 is the length of the second sequence.

The resulting range cannot overlap with either of the input ranges.

The application of function objects in parallel algorithm invoked with a sequential execution policy object execute in sequential order in the calling thread (

sequenced_policy) or in a single new thread spawned from the current thread (for sequenced_task_policy).
Template Parameters
• `ExPolicy`: The type of the execution policy to use (deduced). It describes the manner in which the execution of the algorithm may be parallelized and the manner in which it applies user-provided function objects.

• `Iter1`: The type of the source iterators used (deduced) representing the first sequence. This iterator type must meet the requirements of an forward iterator.

• `Sent1`: The type of the end source iterators used (deduced). This iterator type must meet the requirements of an sentinel for Iter1.

• `Iter2`: The type of the source iterators used (deduced) representing the second sequence. This iterator type must meet the requirements of an forward iterator.

• `Sent2`: The type of the end source iterators used (deduced) representing the second sequence. This iterator type must meet the requirements of an sentinel for Iter2.

• `Iter3`: The type of the iterator representing the destination range (deduced). This iterator type must meet the requirements of an output iterator.

• `Pred`: The type of an optional function/function object to use. Unlike its sequential form, the parallel overload of set_union requires Pred to meet the requirements of CopyConstructible. This defaults to std::less<>

• `Proj1`: The type of an optional projection function applied to the first sequence. This defaults to util::projection_identity

• `Proj2`: The type of an optional projection function applied to the second sequence. This defaults to util::projection_identity

Parameters
• `policy`: The execution policy to use for the scheduling of the iterations.

• `first1`: Refers to the beginning of the sequence of elements of the first range the algorithm will be applied to.

• `last1`: Refers to the end of the sequence of elements of the first range the algorithm will be applied to.

• `first2`: Refers to the beginning of the sequence of elements of the second range the algorithm will be applied to.

• `last2`: Refers to the end of the sequence of elements of the second range the algorithm will be applied to.

• `dest`: Refers to the beginning of the destination range.

• `op`: The binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following:

```bool pred(const Type1 &a, const Type1 &b);
```

The signature does not need to have const &, but the function must not modify the objects passed to it. The type

Type1 must be such that objects of type InIter can be dereferenced and then implicitly converted to Type1

• `proj1`: Specifies the function (or function object) which will be invoked for each of the elements of the first sequence as a projection operation before the actual predicate op is invoked.

• `proj2`: Specifies the function (or function object) which will be invoked for each of the elements of the second sequence as a projection operation before the actual predicate op is invoked.

The application of function objects in parallel algorithm invoked with an execution policy object of type parallel_policy or parallel_task_policy are permitted to execute in an unordered fashion in unspecified threads, and indeterminately sequenced within each thread.

Return

The set_union algorithm returns a hpx::future<ranges::set_union_result<Iter1, Iter2, Iter3>> if the execution policy is of type sequenced_task_policy or parallel_task_policy and returns ranges::set_union_result<Iter1, Iter2, Iter3> otherwise. The set_union algorithm returns the output iterator to the element in the destination range, one past the last element copied.

template<typename `ExPolicy`, typename `Rng1`, typename `Rng2`, typename `Iter3`, typename `Pred` = detail::less, typename `Proj1` = util::projection_identity, typename `Proj2` = util::projection_identity>
util::detail::algorithm_result<ExPolicy, ranges::set_union_result<typename traits::range_iterator<Rng1>::type, typename traits::range_iterator<Rng2>::type, Iter3>>::type `set_union`(ExPolicy &&policy, Rng1 &&rng1, Rng2 &&rng2, Iter3 dest, Pred &&op = Pred(), Proj1 &&proj1 = Proj1(), Proj2 &&proj2 = Proj2())

Constructs a sorted range beginning at dest consisting of all elements present in one or both sorted ranges [first1, last1) and [first2, last2). This algorithm expects both input ranges to be sorted with the given binary predicate f.

If some element is found

m times in [first1, last1) and n times in [first2, last2), then all m elements will be copied from [first1, last1) to dest, preserving order, and then exactly std::max(n-m, 0) elements will be copied from [first2, last2) to dest, also preserving order.
Note

Complexity: At most 2*(N1 + N2 - 1) comparisons, where N1 is the length of the first sequence and N2 is the length of the second sequence.

The resulting range cannot overlap with either of the input ranges.

The application of function objects in parallel algorithm invoked with a sequential execution policy object execute in sequential order in the calling thread (

sequenced_policy) or in a single new thread spawned from the current thread (for sequenced_task_policy).
Template Parameters
• `ExPolicy`: The type of the execution policy to use (deduced). It describes the manner in which the execution of the algorithm may be parallelized and the manner in which it applies user-provided function objects.

• `Rng1`: The type of the source range used (deduced). The iterators extracted from this range type must meet the requirements of an input iterator.

• `Rng2`: The type of the source range used (deduced). The iterators extracted from this range type must meet the requirements of an input iterator.

• `Iter3`: The type of the iterator representing the destination range (deduced). This iterator type must meet the requirements of an output iterator.

• `Pred`: The type of an optional function/function object to use. Unlike its sequential form, the parallel overload of set_union requires Pred to meet the requirements of CopyConstructible. This defaults to std::less<>

• `Proj1`: The type of an optional projection function applied to the first sequence. This defaults to util::projection_identity

• `Proj2`: The type of an optional projection function applied to the second sequence. This defaults to util::projection_identity

Parameters
• `policy`: The execution policy to use for the scheduling of the iterations.

• `rng1`: Refers to the first sequence of elements the algorithm will be applied to.

• `rng2`: Refers to the second sequence of elements the algorithm will be applied to.

• `dest`: Refers to the beginning of the destination range.

• `op`: The binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following:

```bool pred(const Type1 &a, const Type1 &b);
```

The signature does not need to have const &, but the function must not modify the objects passed to it. The type

Type1 must be such that objects of type InIter can be dereferenced and then implicitly converted to Type1

• `proj1`: Specifies the function (or function object) which will be invoked for each of the elements of the first sequence as a projection operation before the actual predicate op is invoked.

• `proj2`: Specifies the function (or function object) which will be invoked for each of the elements of the second sequence as a projection operation before the actual predicate op is invoked.

The application of function objects in parallel algorithm invoked with an execution policy object of type parallel_policy or parallel_task_policy are permitted to execute in an unordered fashion in unspecified threads, and indeterminately sequenced within each thread.

Return

The set_union algorithm returns a hpx::future<ranges::set_union_result<Iter1, Iter2, Iter3>> if the execution policy is of type sequenced_task_policy or parallel_task_policy and returns ranges::set_union_result<Iter1, Iter2, Iter3> otherwise. where Iter1 is range_iterator_t<Rng1> and Iter2 is range_iterator_t<Rng2> The set_union algorithm returns the output iterator to the element in the destination range, one past the last element copied.