brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 47fe62f Raw
91 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___ALGORITHM_PARTITION_H10#define _LIBCPP___CXX03___ALGORITHM_PARTITION_H11 12#include <__cxx03/__algorithm/iterator_operations.h>13#include <__cxx03/__config>14#include <__cxx03/__iterator/iterator_traits.h>15#include <__cxx03/__utility/move.h>16#include <__cxx03/__utility/pair.h>17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19#  pragma GCC system_header20#endif21 22_LIBCPP_PUSH_MACROS23#include <__cxx03/__undef_macros>24 25_LIBCPP_BEGIN_NAMESPACE_STD26 27template <class _Predicate, class _AlgPolicy, class _ForwardIterator, class _Sentinel>28_LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>29__partition_impl(_ForwardIterator __first, _Sentinel __last, _Predicate __pred, forward_iterator_tag) {30  while (true) {31    if (__first == __last)32      return std::make_pair(std::move(__first), std::move(__first));33    if (!__pred(*__first))34      break;35    ++__first;36  }37 38  _ForwardIterator __p = __first;39  while (++__p != __last) {40    if (__pred(*__p)) {41      _IterOps<_AlgPolicy>::iter_swap(__first, __p);42      ++__first;43    }44  }45  return std::make_pair(std::move(__first), std::move(__p));46}47 48template <class _Predicate, class _AlgPolicy, class _BidirectionalIterator, class _Sentinel>49_LIBCPP_HIDE_FROM_ABI pair<_BidirectionalIterator, _BidirectionalIterator>50__partition_impl(_BidirectionalIterator __first, _Sentinel __sentinel, _Predicate __pred, bidirectional_iterator_tag) {51  _BidirectionalIterator __original_last = _IterOps<_AlgPolicy>::next(__first, __sentinel);52  _BidirectionalIterator __last          = __original_last;53 54  while (true) {55    while (true) {56      if (__first == __last)57        return std::make_pair(std::move(__first), std::move(__original_last));58      if (!__pred(*__first))59        break;60      ++__first;61    }62    do {63      if (__first == --__last)64        return std::make_pair(std::move(__first), std::move(__original_last));65    } while (!__pred(*__last));66    _IterOps<_AlgPolicy>::iter_swap(__first, __last);67    ++__first;68  }69}70 71template <class _AlgPolicy, class _ForwardIterator, class _Sentinel, class _Predicate, class _IterCategory>72inline _LIBCPP_HIDE_FROM_ABI pair<_ForwardIterator, _ForwardIterator>73__partition(_ForwardIterator __first, _Sentinel __last, _Predicate&& __pred, _IterCategory __iter_category) {74  return std::__partition_impl<__remove_cvref_t<_Predicate>&, _AlgPolicy>(75      std::move(__first), std::move(__last), __pred, __iter_category);76}77 78template <class _ForwardIterator, class _Predicate>79inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator80partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate __pred) {81  using _IterCategory = typename iterator_traits<_ForwardIterator>::iterator_category;82  auto __result = std::__partition<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __pred, _IterCategory());83  return __result.first;84}85 86_LIBCPP_END_NAMESPACE_STD87 88_LIBCPP_POP_MACROS89 90#endif // _LIBCPP___CXX03___ALGORITHM_PARTITION_H91