/// \file // Range v3 library // // Copyright Eric Niebler 2013-2014 // // 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_REVERSE_HPP #define RANGES_V3_ALGORITHM_REVERSE_HPP #include #include #include #include #include #include #include #include #include namespace ranges { inline namespace v3 { /// \addtogroup group-algorithms /// @{ struct reverse_fn { private: template static void impl(I begin, I end, concepts::BidirectionalIterator*) { while(begin != end) { if(begin == --end) break; ranges::iter_swap(begin, end); ++begin; } } template static void impl(I begin, I end, concepts::RandomAccessIterator*) { if(begin != end) for(; begin < --end; ++begin) ranges::iter_swap(begin, end); } public: template() && Sentinel() && Permutable())> I operator()(I begin, S end_) const { I end = ranges::next(begin, end_); reverse_fn::impl(begin, end, iterator_concept{}); return end; } template, CONCEPT_REQUIRES_(BidirectionalRange() && Permutable())> safe_iterator_t operator()(Rng &&rng) const { return (*this)(begin(rng), end(rng)); } }; /// \sa `reverse_fn` /// \ingroup group-algorithms RANGES_INLINE_VARIABLE(with_braced_init_args, reverse) /// @} } // namespace v3 } // namespace ranges #endif // include guard