JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
ZipIterator.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPLSpatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2024 Jaroslav Pevno, JPLSpatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <utility>
23#include <iterator>
24#include <type_traits>
25
26namespace JPL
27{
29
30 // ----- pair_ref & arrow proxy -----
31 template<class First, class Second>
32 struct pair_ref
33 {
36
37 // Allows structured bindings and easy copying to a real pair if needed
38 constexpr operator std::pair<First, Second>() const { return { first, second }; }
39 };
40
41 template<class First, class Second>
47
48 // ----- zip_iter -----
49 template<class First, class Second>
51 {
52 template<class, class> friend class zip_iter; // allow access to p1_/p2_
53 public:
54 using difference_type = std::ptrdiff_t;
55 using value_type = std::pair<std::remove_cv_t<First>, std::remove_cv_t<Second>>;
58 using iterator_category = std::random_access_iterator_tag;
59
60 constexpr zip_iter() noexcept : p1_(nullptr), p2_(nullptr) {}
61 constexpr zip_iter(First* p1, Second* p2) noexcept : p1_(p1), p2_(p2) {}
62
63 // dereference
64 constexpr reference operator*() const noexcept { return { *p1_, *p2_ }; }
65 constexpr pointer operator->() const noexcept { return pointer{ **this }; }
66 constexpr reference operator[](difference_type n) const noexcept { return *(*this + n); }
67
68 // ++/--
69 constexpr zip_iter& operator++() noexcept { ++p1_; ++p2_; return *this; }
70 constexpr zip_iter operator++(int) noexcept { auto tmp = *this; ++(*this); return tmp; }
71 constexpr zip_iter& operator--() noexcept { --p1_; --p2_; return *this; }
72 constexpr zip_iter operator--(int) noexcept { auto tmp = *this; --(*this); return tmp; }
73
74 // +/- n
75 constexpr zip_iter& operator+=(difference_type n) noexcept { p1_ += n; p2_ += n; return *this; }
76 constexpr zip_iter& operator-=(difference_type n) noexcept { return (*this += -n); }
77
78 friend zip_iter operator+(zip_iter it, difference_type n) noexcept { it += n; return it; }
79 friend zip_iter operator+(difference_type n, zip_iter it) noexcept { it += n; return it; }
80 friend zip_iter operator-(zip_iter it, difference_type n) noexcept { it -= n; return it; }
81
82 // distance (supports const/non-const mixes)
83 template<class F2, class S2>
84 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
85 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
86 friend difference_type operator-(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept
87 {
88 return a.p1_ - b.p1_;
89 }
90
91 // comparisons (compare by the first pointer; both advance together)
92 template<class F2, class S2>
93 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
94 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
95 friend bool operator==(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept
96 {
97 return a.p1_ == b.p1_;
98 }
99
100 template<class F2, class S2>
101 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
102 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
103 friend bool operator<(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept
104 {
105 return a.p1_ < b.p1_;
106 }
107
108 template<class F2, class S2>
109 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
110 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
111 friend bool operator!=(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept { return !(a == b); }
112
113 template<class F2, class S2>
114 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
115 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
116 friend bool operator<=(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept { return !(b < a); }
117
118 template<class F2, class S2>
119 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
120 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
121 friend bool operator>(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept { return (b < a); }
122
123 template<class F2, class S2>
124 requires (std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>>
125 && std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>>)
126 friend bool operator>=(const zip_iter& a, const zip_iter<F2, S2>& b) noexcept { return !(a < b); }
127
128 // enabling conversion when pointer types are convertible (e.g., T* -> const T*)
129 template<class F2, class S2> requires (
130 std::is_same_v<std::remove_cv_t<F2>, std::remove_cv_t<First>> &&
131 std::is_same_v<std::remove_cv_t<S2>, std::remove_cv_t<Second>> &&
132 std::is_convertible_v<F2*, First*>&&
133 std::is_convertible_v<S2*, Second*>)
135 : p1_(other.p1_), p2_(other.p2_)
136 {
137 }
138
139 private:
140 First* p1_;
141 Second* p2_;
142 };
143
144
145 // ----- tiny range helper -----
146 template<class First, class Second>
148 {
150 using const_iterator = zip_iter<const std::remove_cv_t<First>, const std::remove_cv_t<Second>>;
151
154 std::size_t n{};
155
156 constexpr iterator begin() noexcept { return { a, b }; }
157 constexpr iterator end() noexcept { return { a + n, b + n }; }
158
159 constexpr const_iterator begin() const noexcept { return { a, b }; }
160 constexpr const_iterator end() const noexcept { return { a + n, b + n }; }
161 };
162
163} // namespace JPL
Definition ZipIterator.h:51
constexpr zip_iter & operator+=(difference_type n) noexcept
Definition ZipIterator.h:75
std::ptrdiff_t difference_type
Definition ZipIterator.h:54
zip_iter(const zip_iter< F2, S2 > &other) noexcept
Definition ZipIterator.h:134
constexpr zip_iter & operator++() noexcept
Definition ZipIterator.h:69
constexpr reference operator*() const noexcept
Definition ZipIterator.h:64
constexpr zip_iter operator++(int) noexcept
Definition ZipIterator.h:70
constexpr pointer operator->() const noexcept
Definition ZipIterator.h:65
constexpr zip_iter() noexcept
Definition ZipIterator.h:60
constexpr reference operator[](difference_type n) const noexcept
Definition ZipIterator.h:66
friend zip_iter operator-(zip_iter it, difference_type n) noexcept
Definition ZipIterator.h:80
constexpr zip_iter operator--(int) noexcept
Definition ZipIterator.h:72
std::pair< std::remove_cv_t< First >, std::remove_cv_t< Second > > value_type
Definition ZipIterator.h:55
friend zip_iter operator+(zip_iter it, difference_type n) noexcept
Definition ZipIterator.h:78
constexpr zip_iter & operator--() noexcept
Definition ZipIterator.h:71
constexpr zip_iter(First *p1, Second *p2) noexcept
Definition ZipIterator.h:61
std::random_access_iterator_tag iterator_category
Definition ZipIterator.h:58
constexpr zip_iter & operator-=(difference_type n) noexcept
Definition ZipIterator.h:76
friend zip_iter operator+(difference_type n, zip_iter it) noexcept
Definition ZipIterator.h:79
Definition AcousticMaterial.h:36
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Definition ZipIterator.h:43
pair_ref< First, Second > ref
Definition ZipIterator.h:44
constexpr pair_ref< First, Second > * operator->() noexcept
Definition ZipIterator.h:45
Zip iterator for the FlatMap.
Definition ZipIterator.h:33
Second & second
Definition ZipIterator.h:35
First & first
Definition ZipIterator.h:34
Definition ZipIterator.h:148
Second * b
Definition ZipIterator.h:153
First * a
Definition ZipIterator.h:152
constexpr const_iterator begin() const noexcept
Definition ZipIterator.h:159
constexpr const_iterator end() const noexcept
Definition ZipIterator.h:160
constexpr iterator begin() noexcept
Definition ZipIterator.h:156
constexpr iterator end() noexcept
Definition ZipIterator.h:157
std::size_t n
Definition ZipIterator.h:154