sparrow 0.3.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
37 constexpr functor_index_iterator() = default;
38
39 constexpr functor_index_iterator(FUNCTOR functor, size_type index)
40 : m_functor(std::move(functor))
41 , m_index(index)
42 {
43 }
44
45 private:
46
47 [[nodiscard]] result_type dereference() const
48 {
49 return m_functor(m_index);
50 }
51
52 void increment()
53 {
54 ++m_index;
55 }
56
57 void decrement()
58 {
59 --m_index;
60 }
61
62 void advance(difference_type n)
63 {
64 if (n >= 0)
65 {
66 m_index += static_cast<size_type>(n);
67 }
68 else
69 {
70 SPARROW_ASSERT_TRUE(std::abs(n) <= static_cast<difference_type>(m_index));
71 m_index -= static_cast<size_type>(-n);
72 }
73 }
74
75 difference_type distance_to(const self_type& rhs) const
76 {
77 return static_cast<difference_type>(rhs.m_index) - static_cast<difference_type>(m_index);
78 }
79
80 [[nodiscard]] bool equal(const self_type& rhs) const
81 {
82 return m_index == rhs.m_index;
83 }
84
85 [[nodiscard]] bool less_than(const self_type& rhs) const
86 {
87 return m_index < rhs.m_index;
88 }
89
90 FUNCTOR m_functor = FUNCTOR{};
91 size_type m_index = 0;
92
93 friend class iterator_access;
94 };
95}
constexpr functor_index_iterator(FUNCTOR functor, size_type index)
constexpr functor_index_iterator()=default
functor_index_iterator< detail::layout_value_functor< array_type, inner_value_type > > self_type
std::invoke_result_t< detail::layout_value_functor< array_type, inner_value_type >, std::size_t > result_type
#define SPARROW_ASSERT_TRUE(expr__)