Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:27:56

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2013-present
0005 //  Copyright Gonzalo Brito Gadeschi 2014
0006 //
0007 //  Use, modification and distribution is subject to the
0008 //  Boost Software License, Version 1.0. (See accompanying
0009 //  file LICENSE_1_0.txt or copy at
0010 //  http://www.boost.org/LICENSE_1_0.txt)
0011 //
0012 // Project home: https://github.com/ericniebler/range-v3
0013 //
0014 // Implementation based on the code in libc++
0015 //   http://http://libcxx.llvm.org/
0016 
0017 #ifndef RANGES_V3_ALGORITHM_UNIQUE_HPP
0018 #define RANGES_V3_ALGORITHM_UNIQUE_HPP
0019 
0020 #include <range/v3/range_fwd.hpp>
0021 
0022 #include <range/v3/algorithm/adjacent_find.hpp>
0023 #include <range/v3/functional/comparisons.hpp>
0024 #include <range/v3/functional/identity.hpp>
0025 #include <range/v3/functional/invoke.hpp>
0026 #include <range/v3/iterator/operations.hpp>
0027 #include <range/v3/range/access.hpp>
0028 #include <range/v3/range/concepts.hpp>
0029 #include <range/v3/range/dangling.hpp>
0030 #include <range/v3/range/traits.hpp>
0031 #include <range/v3/utility/static_const.hpp>
0032 
0033 #include <range/v3/detail/prologue.hpp>
0034 
0035 namespace ranges
0036 {
0037     /// \addtogroup group-algorithms
0038     /// @{
0039     RANGES_FUNC_BEGIN(unique)
0040 
0041         /// \brief template function \c unique
0042         ///
0043         /// range-based version of the \c unique std algorithm
0044         ///
0045         /// \pre `Rng` is a model of the `forward_range` concept
0046         /// \pre `I` is a model of the `forward_iterator` concept
0047         /// \pre `S` is a model of the `sentinel_for` concept
0048         /// \pre `C` is a model of the `relation` concept
0049         ///
0050         template(typename I, typename S, typename C = equal_to, typename P = identity)(
0051             requires sortable<I, C, P> AND sentinel_for<S, I>)
0052         constexpr I RANGES_FUNC(unique)(I first, S last, C pred = C{}, P proj = P{})
0053         {
0054             first = adjacent_find(std::move(first), last, ranges::ref(pred), ranges::ref(proj));
0055 
0056             if(first != last)
0057             {
0058                 for(I i = next(first); ++i != last;)
0059                     if(!invoke(pred, invoke(proj, *first), invoke(proj, *i)))
0060                         *++first = iter_move(i);
0061                 ++first;
0062             }
0063             return first;
0064         }
0065 
0066         /// \overload
0067         template(typename Rng, typename C = equal_to, typename P = identity)(
0068             requires sortable<iterator_t<Rng>, C, P> AND range<Rng>)
0069         constexpr borrowed_iterator_t<Rng> //
0070         RANGES_FUNC(unique)(Rng && rng, C pred = C{}, P proj = P{}) //
0071         {
0072             return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
0073         }
0074 
0075     RANGES_FUNC_END(unique)
0076 
0077     namespace cpp20
0078     {
0079         using ranges::unique;
0080     }
0081     /// @}
0082 } // namespace ranges
0083 
0084 #include <range/v3/detail/epilogue.hpp>
0085 
0086 #endif