/// \file
// Range v3 library
//
// Copyright Eric Niebler 2014-present
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//
//===-------------------------- algorithm ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef RANGES_V3_ALGORITHM_PARTITION_HPP
#define RANGES_V3_ALGORITHM_PARTITION_HPP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace ranges
{
/// \addtogroup group-algorithms
/// @{
/// \cond
namespace detail
{
template
I partition_impl(I first, S last, C pred, P proj, detail::forward_iterator_tag_)
{
while(true)
{
if(first == last)
return first;
if(!invoke(pred, invoke(proj, *first)))
break;
++first;
}
for(I p = first; ++p != last;)
{
if(invoke(pred, invoke(proj, *p)))
{
ranges::iter_swap(first, p);
++first;
}
}
return first;
}
template
I partition_impl(I first, S end_, C pred, P proj,
detail::bidirectional_iterator_tag_)
{
I last = ranges::next(first, end_);
while(true)
{
while(true)
{
if(first == last)
return first;
if(!invoke(pred, invoke(proj, *first)))
break;
++first;
}
do
{
if(first == --last)
return first;
} while(!invoke(pred, invoke(proj, *last)));
ranges::iter_swap(first, last);
++first;
}
}
} // namespace detail
/// \endcond
RANGES_BEGIN_NIEBLOID(partition)
/// \brief function template \c partition
template
auto RANGES_FUN_NIEBLOID(partition)(I first, S last, C pred, P proj = P{}) //
->CPP_ret(I)( //
requires permutable && sentinel_for &&
indirect_unary_predicate>)
{
return detail::partition_impl(std::move(first),
std::move(last),
std::move(pred),
std::move(proj),
iterator_tag_of());
}
/// \overload
template
auto RANGES_FUN_NIEBLOID(partition)(Rng && rng, C pred, P proj = P{}) //
->CPP_ret(safe_iterator_t)( //
requires forward_range && permutable> &&
indirect_unary_predicate, P>>)
{
return detail::partition_impl(begin(rng),
end(rng),
std::move(pred),
std::move(proj),
iterator_tag_of>());
}
RANGES_END_NIEBLOID(partition)
namespace cpp20
{
using ranges::partition;
}
/// @}
} // namespace ranges
#endif // include guard