1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___PSTL_BACKENDS_DEFAULT_H
#define _LIBCPP___PSTL_BACKENDS_DEFAULT_H
#include <__algorithm/copy_n.h>
#include <__algorithm/equal.h>
#include <__algorithm/fill_n.h>
#include <__algorithm/for_each_n.h>
#include <__config>
#include <__functional/identity.h>
#include <__functional/not_fn.h>
#include <__functional/operations.h>
#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
#include <__pstl/backend_fwd.h>
#include <__pstl/dispatch.h>
#include <__utility/empty.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <optional>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
namespace __pstl {
//
// This file provides an incomplete PSTL backend that implements all of the PSTL algorithms
// based on a smaller set of basis operations.
//
// It is intended as a building block for other PSTL backends that implement some operations more
// efficiently but may not want to define the full set of PSTL algorithms.
//
// This backend implements all the PSTL algorithms based on the following basis operations:
//
// find_if family
// --------------
// - find
// - find_if_not
// - any_of
// - all_of
// - none_of
// - is_partitioned
//
// for_each family
// ---------------
// - for_each_n
// - fill
// - fill_n
// - replace
// - replace_if
// - generate
// - generate_n
//
// merge family
// ------------
// No other algorithms based on merge
//
// stable_sort family
// ------------------
// - sort
//
// transform_reduce and transform_reduce_binary family
// ---------------------------------------------------
// - count_if
// - count
// - equal(3 legs)
// - equal
// - reduce
//
// transform and transform_binary family
// -------------------------------------
// - replace_copy_if
// - replace_copy
// - move
// - copy
// - copy_n
// - rotate_copy
//
//////////////////////////////////////////////////////////////
// find_if family
//////////////////////////////////////////////////////////////
template <class _ExecutionPolicy>
struct __find<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) const noexcept {
using _FindIf = __dispatch<__find_if, __current_configuration, _ExecutionPolicy>;
return _FindIf()(
__policy, std::move(__first), std::move(__last), [&](__iter_reference<_ForwardIterator> __element) {
return __element == __value;
});
}
};
template <class _ExecutionPolicy>
struct __find_if_not<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardIterator>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred) const noexcept {
using _FindIf = __dispatch<__find_if, __current_configuration, _ExecutionPolicy>;
return _FindIf()(__policy, __first, __last, std::not_fn(std::forward<_Pred>(__pred)));
}
};
template <class _ExecutionPolicy>
struct __any_of<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred) const noexcept {
using _FindIf = __dispatch<__find_if, __current_configuration, _ExecutionPolicy>;
auto __res = _FindIf()(__policy, __first, __last, std::forward<_Pred>(__pred));
if (!__res)
return nullopt;
return *__res != __last;
}
};
template <class _ExecutionPolicy>
struct __all_of<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred) const noexcept {
using _AnyOf = __dispatch<__any_of, __current_configuration, _ExecutionPolicy>;
auto __res = _AnyOf()(__policy, __first, __last, [&](__iter_reference<_ForwardIterator> __value) {
return !__pred(__value);
});
if (!__res)
return nullopt;
return !*__res;
}
};
template <class _ExecutionPolicy>
struct __none_of<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred) const noexcept {
using _AnyOf = __dispatch<__any_of, __current_configuration, _ExecutionPolicy>;
auto __res = _AnyOf()(__policy, __first, __last, std::forward<_Pred>(__pred));
if (!__res)
return nullopt;
return !*__res;
}
};
template <class _ExecutionPolicy>
struct __is_partitioned<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Pred>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred) const noexcept {
using _FindIfNot = __dispatch<__find_if_not, __current_configuration, _ExecutionPolicy>;
auto __maybe_first = _FindIfNot()(__policy, std::move(__first), std::move(__last), __pred);
if (__maybe_first == nullopt)
return nullopt;
__first = *__maybe_first;
if (__first == __last)
return true;
++__first;
using _NoneOf = __dispatch<__none_of, __current_configuration, _ExecutionPolicy>;
return _NoneOf()(__policy, std::move(__first), std::move(__last), __pred);
}
};
//////////////////////////////////////////////////////////////
// for_each family
//////////////////////////////////////////////////////////////
template <class _ExecutionPolicy>
struct __for_each_n<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Size, class _Function>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy, _ForwardIterator __first, _Size __size, _Function __func) const noexcept {
if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
using _ForEach = __dispatch<__for_each, __current_configuration, _ExecutionPolicy>;
_ForwardIterator __last = __first + __size;
return _ForEach()(__policy, std::move(__first), std::move(__last), std::move(__func));
} else {
// Otherwise, use the serial algorithm to avoid doing two passes over the input
std::for_each_n(std::move(__first), __size, std::move(__func));
return __empty{};
}
}
};
template <class _ExecutionPolicy>
struct __fill<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp const& __value) const noexcept {
using _ForEach = __dispatch<__for_each, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _ForEach()(__policy, std::move(__first), std::move(__last), [&](_Ref __element) { __element = __value; });
}
};
template <class _ExecutionPolicy>
struct __fill_n<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Size, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy, _ForwardIterator __first, _Size __n, _Tp const& __value) const noexcept {
if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
using _Fill = __dispatch<__fill, __current_configuration, _ExecutionPolicy>;
_ForwardIterator __last = __first + __n;
return _Fill()(__policy, std::move(__first), std::move(__last), __value);
} else {
// Otherwise, use the serial algorithm to avoid doing two passes over the input
std::fill_n(std::move(__first), __n, __value);
return optional<__empty>{__empty{}};
}
}
};
template <class _ExecutionPolicy>
struct __replace<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp const& __old, _Tp const& __new)
const noexcept {
using _ReplaceIf = __dispatch<__replace_if, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _ReplaceIf()(
__policy, std::move(__first), std::move(__last), [&](_Ref __element) { return __element == __old; }, __new);
}
};
template <class _ExecutionPolicy>
struct __replace_if<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Pred, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty> operator()(
_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Pred&& __pred, _Tp const& __new_value)
const noexcept {
using _ForEach = __dispatch<__for_each, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _ForEach()(__policy, std::move(__first), std::move(__last), [&](_Ref __element) {
if (__pred(__element))
__element = __new_value;
});
}
};
template <class _ExecutionPolicy>
struct __generate<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Generator>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Generator&& __gen) const noexcept {
using _ForEach = __dispatch<__for_each, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _ForEach()(__policy, std::move(__first), std::move(__last), [&](_Ref __element) { __element = __gen(); });
}
};
template <class _ExecutionPolicy>
struct __generate_n<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Size, class _Generator>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy, _ForwardIterator __first, _Size __n, _Generator&& __gen) const noexcept {
using _ForEachN = __dispatch<__for_each_n, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _ForEachN()(__policy, std::move(__first), __n, [&](_Ref __element) { __element = __gen(); });
}
};
//////////////////////////////////////////////////////////////
// stable_sort family
//////////////////////////////////////////////////////////////
template <class _ExecutionPolicy>
struct __sort<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _RandomAccessIterator, class _Comp>
_LIBCPP_HIDE_FROM_ABI optional<__empty> operator()(
_Policy&& __policy, _RandomAccessIterator __first, _RandomAccessIterator __last, _Comp&& __comp) const noexcept {
using _StableSort = __dispatch<__stable_sort, __current_configuration, _ExecutionPolicy>;
return _StableSort()(__policy, std::move(__first), std::move(__last), std::forward<_Comp>(__comp));
}
};
//////////////////////////////////////////////////////////////
// transform_reduce family
//////////////////////////////////////////////////////////////
template <class _ExecutionPolicy>
struct __count_if<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Predicate>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>> operator()(
_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Predicate&& __pred) const noexcept {
using _TransformReduce = __dispatch<__transform_reduce, __current_configuration, _ExecutionPolicy>;
using _DiffT = __iter_diff_t<_ForwardIterator>;
using _Ref = __iter_reference<_ForwardIterator>;
return _TransformReduce()(
__policy, std::move(__first), std::move(__last), _DiffT{}, std::plus{}, [&](_Ref __element) -> _DiffT {
return __pred(__element) ? _DiffT(1) : _DiffT(0);
});
}
};
template <class _ExecutionPolicy>
struct __count<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__iter_diff_t<_ForwardIterator>>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp const& __value) const noexcept {
using _CountIf = __dispatch<__count_if, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _CountIf()(__policy, std::move(__first), std::move(__last), [&](_Ref __element) -> bool {
return __element == __value;
});
}
};
template <class _ExecutionPolicy>
struct __equal_3leg<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
operator()(_Policy&& __policy,
_ForwardIterator1 __first1,
_ForwardIterator1 __last1,
_ForwardIterator2 __first2,
_Predicate&& __pred) const noexcept {
using _TransformReduce = __dispatch<__transform_reduce_binary, __current_configuration, _ExecutionPolicy>;
return _TransformReduce()(
__policy,
std::move(__first1),
std::move(__last1),
std::move(__first2),
true,
std::logical_and{},
std::forward<_Predicate>(__pred));
}
};
template <class _ExecutionPolicy>
struct __equal<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator1, class _ForwardIterator2, class _Predicate>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<bool>
operator()(_Policy&& __policy,
_ForwardIterator1 __first1,
_ForwardIterator1 __last1,
_ForwardIterator2 __first2,
_ForwardIterator2 __last2,
_Predicate&& __pred) const noexcept {
if constexpr (__has_random_access_iterator_category<_ForwardIterator1>::value &&
__has_random_access_iterator_category<_ForwardIterator2>::value) {
if (__last1 - __first1 != __last2 - __first2)
return false;
// Fall back to the 3 legged algorithm
using _Equal3Leg = __dispatch<__equal_3leg, __current_configuration, _ExecutionPolicy>;
return _Equal3Leg()(
__policy, std::move(__first1), std::move(__last1), std::move(__first2), std::forward<_Predicate>(__pred));
} else {
// If we don't have random access, fall back to the serial algorithm cause we can't do much
return std::equal(
std::move(__first1),
std::move(__last1),
std::move(__first2),
std::move(__last2),
std::forward<_Predicate>(__pred));
}
}
};
template <class _ExecutionPolicy>
struct __reduce<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Tp, class _BinaryOperation>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_Tp>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _Tp __init, _BinaryOperation&& __op)
const noexcept {
using _TransformReduce = __dispatch<__transform_reduce, __current_configuration, _ExecutionPolicy>;
return _TransformReduce()(
__policy,
std::move(__first),
std::move(__last),
std::move(__init),
std::forward<_BinaryOperation>(__op),
__identity{});
}
};
//////////////////////////////////////////////////////////////
// transform family
//////////////////////////////////////////////////////////////
template <class _ExecutionPolicy>
struct __replace_copy_if<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _Pred, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy,
_ForwardIterator __first,
_ForwardIterator __last,
_ForwardOutIterator __out_it,
_Pred&& __pred,
_Tp const& __new_value) const noexcept {
using _Transform = __dispatch<__transform, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
auto __res =
_Transform()(__policy, std::move(__first), std::move(__last), std::move(__out_it), [&](_Ref __element) {
return __pred(__element) ? __new_value : __element;
});
if (__res == nullopt)
return nullopt;
return __empty{};
}
};
template <class _ExecutionPolicy>
struct __replace_copy<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _ForwardOutIterator, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<__empty>
operator()(_Policy&& __policy,
_ForwardIterator __first,
_ForwardIterator __last,
_ForwardOutIterator __out_it,
_Tp const& __old_value,
_Tp const& __new_value) const noexcept {
using _ReplaceCopyIf = __dispatch<__replace_copy_if, __current_configuration, _ExecutionPolicy>;
using _Ref = __iter_reference<_ForwardIterator>;
return _ReplaceCopyIf()(
__policy,
std::move(__first),
std::move(__last),
std::move(__out_it),
[&](_Ref __element) { return __element == __old_value; },
__new_value);
}
};
// TODO: Use the std::copy/move shenanigans to forward to std::memmove
// Investigate whether we want to still forward to std::transform(policy)
// in that case for the execution::par part, or whether we actually want
// to run everything serially in that case.
template <class _ExecutionPolicy>
struct __move<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _ForwardOutIterator>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _ForwardOutIterator __out_it)
const noexcept {
using _Transform = __dispatch<__transform, __current_configuration, _ExecutionPolicy>;
return _Transform()(__policy, std::move(__first), std::move(__last), std::move(__out_it), [&](auto&& __element) {
return std::move(__element);
});
}
};
// TODO: Use the std::copy/move shenanigans to forward to std::memmove
template <class _ExecutionPolicy>
struct __copy<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _ForwardOutIterator>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
operator()(_Policy&& __policy, _ForwardIterator __first, _ForwardIterator __last, _ForwardOutIterator __out_it)
const noexcept {
using _Transform = __dispatch<__transform, __current_configuration, _ExecutionPolicy>;
return _Transform()(__policy, std::move(__first), std::move(__last), std::move(__out_it), __identity());
}
};
template <class _ExecutionPolicy>
struct __copy_n<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _Size, class _ForwardOutIterator>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
operator()(_Policy&& __policy, _ForwardIterator __first, _Size __n, _ForwardOutIterator __out_it) const noexcept {
if constexpr (__has_random_access_iterator_category_or_concept<_ForwardIterator>::value) {
using _Copy = __dispatch<__copy, __current_configuration, _ExecutionPolicy>;
_ForwardIterator __last = __first + __n;
return _Copy()(__policy, std::move(__first), std::move(__last), std::move(__out_it));
} else {
// Otherwise, use the serial algorithm to avoid doing two passes over the input
return std::copy_n(std::move(__first), __n, std::move(__out_it));
}
}
};
template <class _ExecutionPolicy>
struct __rotate_copy<__default_backend_tag, _ExecutionPolicy> {
template <class _Policy, class _ForwardIterator, class _ForwardOutIterator>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI optional<_ForwardOutIterator>
operator()(_Policy&& __policy,
_ForwardIterator __first,
_ForwardIterator __middle,
_ForwardIterator __last,
_ForwardOutIterator __out_it) const noexcept {
using _Copy = __dispatch<__copy, __current_configuration, _ExecutionPolicy>;
auto __result_mid = _Copy()(__policy, __middle, std::move(__last), std::move(__out_it));
if (__result_mid == nullopt)
return nullopt;
return _Copy()(__policy, std::move(__first), std::move(__middle), *std::move(__result_mid));
}
};
} // namespace __pstl
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP___PSTL_BACKENDS_DEFAULT_H
|