You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

take_last.hpp 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Barry Revzin 2019-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_VIEW_TAKE_LAST_HPP
  14. #define RANGES_V3_VIEW_TAKE_LAST_HPP
  15. #include <concepts/concepts.hpp>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/functional/bind_back.hpp>
  18. #include <range/v3/functional/pipeable.hpp>
  19. #include <range/v3/range/concepts.hpp>
  20. #include <range/v3/range/operations.hpp>
  21. #include <range/v3/view/drop_exactly.hpp>
  22. namespace ranges
  23. {
  24. namespace views
  25. {
  26. struct take_last_fn
  27. {
  28. private:
  29. friend view_access;
  30. template<typename Int>
  31. static constexpr auto CPP_fun(bind)(take_last_fn take_last, Int n)( //
  32. requires integral<Int>)
  33. {
  34. return make_pipeable(bind_back(take_last, n));
  35. }
  36. public:
  37. template<typename Rng>
  38. auto CPP_fun(operator())(Rng && rng, range_difference_t<Rng> n)(
  39. const requires viewable_range<Rng> && sized_range<Rng>)
  40. {
  41. auto sz = ranges::distance(rng);
  42. return drop_exactly(static_cast<Rng &&>(rng), sz > n ? sz - n : 0);
  43. }
  44. };
  45. /// \relates take_last_fn
  46. /// \ingroup group-views
  47. RANGES_INLINE_VARIABLE(view<take_last_fn>, take_last)
  48. } // namespace views
  49. /// @}
  50. } // namespace ranges
  51. #endif