Path: blob/main/contrib/llvm-project/libcxx/include/__pstl/cpu_algos/merge.h
35271 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___PSTL_CPU_ALGOS_MERGE_H9#define _LIBCPP___PSTL_CPU_ALGOS_MERGE_H1011#include <__algorithm/merge.h>12#include <__assert>13#include <__config>14#include <__iterator/concepts.h>15#include <__pstl/backend_fwd.h>16#include <__pstl/cpu_algos/cpu_traits.h>17#include <__type_traits/is_execution_policy.h>18#include <__utility/move.h>19#include <optional>2021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif2425_LIBCPP_PUSH_MACROS26#include <__undef_macros>2728_LIBCPP_BEGIN_NAMESPACE_STD29namespace __pstl {3031template <class _Backend, class _RawExecutionPolicy>32struct __cpu_parallel_merge {33template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _ForwardOutIterator, class _Comp>34_LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator> operator()(35_Policy&& __policy,36_ForwardIterator1 __first1,37_ForwardIterator1 __last1,38_ForwardIterator2 __first2,39_ForwardIterator2 __last2,40_ForwardOutIterator __result,41_Comp __comp) const noexcept {42if constexpr (__is_parallel_execution_policy_v<_RawExecutionPolicy> &&43__has_random_access_iterator_category_or_concept<_ForwardIterator1>::value &&44__has_random_access_iterator_category_or_concept<_ForwardIterator2>::value &&45__has_random_access_iterator_category_or_concept<_ForwardOutIterator>::value) {46auto __res = __cpu_traits<_Backend>::__merge(47__first1,48__last1,49__first2,50__last2,51__result,52__comp,53[&__policy](_ForwardIterator1 __g_first1,54_ForwardIterator1 __g_last1,55_ForwardIterator2 __g_first2,56_ForwardIterator2 __g_last2,57_ForwardOutIterator __g_result,58_Comp __g_comp) {59using _MergeUnseq = __pstl::__merge<_Backend, __remove_parallel_policy_t<_RawExecutionPolicy>>;60[[maybe_unused]] auto __g_res = _MergeUnseq()(61std::__remove_parallel_policy(__policy),62std::move(__g_first1),63std::move(__g_last1),64std::move(__g_first2),65std::move(__g_last2),66std::move(__g_result),67std::move(__g_comp));68_LIBCPP_ASSERT_INTERNAL(__g_res, "unsed/sed should never try to allocate!");69});70if (!__res)71return nullopt;72return __result + (__last1 - __first1) + (__last2 - __first2);73} else {74return std::merge(__first1, __last1, __first2, __last2, __result, __comp);75}76}77};7879} // namespace __pstl80_LIBCPP_END_NAMESPACE_STD8182_LIBCPP_POP_MACROS8384#endif // _LIBCPP___PSTL_CPU_ALGOS_MERGE_H858687