sparrow 2.3.1
C++20 idiomatic APIs for the Apache Arrow Columnar Format
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() noexcept(SPARROW_CONTRACTS_THROW_ON_FAILURE == 0)
54 {
55 SPARROW_ASSERT_TRUE(m_index < std::numeric_limits<size_type>::max());
56 ++m_index;
57 }
58
59 constexpr void decrement() noexcept(SPARROW_CONTRACTS_THROW_ON_FAILURE == 0)
60 {
61 SPARROW_ASSERT_TRUE(m_index > 0);
62 --m_index;
63 }
64
65 constexpr void advance(difference_type n) noexcept(SPARROW_CONTRACTS_THROW_ON_FAILURE == 0)
66 {
67 if (n >= 0)
68 {
69 m_index += static_cast<size_type>(n);
70 }
71 else
72 {
73 SPARROW_ASSERT_TRUE(std::abs(n) <= static_cast<difference_type>(m_index));
74 m_index -= static_cast<size_type>(-n);
75 }
76 }
77
78 [[nodiscard]] constexpr difference_type
79 distance_to(const self_type& rhs) const noexcept(SPARROW_CONTRACTS_THROW_ON_FAILURE == 0)
80 {
81 return static_cast<difference_type>(rhs.m_index) - static_cast<difference_type>(m_index);
82 }
83
84 [[nodiscard]] constexpr bool equal(const self_type& rhs) const noexcept
85 {
86 return m_index == rhs.m_index;
87 }
88
89 [[nodiscard]] constexpr bool less_than(const self_type& rhs) const noexcept
90 {
91 return m_index < rhs.m_index;
92 }
93
94 FUNCTOR m_functor = FUNCTOR{};
95 size_type m_index = 0;
96
97 friend class iterator_access;
98 };
99}
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__)
#define SPARROW_CONTRACTS_THROW_ON_FAILURE
Definition contracts.hpp:24
Extensions to the C++ standard library.