VCCC  2024.05
VisualCamp Common C++ library
distance.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 2024/01/03.
3 //
4 
5 #ifndef VCCC_RANGES_DISTANCE_HPP
6 #define VCCC_RANGES_DISTANCE_HPP
7 
8 #include <type_traits>
9 
14 #include "vccc/__ranges/begin.hpp"
15 #include "vccc/__ranges/end.hpp"
16 #include "vccc/__ranges/range.hpp"
18 #include "vccc/__ranges/size.hpp"
24 
25 namespace vccc {
26 namespace ranges {
27 namespace detail {
28 
29 using vccc::detail::return_category;
30 
31 struct distance_niebloid {
32  template<typename I, typename S, std::enable_if_t<conjunction<
33  sentinel_for<S, I>,
34  negation< sized_sentinel_for<S, I> >
35  >::value, int> = 0>
36  constexpr iter_difference_t<I> operator()(I first, S last) const {
37  using namespace vccc::rel_ops;
38  iter_difference_t<I> result = 0;
39  while (first != last) {
40  ++first;
41  ++result;
42  }
43  return result;
44  }
45 
46  template<typename I, typename S, std::enable_if_t<sized_sentinel_for<S, std::decay_t<I>>::value, int> = 0>
47  constexpr iter_difference_t<std::decay_t<I>> operator()(I&& first, S last) const {
48  return last - static_cast<const std::decay_t<I>&>(first);
49  }
50 
51  template<typename R, std::enable_if_t<ranges::sized_range<remove_cvref_t<R>>::value, int> = 0>
52  constexpr ranges::range_difference_t<R> operator()(R&& r) const {
53  return static_cast<ranges::range_difference_t<R>>(ranges::size(r));
54  }
55 
56  template<typename R, std::enable_if_t<conjunction<
57  ranges::range<remove_cvref_t<R>>,
58  negation< ranges::sized_range<remove_cvref_t<R>> >
59  >::value, int> = 0>
60  constexpr ranges::range_difference_t<R> operator()(R&& r) const {
61  return (*this)(ranges::begin(r), ranges::end(r));
62  }
63 };
64 
65 } // namespace detail
66 
67 namespace niebloid {
68 
71 
72 VCCC_INLINE_OR_STATIC constexpr detail::distance_niebloid distance{};
73 
75 
76 } // namespace niebloid
77 using namespace niebloid;
78 
79 } // namespace ranges
80 } // namespace vccc
81 
82 #endif // VCCC_RANGES_DISTANCE_HPP
constexpr VCCC_INLINE_OR_STATIC detail::begin_niebloid begin
returns an iterator to the beginning of a range
Definition: begin.hpp:116
constexpr VCCC_INLINE_OR_STATIC detail::distance_niebloid distance
Definition: distance.hpp:72
constexpr VCCC_INLINE_OR_STATIC detail::size_niebloid size
returns the size of a container or array
Definition: size.hpp:145
constexpr VCCC_INLINE_OR_STATIC detail::end_niebloid end
returns a sentinel indicating the end of a range
Definition: end.hpp:120
#define VCCC_INLINE_OR_STATIC
Definition: inline_or_static.hpp:9
Definition: cxx20_rel_ops.hpp:17
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35