VCCC  2024.05
VisualCamp Common C++ library
counted.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 1/27/24.
3 //
4 
5 #ifndef VCCC_RANGES_VIEWS_COUNTED_HPP_
6 #define VCCC_RANGES_VIEWS_COUNTED_HPP_
7 
8 #include <cstddef>
9 #include <type_traits>
10 
20 #include "vccc/span.hpp"
26 
27 namespace vccc {
28 namespace ranges {
29 namespace views {
30 namespace detail {
31 
32 using vccc::detail::return_category;
33 using vccc::detail::tag_1;
34 using vccc::detail::tag_2;
35 using vccc::detail::tag_else;
36 
38  private:
40  struct check : convertible_to<DiffType, iter_difference_t<T>> {};
41  template<typename T, typename DiffType>
42  struct check<T, DiffType, false> : std::false_type {};
43 
44  template<typename T>
45  using tag = vccc::detail::conditional_tag<
48 
49  template<typename D, typename I, typename DiffType>
50  constexpr auto call(I&& it, DiffType&& count, tag_1 /* contigious_iterator */) const {
51  using Address = decltype(vccc::to_address(std::forward<I>(it)));
52  using T = std::remove_reference_t<iter_reference_t<Address>>;
53  return span<T>(
54  vccc::to_address(std::forward<I>(it)),
55  static_cast<std::size_t>(static_cast<D>(std::forward<DiffType>(count))) );
56  }
57 
58  template<typename D, typename I, typename DiffType>
59  constexpr auto call(I&& it, DiffType&& count, tag_2 /* random_access_iterator */) const {
60  return ranges::make_subrange(it, it + static_cast<D>(std::forward<DiffType>(count)));
61  }
62 
63  template<typename D, typename I, typename DiffType>
64  constexpr auto call(I&& it, DiffType&& count, tag_else) const {
65  return ranges::make_subrange(
67  std::forward<I>(it),
68  std::forward<DiffType>(count)),
70  );
71  }
72 
73  public:
74  template<typename I, typename DiffType, std::enable_if_t<
75  check<std::decay_t<I>, DiffType&&>::value, int> = 0>
76  constexpr auto operator()(I&& it, DiffType&& count) const {
77  using T = std::decay_t<I>;
78  using D = iter_difference_t<T>;
79  return call<D>(std::forward<I>(it), std::forward<DiffType>(count), tag<T>{});
80  }
81 
82 };
83 
84 } // namespace detail
85 
88 
102 
104 
105 } // namespace views
106 } // namespace ranges
107 } // namespace vccc
108 
109 #endif // VCCC_RANGES_VIEWS_COUNTED_HPP_
Definition: counted_iterator.hpp:71
a non-owning view over a contiguous sequence of objects
Definition: span.hpp:118
constexpr VCCC_INLINE_OR_STATIC detail::count_niebloid count
Definition: count.hpp:58
constexpr VCCC_INLINE_OR_STATIC default_sentinel_t default_sentinel
Definition: default_sentinel_t.hpp:25
constexpr T * to_address(T *p) noexcept
Definition: to_address.hpp:37
typename iter_difference< T >::type iter_difference_t
Computes the difference type of T
Definition: iter_difference_t.hpp:49
constexpr VCCC_INLINE_OR_STATIC detail::counted_niebloid counted
creates a subrange from an iterator and a count
Definition: counted.hpp:101
constexpr subrange< I, S > make_subrange(I i, S s)
Definition: subrange.hpp:275
typename remove_cvref< T >::type remove_cvref_t
Definition: remove_cvref.hpp:24
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35
specifies that a random_access_iterator is a contiguous iterator, referring to elements that are cont...
Definition: contiguous_iterator.hpp:88
Models std::convertible_to
Definition: convertible_to.hpp:38
specifies that a bidirectional_iterator is a random-access iterator, supporting advancement in consta...
Definition: random_access_iterator.hpp:120
constexpr auto operator()(I &&it, DiffType &&count) const
Definition: counted.hpp:76