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_HPP
0015 #define RANGES_V3_VIEW_REPEAT_HPP
0016 
0017 #include <utility>
0018 
0019 #include <range/v3/range_fwd.hpp>
0020 
0021 #include <range/v3/iterator/unreachable_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_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_view : view_facade<repeat_view<Val>, infinite>
0043     {
0044     private:
0045         semiregular_box_t<Val> value_;
0046         friend range_access;
0047 
0048         struct cursor
0049         {
0050         private:
0051             Val const * value_;
0052             std::ptrdiff_t n_ = 0;
0053 
0054         public:
0055             cursor() = default;
0056             explicit cursor(Val const & value)
0057               : value_(std::addressof(value))
0058             {}
0059             Val const & read() const noexcept
0060             {
0061                 return *value_;
0062             }
0063             bool equal(cursor const & that) const
0064             {
0065                 return n_ == that.n_;
0066             }
0067             void next()
0068             {
0069                 ++n_;
0070             }
0071             void prev()
0072             {
0073                 --n_;
0074             }
0075             void advance(std::ptrdiff_t d)
0076             {
0077                 n_ += d;
0078             }
0079             std::ptrdiff_t distance_to(cursor const & that) const
0080             {
0081                 return that.n_ - n_;
0082             }
0083         };
0084         cursor begin_cursor() const
0085         {
0086             return cursor{value_};
0087         }
0088         unreachable_sentinel_t end_cursor() const
0089         {
0090             return unreachable;
0091         }
0092 
0093     public:
0094         repeat_view() = default;
0095         constexpr explicit repeat_view(Val value)
0096           : value_(detail::move(value))
0097         {}
0098     };
0099 
0100     namespace views
0101     {
0102         struct repeat_fn
0103         {
0104             template(typename Val)(
0105                 requires copy_constructible<Val>)
0106             repeat_view<Val> operator()(Val value) const
0107             {
0108                 return repeat_view<Val>{std::move(value)};
0109             }
0110         };
0111 
0112         /// \relates repeat_fn
0113         /// \ingroup group-views
0114         RANGES_INLINE_VARIABLE(repeat_fn, repeat)
0115     } // namespace views
0116     /// @}
0117 } // namespace ranges
0118 
0119 #include <range/v3/detail/epilogue.hpp>
0120 #include <range/v3/detail/satisfy_boost_range.hpp>
0121 RANGES_SATISFY_BOOST_RANGE(::ranges::repeat_view)
0122 
0123 #endif