Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:09:57

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2013-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 
0014 #ifndef RANGES_V3_VIEW_REPEAT_N_HPP
0015 #define RANGES_V3_VIEW_REPEAT_N_HPP
0016 
0017 #include <utility>
0018 
0019 #include <range/v3/range_fwd.hpp>
0020 
0021 #include <range/v3/iterator/default_sentinel.hpp>
0022 #include <range/v3/range/concepts.hpp>
0023 #include <range/v3/utility/semiregular_box.hpp>
0024 #include <range/v3/utility/static_const.hpp>
0025 #include <range/v3/view/facade.hpp>
0026 
0027 #include <range/v3/detail/prologue.hpp>
0028 
0029 namespace ranges
0030 {
0031     /// \addtogroup group-views
0032     /// @{
0033 
0034     // Ordinarily, a view shouldn't contain its elements. This is so that copying
0035     // and assigning ranges is O(1), and also so that in the event of element
0036     // mutation, all the copies of the range see the mutation the same way. The
0037     // repeat_n_view *does* own its lone element, though. This is OK because:
0038     //  - O(N) copying is fine when N==1 as it is in this case, and
0039     //  - The element is immutable, so there is no potential for incorrect
0040     //    semantics.
0041     template<typename Val>
0042     struct repeat_n_view : view_facade<repeat_n_view<Val>, finite>
0043     {
0044     private:
0045         friend range_access;
0046         semiregular_box_t<Val> value_;
0047         std::ptrdiff_t n_;
0048 
0049         struct cursor
0050         {
0051         private:
0052             Val const * value_;
0053             std::ptrdiff_t n_;
0054 
0055         public:
0056             cursor() = default;
0057             cursor(Val const & value, std::ptrdiff_t n)
0058               : value_(std::addressof(value))
0059               , n_(n)
0060             {}
0061             Val const & read() const
0062             {
0063                 return *value_;
0064             }
0065             constexpr bool equal(default_sentinel_t) const
0066             {
0067                 return 0 == n_;
0068             }
0069             bool equal(cursor const & that) const
0070             {
0071                 return n_ == that.n_;
0072             }
0073             void next()
0074             {
0075                 RANGES_EXPECT(0 != n_);
0076                 --n_;
0077             }
0078             void prev()
0079             {
0080                 ++n_;
0081             }
0082             void advance(std::ptrdiff_t n)
0083             {
0084                 n_ -= n;
0085             }
0086             std::ptrdiff_t distance_to(cursor const & that) const
0087             {
0088                 return n_ - that.n_;
0089             }
0090         };
0091         cursor begin_cursor() const
0092         {
0093             return {value_, n_};
0094         }
0095 
0096     public:
0097         repeat_n_view() = default;
0098         constexpr repeat_n_view(Val value, std::ptrdiff_t n)
0099           : value_(detail::move(value))
0100           , n_((RANGES_EXPECT(0 <= n), n))
0101         {}
0102         constexpr std::size_t size() const
0103         {
0104             return static_cast<std::size_t>(n_);
0105         }
0106     };
0107 
0108     namespace views
0109     {
0110         struct repeat_n_fn
0111         {
0112             template(typename Val)(
0113                 requires copy_constructible<Val>)
0114             repeat_n_view<Val> operator()(Val value, std::ptrdiff_t n) const
0115             {
0116                 return repeat_n_view<Val>{std::move(value), n};
0117             }
0118         };
0119 
0120         /// \relates repeat_n_fn
0121         /// \ingroup group-views
0122         RANGES_INLINE_VARIABLE(repeat_n_fn, repeat_n)
0123     } // namespace views
0124     /// @}
0125 } // namespace ranges
0126 
0127 #include <range/v3/detail/epilogue.hpp>
0128 #include <range/v3/detail/satisfy_boost_range.hpp>
0129 RANGES_SATISFY_BOOST_RANGE(::ranges::repeat_n_view)
0130 
0131 #endif