sparrow 1.0.0
Loading...
Searching...
No Matches
functor_index_iterator.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
19
20namespace sparrow
21{
22 template <class FUNCTOR>
24 functor_index_iterator<FUNCTOR>, // Derived
25 std::invoke_result_t<FUNCTOR, std::size_t>, // Element
26 std::random_access_iterator_tag,
27 std::invoke_result_t<FUNCTOR, std::size_t> // Reference
28 >
29 {
30 public:
31
32 using result_type = std::invoke_result_t<FUNCTOR, std::size_t>;
34 using difference_type = std::ptrdiff_t;
35 using size_type = std::size_t;
36 using functor_type = FUNCTOR;
37
38 constexpr functor_index_iterator() = default;
39
40 constexpr functor_index_iterator(FUNCTOR functor, size_type index)
41 : m_functor(std::move(functor))
42 , m_index(index)
43 {
44 }
45
46 private:
47
48 [[nodiscard]] result_type dereference() const
49 {
50 return m_functor(m_index);
51 }
52
53 constexpr void increment()
54 {
55 ++m_index;
56 }
57
58 constexpr void decrement()
59 {
60 --m_index;
61 }
62
63 constexpr void advance(difference_type n)
64 {
65 if (n >= 0)
66 {
67 m_index += static_cast<size_type>(n);
68 }
69 else
70 {
71 SPARROW_ASSERT_TRUE(std::abs(n) <= static_cast<difference_type>(m_index));
72 m_index -= static_cast<size_type>(-n);
73 }
74 }
75
76 [[nodiscard]] constexpr difference_type distance_to(const self_type& rhs) const
77 {
78 return static_cast<difference_type>(rhs.m_index) - static_cast<difference_type>(m_index);
79 }
80
81 [[nodiscard]] constexpr bool equal(const self_type& rhs) const
82 {
83 return m_index == rhs.m_index;
84 }
85
86 [[nodiscard]] constexpr bool less_than(const self_type& rhs) const
87 {
88 return m_index < rhs.m_index;
89 }
90
91 FUNCTOR m_functor = FUNCTOR{};
92 size_type m_index = 0;
93
94 friend class iterator_access;
95 };
96}
constexpr functor_index_iterator(FUNCTOR functor, size_type index)
constexpr functor_index_iterator()=default
functor_index_iterator< detail::layout_value_functor< array_type, inner_reference > > self_type
std::invoke_result_t< detail::layout_value_functor< array_type, inner_reference >, std::size_t > result_type
#define SPARROW_ASSERT_TRUE(expr__)