OpenShot Audio Library | OpenShotAudio: juce_Enumerate.h Source File

OpenShot Audio Library | OpenShotAudio 0.6.0

Loading...
Searching...
No Matches
juce_Enumerate.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26namespace detail
27{
28
29template <typename T, typename = void>
30constexpr auto canPreDecrement = false;
31
32template <typename T>
33constexpr auto canPreDecrement<T, std::void_t<decltype (--std::declval<T>())>> = true;
34
35template <typename T, typename I, typename = void>
36constexpr auto canAddAssign = false;
37
38template <typename T, typename I>
39constexpr auto canAddAssign<T, I, std::void_t<decltype (std::declval<T>() += std::declval<I>())>> = true;
40
41template <typename T, typename I, typename = void>
42constexpr auto canSubAssign = false;
43
44template <typename T, typename I>
45constexpr auto canSubAssign<T, I, std::void_t<decltype (std::declval<T>() -= std::declval<I>())>> = true;
46
47template <typename T, typename I, typename = void>
48constexpr auto canAdd = false;
49
50template <typename T, typename I>
51constexpr auto canAdd<T, I, std::void_t<decltype (std::declval<T>() + std::declval<I>())>> = true;
52
53template <typename T, typename I, typename = void>
54constexpr auto canSub = false;
55
56template <typename T, typename I>
57constexpr auto canSub<T, I, std::void_t<decltype (std::declval<T>() - std::declval<I>())>> = true;
58
59template <typename T, typename I, typename = void>
60constexpr auto canLessThan = false;
61
62template <typename T, typename I>
63constexpr auto canLessThan<T, I, std::void_t<decltype (std::declval<T>() < std::declval<I>())>> = true;
64
65template <typename T, typename I, typename = void>
66constexpr auto canLessThanEqual = false;
67
68template <typename T, typename I>
69constexpr auto canLessThanEqual<T, I, std::void_t<decltype (std::declval<T>() <= std::declval<I>())>> = true;
70
71template <typename T, typename I, typename = void>
72constexpr auto canGreaterThan = false;
73
74template <typename T, typename I>
75constexpr auto canGreaterThan<T, I, std::void_t<decltype (std::declval<T>() > std::declval<I>())>> = true;
76
77template <typename T, typename I, typename = void>
78constexpr auto canGreaterThanEqual = false;
79
80template <typename T, typename I>
81constexpr auto canGreaterThanEqual<T, I, std::void_t<decltype (std::declval<T>() >= std::declval<I>())>> = true;
82
83namespace withAdlSize
84{
85 using std::size;
86
87 template <typename Range>
88 using AdlSize = decltype (size (std::declval<Range>()));
89
90 template <typename Range>
91 using AdlSignedSize = std::common_type_t<std::ptrdiff_t, std::make_signed_t<AdlSize<Range>>>;
92}
93
94} // namespace detail
95
108template <typename Index, typename Value>
109 struct Enumerated
110{
111 Index index;
112 Value value;
113};
114
126template <typename Iter, typename Index = ptrdiff_t>
127 class EnumerateIterator
128{
129public:
131 constexpr EnumerateIterator() = default;
132
134 constexpr explicit EnumerateIterator (Iter iter)
135 : EnumerateIterator (std::move (iter), Index{}) {}
136
138 constexpr EnumerateIterator (Iter iter, Index ind)
139 : iterator (std::move (iter)), index (ind) {}
140
142 template <typename OtherIter, typename OtherInd>
143 [[nodiscard]] constexpr bool operator== (const EnumerateIterator<OtherIter, OtherInd>& other) const
144 {
145 return iterator == other.iterator;
146 }
147
149 template <typename OtherIter, typename OtherInd>
150 [[nodiscard]] constexpr bool operator!= (const EnumerateIterator<OtherIter, OtherInd>& other) const
151 {
152 return ! operator== (other);
153 }
154
159 [[nodiscard]] constexpr Enumerated<Index, decltype (*std::declval<Iter>())> operator*() const
160 {
161 return { index, *iterator };
162 }
163
165 constexpr EnumerateIterator& operator++()
166 {
167 ++iterator;
168 ++index;
169 return *this;
170 }
171
173 constexpr EnumerateIterator operator++ (int)
174 {
175 auto copy = *this;
176 operator++();
177 return copy;
178 }
179
183 template <typename T = Iter, std::enable_if_t<detail::canPreDecrement<T>, int> = 0>
184 constexpr EnumerateIterator& operator--()
185 {
186 --iterator;
187 --index;
188 return *this;
189 }
190
194 template <typename T = Iter, std::enable_if_t<detail::canPreDecrement<T>, int> = 0>
195 constexpr EnumerateIterator operator-- (int)
196 {
197 auto copy = *this;
198 operator--();
199 return copy;
200 }
201
205 template <typename I, std::enable_if_t<detail::canAddAssign<Iter&, I>, int> = 0>
206 constexpr EnumerateIterator& operator+= (I diff)
207 {
208 iterator += diff;
209 index += static_cast<Index> (diff);
210 return *this;
211 }
212
216 template <typename I, std::enable_if_t<detail::canSubAssign<Iter&, I>, int> = 0>
217 constexpr EnumerateIterator& operator-= (I diff)
218 {
219 iterator -= diff;
220 index -= static_cast<Index> (diff);
221 return *this;
222 }
223
229 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canSub<Iter, OtherIter>, int> = 0>
230 [[nodiscard]] constexpr auto operator- (const EnumerateIterator<OtherIter, OtherInd>& other) const
231 {
232 return iterator - other.iterator;
233 }
234
240 template <typename I, std::enable_if_t<detail::canAdd<EnumerateIterator, I>, int> = 0>
241 [[nodiscard]] constexpr auto operator[] (I diff) const
242 {
243 return *(*this + diff);
244 }
245
249 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canLessThan<Iter, OtherIter>, int> = 0>
250 [[nodiscard]] constexpr bool operator< (const EnumerateIterator<OtherIter, OtherInd>& other) const
251 {
252 return iterator < other.iterator;
253 }
254
258 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canLessThanEqual<Iter, OtherIter>, int> = 0>
259 [[nodiscard]] constexpr bool operator<= (const EnumerateIterator<OtherIter, OtherInd>& other) const
260 {
261 return iterator <= other.iterator;
262 }
263
267 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canGreaterThan<Iter, OtherIter>, int> = 0>
268 [[nodiscard]] constexpr bool operator> (const EnumerateIterator<OtherIter, OtherInd>& other) const
269 {
270 return iterator > other.iterator;
271 }
272
276 template <typename OtherIter, typename OtherInd, std::enable_if_t<detail::canGreaterThanEqual<Iter, OtherIter>, int> = 0>
277 [[nodiscard]] constexpr bool operator>= (const EnumerateIterator<OtherIter, OtherInd>& other) const
278 {
279 return iterator >= other.iterator;
280 }
281
285 template <typename I, std::enable_if_t<detail::canAddAssign<EnumerateIterator&, I>, int> = 0>
286 constexpr friend auto operator+ (EnumerateIterator iter, I ind)
287 {
288 return iter += ind;
289 }
290
294 template <typename I, std::enable_if_t<detail::canAddAssign<EnumerateIterator&, I>, int> = 0>
295 constexpr friend auto operator+ (I ind, EnumerateIterator iter)
296 {
297 return iter += ind;
298 }
299
303 template <typename I, std::enable_if_t<detail::canSubAssign<EnumerateIterator&, I>, int> = 0>
304 constexpr friend auto operator- (EnumerateIterator iter, I ind)
305 {
306 return iter -= ind;
307 }
308
309private:
310 Iter iterator{};
311 Index index = 0;
312};
313
314//==============================================================================
324template <typename Begin, typename End>
325 class IteratorPair
326{
327public:
331 constexpr IteratorPair (Begin bIn, End eIn)
332 : b (std::move (bIn)), e (std::move (eIn)) {}
333
335 constexpr auto begin() const { return b; }
336
338 constexpr auto end() const { return e; }
339
340private:
341 Begin b;
342 End e;
343};
344
352template <typename Begin, typename End = Begin>
353[[nodiscard]] constexpr auto makeRange (Begin begin, End end)
354{
355 return IteratorPair<Begin, End> { std::move (begin), std::move (end) };
356}
357
358//==============================================================================
418template <typename Range, typename Index = detail::withAdlSize::AdlSignedSize<Range>>
419[[nodiscard]] constexpr auto enumerate (Range&& range, Index startingValue = {})
420{
421 // This ensures argument-dependent lookup works properly for user-defined non-member begin/end
422 using std::begin, std::end;
423 return makeRange (EnumerateIterator { begin (range), startingValue },
424 EnumerateIterator { end (range), startingValue });
425}
426
427} // namespace juce
juce::EnumerateIterator
Definition juce_Enumerate.h:128
juce::EnumerateIterator::EnumerateIterator
constexpr EnumerateIterator(Iter iter, Index ind)
Definition juce_Enumerate.h:138
juce::EnumerateIterator::operator-=
constexpr EnumerateIterator & operator-=(I diff)
Definition juce_Enumerate.h:217
juce::EnumerateIterator::operator++
constexpr EnumerateIterator & operator++()
Definition juce_Enumerate.h:165
juce::EnumerateIterator::operator<
constexpr bool operator<(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:250
juce::EnumerateIterator::EnumerateIterator
constexpr EnumerateIterator()=default
juce::EnumerateIterator::EnumerateIterator
constexpr EnumerateIterator(Iter iter)
Definition juce_Enumerate.h:134
juce::EnumerateIterator::operator!=
constexpr bool operator!=(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:150
juce::EnumerateIterator::operator>
constexpr bool operator>(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:268
juce::EnumerateIterator::operator--
constexpr EnumerateIterator & operator--()
Definition juce_Enumerate.h:184
juce::EnumerateIterator::operator+
constexpr friend auto operator+(EnumerateIterator iter, I ind)
Definition juce_Enumerate.h:286
juce::EnumerateIterator::operator[]
constexpr auto operator[](I diff) const
Definition juce_Enumerate.h:241
juce::EnumerateIterator::operator<=
constexpr bool operator<=(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:259
juce::EnumerateIterator::operator+=
constexpr EnumerateIterator & operator+=(I diff)
Definition juce_Enumerate.h:206
juce::EnumerateIterator::operator==
constexpr bool operator==(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:143
juce::EnumerateIterator::operator-
constexpr auto operator-(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:230
juce::EnumerateIterator::operator>=
constexpr bool operator>=(const EnumerateIterator< OtherIter, OtherInd > &other) const
Definition juce_Enumerate.h:277
juce::IteratorPair::IteratorPair
constexpr IteratorPair(Begin bIn, End eIn)
Definition juce_Enumerate.h:331
juce::IteratorPair::end
constexpr auto end() const
Definition juce_Enumerate.h:338
juce::IteratorPair::begin
constexpr auto begin() const
Definition juce_Enumerate.h:335
juce::Range
Definition juce_Range.h:40
juce::Value
Definition juce_Value.h:51
juce::Enumerated
Definition juce_Enumerate.h:110