/// \file
// Range v3 library
//
// Copyright Eric Niebler 2013-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
//
#ifndef RANGES_V3_ALGORITHM_UNIQUE_COPY_HPP
#define RANGES_V3_ALGORITHM_UNIQUE_COPY_HPP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace ranges
{
/// \addtogroup group-algorithms
/// @{
template
using unique_copy_result = detail::in_out_result;
/// \cond
namespace detail
{
template
unique_copy_result unique_copy_impl(I first, S last, O out, C pred, P proj,
detail::input_iterator_tag_,
std::false_type)
{
if(first != last)
{
// Must save a copy into a local because we will need this value
// even after we advance the input iterator.
iter_value_t value =
*first; // This is guaranteed by indirectly_copyable
*out = value;
++out;
while(++first != last)
{
auto && x = *first;
if(!invoke(pred, invoke(proj, value), invoke(proj, x)))
{
value = (decltype(x) &&)x;
*out = value;
++out;
}
}
}
return {first, out};
}
template
unique_copy_result unique_copy_impl(I first, S last, O out, C pred, P proj,
detail::forward_iterator_tag_,
std::false_type)
{
if(first != last)
{
I tmp = first;
*out = *tmp;
++out;
while(++first != last)
{
auto && x = *first;
if(!invoke(pred, invoke(proj, *tmp), invoke(proj, x)))
{
*out = (decltype(x) &&)x;
++out;
tmp = first;
}
}
}
return {first, out};
}
template
unique_copy_result unique_copy_impl(I first, S last, O out, C pred, P proj,
detail::input_iterator_tag_,
std::true_type)
{
if(first != last)
{
*out = *first;
while(++first != last)
{
auto && x = *first;
if(!invoke(pred, invoke(proj, *out), invoke(proj, x)))
*++out = (decltype(x) &&)x;
}
++out;
}
return {first, out};
}
} // namespace detail
/// \endcond
RANGES_BEGIN_NIEBLOID(unique_copy)
/// \brief template function unique_copy
///
/// range-based version of the `unique_copy` std algorithm
///
/// \pre `Rng` is a model of the `input_range` concept
/// \pre `O` is a model of the `weakly_incrementable` concept
/// \pre `C` is a model of the `relation` concept
template
auto RANGES_FUN_NIEBLOID(unique_copy)(
I first, S last, O out, C pred = C{}, P proj = P{}) //
->CPP_ret(unique_copy_result)( //
requires input_iterator && sentinel_for &&
indirect_relation> && weakly_incrementable &&
indirectly_copyable &&
(forward_iterator || forward_iterator ||
indirectly_copyable_storable))
{
return detail::unique_copy_impl(std::move(first),
std::move(last),
std::move(out),
std::move(pred),
std::move(proj),
iterator_tag_of(),
meta::bool_>{});
}
/// \overload
template
auto RANGES_FUN_NIEBLOID(unique_copy)(
Rng && rng, O out, C pred = C{}, P proj = P{}) //
->CPP_ret(unique_copy_result, O>)( //
requires input_range &&
indirect_relation, P>> &&
weakly_incrementable && indirectly_copyable, O> &&
(forward_iterator> || forward_iterator ||
indirectly_copyable_storable, O>))
{
return detail::unique_copy_impl(begin(rng),
end(rng),
std::move(out),
std::move(pred),
std::move(proj),
iterator_tag_of>(),
meta::bool_>{});
}
RANGES_END_NIEBLOID(unique_copy)
namespace cpp20
{
using ranges::unique_copy;
using ranges::unique_copy_result;
} // namespace cpp20
/// @}
} // namespace ranges
#endif // include guard