GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
algorithms.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_ALGORITHMS_HPP
5#define GREM_CORE_ALGORITHMS_HPP
6
8
12
13#include <algorithm> // std::...
14#include <cstddef> // std::size_t
15#include <functional> // std::hash, std::invoke
16#include <iterator> // std::begin, std::end, std::size, std::next, std::distance, std::iterator_traits, std::random_access_iterator_tag
17#include <numeric> // std::accumulate, std::reduce, std::iota
18#include <utility> // std::move, std::forward, std::swap
19
20namespace grem {
21
22struct EqualTo {
23 template <typename A, typename B>
24 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) == std::forward<B>(b)) {
25 return std::forward<A>(a) == std::forward<B>(b);
26 }
27};
28inline constexpr EqualTo EQUAL_TO{};
29
30struct NotEqualTo {
31 template <typename A, typename B>
32 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) != std::forward<B>(b)) {
33 return std::forward<A>(a) != std::forward<B>(b);
34 }
35};
36inline constexpr NotEqualTo NOT_EQUAL_TO{};
37
38struct LessThan {
39 template <typename A, typename B>
40 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) < std::forward<B>(b)) {
41 return std::forward<A>(a) < std::forward<B>(b);
42 }
43};
44inline constexpr LessThan LESS_THAN{};
45
47 template <typename A, typename B>
48 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) <= std::forward<B>(b)) {
49 return std::forward<A>(a) <= std::forward<B>(b);
50 }
51};
53
55 template <typename A, typename B>
56 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) > std::forward<B>(b)) {
57 return std::forward<A>(a) > std::forward<B>(b);
58 }
59};
60inline constexpr GreaterThan GREATER_THAN{};
61
63 template <typename A, typename B>
64 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) >= std::forward<B>(b)) {
65 return std::forward<A>(a) >= std::forward<B>(b);
66 }
67};
69
70struct Add {
71 template <typename A, typename B>
72 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) + std::forward<B>(b)) {
73 return std::forward<A>(a) + std::forward<B>(b);
74 }
75};
76inline constexpr Add ADD{};
77
78struct Subtract {
79 template <typename A, typename B>
80 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) - std::forward<B>(b)) {
81 return std::forward<A>(a) - std::forward<B>(b);
82 }
83};
84inline constexpr Subtract SUBTRACT{};
85
86struct Multiply {
87 template <typename A, typename B>
88 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) * std::forward<B>(b)) {
89 return std::forward<A>(a) * std::forward<B>(b);
90 }
91};
92inline constexpr Multiply MULTIPLY{};
93
94struct Divide {
95 template <typename A, typename B>
96 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) / std::forward<B>(b)) {
97 return std::forward<A>(a) / std::forward<B>(b);
98 }
99};
100inline constexpr Divide DIVIDE{};
101
102struct Modulo {
103 template <typename A, typename B>
104 [[nodiscard]] constexpr auto operator()(A&& a, B&& b) const -> decltype(std::forward<A>(a) % std::forward<B>(b)) {
105 return std::forward<A>(a) % std::forward<B>(b);
106 }
107};
108inline constexpr Modulo MODULO{};
109
110struct Negate {
111 template <typename A>
112 [[nodiscard]] constexpr auto operator()(A&& a) const -> decltype(-std::forward<A>(a)) {
113 return -std::forward<A>(a);
114 }
115};
116inline constexpr Negate NEGATE{};
117
118template <typename T>
120public:
121 using is_transparent = void;
122
123 [[nodiscard]] constexpr size_t operator()(const T& value) const {
124 return hasher(value);
125 }
126
127private:
128 [[no_unique_address]] std::hash<T> hasher;
129};
130
131template <typename T>
133public:
134 using is_transparent = void;
135
136 [[nodiscard]] constexpr size_t operator()(const T& a, const T& b) const {
137 return a == b;
138 }
139};
140
141[[nodiscard]] constexpr std::size_t combineHashes(std::size_t hash, convertible_to<std::size_t> auto... otherHashes) {
142 ((hash ^= std::size_t{otherHashes} + (hash << 6) + (hash >> 2)), ...);
143 return hash;
144}
145
146template <typename T, typename... Ts>
147[[nodiscard]] constexpr std::size_t getHash(const T& value, const Ts&... otherValues) {
148 return combineHashes(std::hash<T>{}(value), std::hash<Ts>{}(otherValues)...);
149}
150
151[[nodiscard]] constexpr std::size_t getRangeHash(input_range auto&& range) {
152 auto it = std::begin(range);
153 auto end = std::end(range);
154 if (it != end) {
155 std::size_t result = getHash(*it++);
156 while (it != end) {
157 result = combineHashes(result, getHash(*it++));
158 }
159 return result;
160 }
161 return 0;
162}
163
164constexpr void iterSwap(auto itA, auto itB) {
165 using std::swap;
166 swap(*itA, *itB);
167}
168
169constexpr void swapRanges(input_range auto&& rangeA, input_range auto&& rangeB) {
170 auto itA = std::begin(rangeA);
171 auto endA = std::end(rangeA);
172 auto itB = std::begin(rangeB);
173 auto endB = std::end(rangeB);
174 while (itA != endA && itB != endB) {
175 iterSwap(itA, itB);
176 ++itA;
177 ++itB;
178 }
179}
180
181constexpr void forEach(input_range auto&& range, auto operation) {
182 std::for_each(std::begin(range), std::end(range), std::move(operation));
183}
184
185constexpr auto transform(input_range auto&& range, auto outputIterator, auto transformationFunction) {
186 return std::transform(std::begin(range), std::end(range), std::move(outputIterator), std::move(transformationFunction));
187}
188
189constexpr auto transform(input_range auto&& rangeA, input_range auto&& rangeB, auto outputIterator, auto transformationFunction) {
190 auto itA = std::begin(rangeA);
191 auto endA = std::end(rangeA);
192 auto itB = std::begin(rangeB);
193 auto endB = std::end(rangeB);
194 while (itA != endA && itB != endB) {
195 *outputIterator++ = std::invoke(transformationFunction, *itA, *itB);
196 ++itA;
197 ++itB;
198 }
199 return outputIterator;
200}
201
202[[nodiscard]] constexpr auto accumulate(input_range auto&& range, auto initialValue) {
203 return std::accumulate(std::begin(range), std::end(range), std::move(initialValue));
204}
205
206[[nodiscard]] constexpr auto accumulate(input_range auto&& range, auto initialValue, auto reductionFunction) {
207 return std::accumulate(std::begin(range), std::end(range), std::move(initialValue), std::move(reductionFunction));
208}
209
210[[nodiscard]] constexpr auto reduce(input_range auto&& range) {
211 return std::reduce(std::begin(range), std::end(range));
212}
213
214[[nodiscard]] constexpr auto reduce(input_range auto&& range, auto initialValue) {
215 return std::reduce(std::begin(range), std::end(range), std::move(initialValue));
216}
217
218[[nodiscard]] constexpr auto reduce(input_range auto&& range, auto initialValue, auto reductionFunction) {
219 return std::reduce(std::begin(range), std::end(range), std::move(initialValue), std::move(reductionFunction));
220}
221
222[[nodiscard]] constexpr bool anyOf(input_range auto&& range, auto predicate) {
223 return std::any_of(std::begin(range), std::end(range), std::move(predicate));
224}
225
226[[nodiscard]] constexpr bool allOf(input_range auto&& range, auto predicate) {
227 return std::all_of(std::begin(range), std::end(range), std::move(predicate));
228}
229
230[[nodiscard]] constexpr bool noneOf(input_range auto&& range, auto predicate) {
231 return std::none_of(std::begin(range), std::end(range), std::move(predicate));
232}
233
234[[nodiscard]] constexpr auto count(input_range auto&& range, const auto& value) {
235 return std::count(std::begin(range), std::end(range), value);
236}
237
238[[nodiscard]] constexpr auto countIf(input_range auto&& range, auto predicate) {
239 return std::count_if(std::begin(range), std::end(range), std::move(predicate));
240}
241
242[[nodiscard]] constexpr auto find(input_range auto&& range, const auto& value) {
243 return std::find(std::begin(range), std::end(range), value);
244}
245
246template <auto Member>
247[[nodiscard]] constexpr auto findBy(input_range auto&& range, const auto& value) {
248 return std::find_if(std::begin(range), std::end(range), [&](const auto& element) -> bool { return element.*Member == value; });
249}
250
251[[nodiscard]] constexpr auto findIf(input_range auto&& range, auto predicate) {
252 return std::find_if(std::begin(range), std::end(range), std::move(predicate));
253}
254
255[[nodiscard]] constexpr auto findIfNot(input_range auto&& range, auto predicate) {
256 return std::find_if_not(std::begin(range), std::end(range), std::move(predicate));
257}
258
259[[nodiscard]] constexpr auto findFirstOf(input_range auto&& range, forward_range auto&& values) {
260 return std::find_first_of(std::begin(range), std::end(range), std::begin(values), std::end(values));
261}
262
263[[nodiscard]] constexpr auto findFirstOf(input_range auto&& range, forward_range auto&& values, auto equal) {
264 return std::find_first_of(std::begin(range), std::end(range), std::begin(values), std::end(values), std::move(equal));
265}
266
267[[nodiscard]] constexpr auto findEnd(forward_range auto&& range, forward_range auto&& values) {
268 return std::find_end(std::begin(range), std::end(range), std::begin(values), std::end(values));
269}
270
271[[nodiscard]] constexpr auto findEnd(forward_range auto&& range, forward_range auto&& values, auto equal) {
272 return std::find_end(std::begin(range), std::end(range), std::begin(values), std::end(values), std::move(equal));
273}
274
275[[nodiscard]] constexpr auto search(forward_range auto&& range, forward_range auto&& sequence) {
276 return std::search(std::begin(range), std::end(range), std::begin(sequence), std::end(sequence));
277}
278
279[[nodiscard]] constexpr auto search(forward_range auto&& range, forward_range auto&& sequence, auto equal) {
280 return std::search(std::begin(range), std::end(range), std::begin(sequence), std::end(sequence), std::move(equal));
281}
282
283[[nodiscard]] constexpr auto mismatch(input_range auto&& range, input_range auto&& sequence) {
284 return std::mismatch(std::begin(range), std::end(range), std::begin(sequence), std::end(sequence));
285}
286
287[[nodiscard]] constexpr auto mismatch(input_range auto&& range, input_range auto&& sequence, auto equal) {
288 return std::mismatch(std::begin(range), std::end(range), std::begin(sequence), std::end(sequence), std::move(equal));
289}
290
291[[nodiscard]] constexpr auto adjacentFind(forward_range auto&& range) {
292 return std::adjacent_find(std::begin(range), std::end(range));
293}
294
295[[nodiscard]] constexpr auto adjacentFind(forward_range auto&& range, auto equal) {
296 return std::adjacent_find(std::begin(range), std::end(range), std::move(equal));
297}
298
299[[nodiscard]] constexpr bool contains(input_range auto&& range, const auto& value) {
300 auto end = std::end(range);
301 return std::find(std::begin(range), end, value) != end;
302}
303
304template <auto Member>
305[[nodiscard]] constexpr auto containsBy(input_range auto&& range, const auto& value) {
306 auto end = std::end(range);
307 return std::find_if(std::begin(range), end, [&](const auto& element) -> bool { return element.*Member == value; }) != end;
308}
309
310[[nodiscard]] constexpr bool contains(input_range auto&& range, const auto& value, auto equal) {
311 auto end = std::end(range);
312 return std::find_if(std::begin(range), end, [&](const auto& v) -> bool { return std::invoke(equal, v, value); }) != end;
313}
314
315[[nodiscard]] constexpr bool containsSubrange(forward_range auto&& range, forward_range auto&& sequence) {
316 auto sequenceBegin = std::begin(sequence);
317 auto sequenceEnd = std::end(sequence);
318 if (sequenceBegin == sequenceEnd) {
319 return true;
320 }
321 auto end = std::end(range);
322 return std::search(std::begin(range), end, std::begin(sequence), std::move(sequenceBegin), std::move(sequenceEnd)) != end;
323}
324
325[[nodiscard]] constexpr bool containsSubrange(forward_range auto&& range, forward_range auto&& sequence, auto equal) {
326 auto sequenceBegin = std::begin(sequence);
327 auto sequenceEnd = std::end(sequence);
328 if (sequenceBegin == sequenceEnd) {
329 return true;
330 }
331 auto end = std::end(range);
332 return std::search(std::begin(range), end, std::begin(sequence), std::move(sequenceBegin), std::move(sequenceEnd), std::move(equal)) != end;
333}
334
335[[nodiscard]] constexpr bool startsWith(input_range auto&& range, input_range auto&& sequence) {
336 auto sequenceEnd = std::end(sequence);
337 return std::mismatch(std::begin(range), std::end(range), std::begin(sequence), std::begin(sequence), sequenceEnd).second == sequenceEnd;
338}
339
340[[nodiscard]] constexpr bool startsWith(input_range auto&& range, input_range auto&& sequence, auto equal) {
341 auto sequenceEnd = std::end(sequence);
342 return std::mismatch(std::begin(range), std::end(range), std::begin(sequence), std::begin(sequence), sequenceEnd, std::move(equal)).second == sequenceEnd;
343}
344
345[[nodiscard]] constexpr bool endsWith(input_range auto&& range, input_range auto&& sequence) {
346 const auto rangeSize = std::size(range);
347 const auto sequenceSize = std::size(sequence);
348 return sequenceSize <= rangeSize && std::equal(std::next(std::begin(range), (rangeSize - sequenceSize)), std::end(range), std::begin(sequence), std::end(sequence));
349}
350
351[[nodiscard]] constexpr bool endsWith(input_range auto&& range, input_range auto&& sequence, auto equal) {
352 const auto rangeSize = std::size(range);
353 const auto sequenceSize = std::size(sequence);
354 return sequenceSize <= rangeSize &&
355 std::equal(std::next(std::begin(range), (rangeSize - sequenceSize)), std::end(range), std::begin(sequence), std::end(sequence), std::move(equal));
356}
357
358[[nodiscard]] constexpr bool equal(input_range auto&& rangeA, input_range auto&& rangeB) {
359 return std::equal(std::begin(rangeA), std::end(rangeA), std::begin(rangeB), std::end(rangeB));
360}
361
362[[nodiscard]] constexpr bool equal(input_range auto&& rangeA, input_range auto&& rangeB, auto equal) {
363 return std::equal(std::begin(rangeA), std::end(rangeA), std::begin(rangeB), std::end(rangeB), std::move(equal));
364}
365
366[[nodiscard]] constexpr bool lexicographicalCompare(input_range auto&& rangeA, input_range auto&& rangeB) {
367 return std::lexicographical_compare(std::begin(rangeA), std::end(rangeA), std::begin(rangeB), std::end(rangeB));
368}
369
370[[nodiscard]] constexpr bool lexicographicalCompare(input_range auto&& rangeA, input_range auto&& rangeB, auto compare) {
371 return std::lexicographical_compare(std::begin(rangeA), std::end(rangeA), std::begin(rangeB), std::end(rangeB), std::move(compare));
372}
373
374[[nodiscard]] constexpr auto minElement(forward_range auto&& range) {
375 return std::min_element(std::begin(range), std::end(range));
376}
377
378[[nodiscard]] constexpr auto minElement(forward_range auto&& range, auto compare) {
379 return std::min_element(std::begin(range), std::end(range), std::move(compare));
380}
381
382[[nodiscard]] constexpr auto maxElement(forward_range auto&& range) {
383 return std::max_element(std::begin(range), std::end(range));
384}
385
386[[nodiscard]] constexpr auto maxElement(forward_range auto&& range, auto compare) {
387 return std::max_element(std::begin(range), std::end(range), std::move(compare));
388}
389
390[[nodiscard]] constexpr auto minmaxElement(forward_range auto&& range) {
391 return std::minmax_element(std::begin(range), std::end(range));
392}
393
394[[nodiscard]] constexpr auto minmaxElement(forward_range auto&& range, auto compare) {
395 return std::minmax_element(std::begin(range), std::end(range), std::move(compare));
396}
397
398[[nodiscard]] constexpr bool isSorted(forward_range auto&& range) {
399 return std::is_sorted(std::begin(range), std::end(range));
400}
401
402[[nodiscard]] constexpr bool isSorted(forward_range auto&& range, auto compare) {
403 return std::is_sorted(std::begin(range), std::end(range), std::move(compare));
404}
405
406[[nodiscard]] constexpr bool isPartitioned(input_range auto&& range, auto compare) {
407 return std::is_partitioned(std::begin(range), std::end(range), std::move(compare));
408}
409
410[[nodiscard]] constexpr bool includes(input_range auto&& sortedRange, input_range auto&& sortedSequence) {
411 return std::includes(std::begin(sortedRange), std::end(sortedRange), std::begin(sortedSequence), std::end(sortedSequence));
412}
413
414[[nodiscard]] constexpr bool includes(input_range auto&& sortedRange, input_range auto&& sortedSequence, auto compare) {
415 return std::includes(std::begin(sortedRange), std::end(sortedRange), std::begin(sortedSequence), std::end(sortedSequence), std::move(compare));
416}
417
418[[nodiscard]] constexpr bool binarySearch(forward_range auto&& sortedRange, const auto& value) {
419 return std::binary_search(std::begin(sortedRange), std::end(sortedRange), value);
420}
421
422[[nodiscard]] constexpr bool binarySearch(forward_range auto&& sortedRange, const auto& value, auto compare) {
423 return std::binary_search(std::begin(sortedRange), std::end(sortedRange), value, std::move(compare));
424}
425
426[[nodiscard]] constexpr auto lowerBound(forward_range auto&& sortedRange, const auto& value, auto compare) {
427 // Apparently, Clang optimizes std::lower_bound well, but the other compilers don't.
428 // Meanwhile, the if-statement in the custom implementation below gets optimized into a CMOV under all compilers, except for Clang (as of 21.1.0).
429 // Funny how that works out.
430#ifdef __clang__
431 return std::lower_bound(std::begin(sortedRange), std::end(sortedRange), value, std::move(compare));
432#else
433 if constexpr (convertible_to<typename std::iterator_traits<decltype(std::begin(sortedRange))>::iterator_category, std::random_access_iterator_tag>) {
434 auto it = std::begin(sortedRange);
435 auto n = std::end(sortedRange) - it;
436 if (n == 0) {
437 return it;
438 }
439 while (n > 1) {
440 const auto middle = n / 2;
441 if (compare(it[middle], value)) {
442 it += middle;
443 }
444 n -= middle;
445 }
446 return (compare(*it, value)) ? it + 1 : it;
447 } else {
448 return std::lower_bound(std::begin(sortedRange), std::end(sortedRange), value, std::move(compare));
449 }
450#endif
451}
452
453[[nodiscard]] constexpr auto lowerBound(forward_range auto&& sortedRange, const auto& value) {
454 return lowerBound(std::forward<decltype(sortedRange)>(sortedRange), value, LESS_THAN);
455}
456
457[[nodiscard]] constexpr auto upperBound(forward_range auto&& sortedRange, const auto& value) {
458 return std::upper_bound(std::begin(sortedRange), std::end(sortedRange), value);
459}
460
461[[nodiscard]] constexpr auto upperBound(forward_range auto&& sortedRange, const auto& value, auto compare) {
462 return std::upper_bound(std::begin(sortedRange), std::end(sortedRange), value, std::move(compare));
463}
464
465[[nodiscard]] constexpr auto equalRange(forward_range auto&& sortedRange, const auto& value, auto compare) {
466#ifdef __clang__ // See the comment in lowerBound().
467 auto [begin, end] = std::equal_range(std::begin(sortedRange), std::end(sortedRange), value, std::move(compare));
468 return Pair{std::move(begin), std::move(end)};
469#else
470 if constexpr (convertible_to<typename std::iterator_traits<decltype(std::begin(sortedRange))>::iterator_category, std::random_access_iterator_tag>) {
471 auto begin = lowerBound(std::forward<decltype(sortedRange)>(sortedRange), value, compare);
472 auto end = std::upper_bound(begin, std::end(sortedRange), value, std::move(compare));
473 return Pair{std::move(begin), std::move(end)};
474 } else {
475 const auto [begin, end] = std::equal_range(std::begin(sortedRange), std::end(sortedRange), value, std::move(compare));
476 return Pair{std::move(begin), std::move(end)};
477 }
478#endif
479}
480
481[[nodiscard]] constexpr auto equalRange(forward_range auto&& sortedRange, const auto& value) {
482 return equalRange(std::forward<decltype(sortedRange)>(sortedRange), value, LESS_THAN);
483}
484
485[[nodiscard]] constexpr auto partitionPoint(forward_range auto&& partitionedRange, auto compare) {
486 return std::partition_point(std::begin(partitionedRange), std::end(partitionedRange), std::move(compare));
487}
488
489constexpr auto merge(input_range auto&& sortedRangeA, input_range auto&& sortedRangeB, auto outputIterator) {
490 return std::merge(std::begin(sortedRangeA), std::end(sortedRangeA), std::begin(sortedRangeB), std::end(sortedRangeB), std::move(outputIterator));
491}
492
493constexpr auto merge(input_range auto&& sortedRangeA, input_range auto&& sortedRangeB, auto outputIterator, auto compare) {
494 return std::merge(std::begin(sortedRangeA), std::end(sortedRangeA), std::begin(sortedRangeB), std::end(sortedRangeB), std::move(outputIterator), std::move(compare));
495}
496
497constexpr auto inplaceMerge(bidirectional_range auto&& sortedRange, auto middleIterator) {
498 return std::inplace_merge(std::begin(sortedRange), std::move(middleIterator), std::end(sortedRange));
499}
500
501constexpr auto inplaceMerge(bidirectional_range auto&& sortedRange, auto middleIterator, auto compare) {
502 return std::inplace_merge(std::begin(sortedRange), std::move(middleIterator), std::end(sortedRange), std::move(compare));
503}
504
505constexpr void sort(random_access_range auto&& range) {
506 std::sort(std::begin(range), std::end(range));
507}
508
509constexpr void sort(random_access_range auto&& range, auto compare) {
510 std::sort(std::begin(range), std::end(range), std::move(compare));
511}
512
513template <auto... Members>
515 std::sort(std::begin(range), std::end(range), [](const auto& a, const auto& b) -> bool { return (a.*....*Members) < (b.*....*Members); });
516}
517
518template <auto... Members>
520 std::sort(std::begin(range), std::end(range), [](const auto& a, const auto& b) -> bool { return (a.*....*Members) > (b.*....*Members); });
521}
522
523constexpr void stableSort(random_access_range auto&& range) {
524 std::stable_sort(std::begin(range), std::end(range));
525}
526
527constexpr void stableSort(random_access_range auto&& range, auto compare) {
528 std::stable_sort(std::begin(range), std::end(range), std::move(compare));
529}
530
531template <auto... Members>
533 std::stable_sort(std::begin(range), std::end(range), [](const auto& a, const auto& b) -> bool { return (a.*....*Members) < (b.*....*Members); });
534}
535
536template <auto... Members>
538 std::stable_sort(std::begin(range), std::end(range), [](const auto& a, const auto& b) -> bool { return (a.*....*Members) > (b.*....*Members); });
539}
540
541constexpr void partialSort(random_access_range auto&& range, auto middleIterator) {
542 std::partial_sort(std::begin(range), middleIterator, std::end(range));
543}
544
545constexpr void partialSort(random_access_range auto&& range, auto middleIterator, auto compare) {
546 std::partial_sort(std::begin(range), middleIterator, std::end(range), std::move(compare));
547}
548
549constexpr auto partialSortCopy(input_range auto&& range, random_access_range auto&& outputRange) {
550 return std::partial_sort_copy(std::begin(range), std::end(range), std::begin(outputRange), std::end(outputRange));
551}
552
553constexpr auto partialSortCopy(input_range auto&& range, random_access_range auto&& outputRange, auto compare) {
554 return std::partial_sort_copy(std::begin(range), std::end(range), std::begin(outputRange), std::end(outputRange), std::move(compare));
555}
556
557constexpr auto partition(forward_range auto&& range, auto predicate) {
558 return std::partition(std::begin(range), std::end(range), std::move(predicate));
559}
560
561constexpr auto stablePartition(bidirectional_range auto&& range, auto predicate) {
562 return std::stable_partition(std::begin(range), std::end(range), std::move(predicate));
563}
564
565constexpr auto partitionCopy(input_range auto&& range, auto trueOutputIterator, auto falseOutputIterator, auto predicate) {
566 return std::partition_copy(std::begin(range), std::end(range), std::move(trueOutputIterator), std::move(falseOutputIterator), std::move(predicate));
567}
568
569constexpr auto nthElement(random_access_range auto&& range, auto nthIterator) {
570 return std::nth_element(std::begin(range), std::move(nthIterator), std::end(range));
571}
572
573constexpr auto nthElement(random_access_range auto&& range, auto nthIterator, auto compare) {
574 return std::nth_element(std::begin(range), std::move(nthIterator), std::end(range), std::move(compare));
575}
576
577constexpr auto unique(forward_range auto&& range) {
578 return std::unique(std::begin(range), std::end(range));
579}
580
581constexpr auto unique(forward_range auto&& range, auto equal) {
582 return std::unique(std::begin(range), std::end(range), std::move(equal));
583}
584
585constexpr auto uniqueCopy(input_range auto&& range, auto outputIterator) {
586 return std::unique_copy(std::begin(range), std::end(range), std::move(outputIterator));
587}
588
589constexpr auto uniqueCopy(input_range auto&& range, auto outputIterator, auto equal) {
590 return std::unique_copy(std::begin(range), std::end(range), std::move(outputIterator), std::move(equal));
591}
592
593constexpr auto remove(forward_range auto&& range, const auto& value) {
594 return std::remove(std::begin(range), std::end(range), value);
595}
596
597constexpr auto removeIf(forward_range auto&& range, auto predicate) {
598 return std::remove_if(std::begin(range), std::end(range), std::move(predicate));
599}
600
601constexpr auto removeCopy(input_range auto&& range, auto outputIterator, const auto& value) {
602 return std::remove_copy(std::begin(range), std::end(range), std::move(outputIterator), value);
603}
604
605constexpr auto removeCopyIf(input_range auto&& range, auto outputIterator, auto predicate) {
606 return std::remove_copy_if(std::begin(range), std::end(range), std::move(outputIterator), std::move(predicate));
607}
608
609constexpr auto replace(forward_range auto&& range, const auto& oldValue, const auto& newValue) {
610 return std::replace(std::begin(range), std::end(range), oldValue, newValue);
611}
612
613constexpr auto replaceIf(forward_range auto&& range, auto predicate, const auto& newValue) {
614 return std::replace_if(std::begin(range), std::end(range), std::move(predicate), newValue);
615}
616
617constexpr auto replaceCopy(input_range auto&& range, auto outputIterator, const auto& oldValue, const auto& newValue) {
618 return std::replace_copy(std::begin(range), std::end(range), std::move(outputIterator), oldValue, newValue);
619}
620
621constexpr auto replaceCopyIf(input_range auto&& range, auto outputIterator, auto predicate, const auto& newValue) {
622 return std::replace_copy_if(std::begin(range), std::end(range), std::move(outputIterator), std::move(predicate), newValue);
623}
624
625inline void shuffle(random_access_range auto&& range, auto&& uniformRandomBitGenerator) {
626 std::shuffle(std::begin(range), std::end(range), std::forward<decltype(uniformRandomBitGenerator)>(uniformRandomBitGenerator));
627}
628
629inline auto sample(forward_range auto&& range, auto outputIterator, auto n, auto&& uniformRandomBitGenerator) {
630 return std::sample(std::begin(range), std::end(range), std::move(outputIterator), std::move(n), std::forward<decltype(uniformRandomBitGenerator)>(uniformRandomBitGenerator));
631}
632
633constexpr void reverse(bidirectional_range auto&& range) {
634 std::reverse(std::begin(range), std::end(range));
635}
636
637constexpr auto reverseCopy(bidirectional_range auto&& range, auto outputIterator) {
638 return std::reverse_copy(std::begin(range), std::end(range), std::move(outputIterator));
639}
640
641constexpr auto rotate(forward_range auto&& range, auto middle) {
642 return std::rotate(std::begin(range), std::move(middle), std::end(range));
643}
644
645constexpr auto rotateCopy(forward_range auto&& range, auto middle, auto outputIterator) {
646 return std::rotate_copy(std::begin(range), std::move(middle), std::end(range), std::move(outputIterator));
647}
648
649constexpr auto shiftLeft(forward_range auto&& range, auto n) {
650 return std::shift_left(std::begin(range), std::end(range), n);
651}
652
653constexpr auto shiftRight(forward_range auto&& range, auto n) {
654 return std::shift_right(std::begin(range), std::end(range), n);
655}
656
657constexpr auto copy(input_range auto&& range, auto outputIterator) {
658 return std::copy(std::begin(range), std::end(range), std::move(outputIterator));
659}
660
661constexpr auto copyIf(input_range auto&& range, auto outputIterator, auto predicate) {
662 return std::copy_if(std::begin(range), std::end(range), std::move(outputIterator), std::move(predicate));
663}
664
665constexpr auto copyBackward(bidirectional_range auto&& range, auto outputEnd) {
666 return std::copy_backward(std::begin(range), std::end(range), std::move(outputEnd));
667}
668
669constexpr auto move(input_range auto&& range, auto outputIterator) {
670 return std::move(std::begin(range), std::end(range), std::move(outputIterator));
671}
672
673constexpr auto moveBackward(bidirectional_range auto&& range, auto outputEnd) {
674 return std::move_backward(std::begin(range), std::end(range), std::move(outputEnd));
675}
676
677constexpr void fill(forward_range auto&& range, const auto& value) {
678 std::fill(std::begin(range), std::end(range), value);
679}
680
681constexpr void iota(forward_range auto&& range, auto firstValue) {
682 std::iota(std::begin(range), std::end(range), std::move(firstValue));
683}
684
685constexpr void generate(forward_range auto&& range, auto generate) {
686 std::generate(std::begin(range), std::end(range), std::move(generate));
687}
688
689} // namespace grem
690
691#endif
Definition algorithms.hpp:132
void is_transparent
Definition algorithms.hpp:134
constexpr size_t operator()(const T &a, const T &b) const
Definition algorithms.hpp:136
Definition algorithms.hpp:119
constexpr size_t operator()(const T &value) const
Definition algorithms.hpp:123
void is_transparent
Definition algorithms.hpp:121
Definition concepts.hpp:258
Definition concepts.hpp:47
Definition concepts.hpp:255
Definition concepts.hpp:249
Definition concepts.hpp:261
Definition concepts.hpp:243
Definition aliases.hpp:9
constexpr auto reverseCopy(bidirectional_range auto &&range, auto outputIterator)
Definition algorithms.hpp:637
constexpr auto findBy(input_range auto &&range, const auto &value)
Definition algorithms.hpp:247
constexpr void sortByDescending(random_access_range auto &&range)
Definition algorithms.hpp:519
constexpr bool anyOf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:222
constexpr auto partition(forward_range auto &&range, auto predicate)
Definition algorithms.hpp:557
constexpr Add ADD
Definition algorithms.hpp:76
constexpr bool lexicographicalCompare(input_range auto &&rangeA, input_range auto &&rangeB)
Definition algorithms.hpp:366
constexpr auto find(input_range auto &&range, const auto &value)
Definition algorithms.hpp:242
constexpr auto shiftRight(forward_range auto &&range, auto n)
Definition algorithms.hpp:653
constexpr auto unique(forward_range auto &&range)
Definition algorithms.hpp:577
constexpr bool endsWith(input_range auto &&range, input_range auto &&sequence)
Definition algorithms.hpp:345
constexpr Subtract SUBTRACT
Definition algorithms.hpp:84
constexpr auto removeCopyIf(input_range auto &&range, auto outputIterator, auto predicate)
Definition algorithms.hpp:605
constexpr bool binarySearch(forward_range auto &&sortedRange, const auto &value)
Definition algorithms.hpp:418
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234
constexpr Negate NEGATE
Definition algorithms.hpp:116
constexpr void stableSort(random_access_range auto &&range)
Definition algorithms.hpp:523
constexpr auto replaceCopy(input_range auto &&range, auto outputIterator, const auto &oldValue, const auto &newValue)
Definition algorithms.hpp:617
constexpr auto minmaxElement(forward_range auto &&range)
Definition algorithms.hpp:390
constexpr auto lowerBound(forward_range auto &&sortedRange, const auto &value, auto compare)
Definition algorithms.hpp:426
constexpr auto inplaceMerge(bidirectional_range auto &&sortedRange, auto middleIterator)
Definition algorithms.hpp:497
constexpr auto moveBackward(bidirectional_range auto &&range, auto outputEnd)
Definition algorithms.hpp:673
constexpr auto copyBackward(bidirectional_range auto &&range, auto outputEnd)
Definition algorithms.hpp:665
constexpr auto copyIf(input_range auto &&range, auto outputIterator, auto predicate)
Definition algorithms.hpp:661
constexpr auto equalRange(forward_range auto &&sortedRange, const auto &value, auto compare)
Definition algorithms.hpp:465
constexpr void sortByAscending(random_access_range auto &&range)
Definition algorithms.hpp:514
constexpr LessThan LESS_THAN
Definition algorithms.hpp:44
constexpr Divide DIVIDE
Definition algorithms.hpp:100
constexpr bool startsWith(input_range auto &&range, input_range auto &&sequence)
Definition algorithms.hpp:335
constexpr auto removeCopy(input_range auto &&range, auto outputIterator, const auto &value)
Definition algorithms.hpp:601
constexpr bool noneOf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:230
constexpr void partialSort(random_access_range auto &&range, auto middleIterator)
Definition algorithms.hpp:541
constexpr GreaterThan GREATER_THAN
Definition algorithms.hpp:60
constexpr std::size_t combineHashes(std::size_t hash, convertible_to< std::size_t > auto... otherHashes)
Definition algorithms.hpp:141
constexpr auto containsBy(input_range auto &&range, const auto &value)
Definition algorithms.hpp:305
constexpr bool isSorted(forward_range auto &&range)
Definition algorithms.hpp:398
constexpr auto partialSortCopy(input_range auto &&range, random_access_range auto &&outputRange)
Definition algorithms.hpp:549
constexpr bool containsSubrange(forward_range auto &&range, forward_range auto &&sequence)
Definition algorithms.hpp:315
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
constexpr auto rotate(forward_range auto &&range, auto middle)
Definition algorithms.hpp:641
constexpr void sort(random_access_range auto &&range)
Definition algorithms.hpp:505
constexpr auto removeIf(forward_range auto &&range, auto predicate)
Definition algorithms.hpp:597
constexpr GreaterThanOrEqualTo GREATER_THAN_OR_EQUAL_TO
Definition algorithms.hpp:68
constexpr void forEach(input_range auto &&range, auto operation)
Definition algorithms.hpp:181
constexpr auto findIf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:251
constexpr auto accumulate(input_range auto &&range, auto initialValue)
Definition algorithms.hpp:202
constexpr auto search(forward_range auto &&range, forward_range auto &&sequence)
Definition algorithms.hpp:275
constexpr auto stablePartition(bidirectional_range auto &&range, auto predicate)
Definition algorithms.hpp:561
constexpr auto transform(input_range auto &&range, auto outputIterator, auto transformationFunction)
Definition algorithms.hpp:185
constexpr auto maxElement(forward_range auto &&range)
Definition algorithms.hpp:382
constexpr auto upperBound(forward_range auto &&sortedRange, const auto &value)
Definition algorithms.hpp:457
constexpr auto replace(forward_range auto &&range, const auto &oldValue, const auto &newValue)
Definition algorithms.hpp:609
constexpr auto remove(forward_range auto &&range, const auto &value)
Definition algorithms.hpp:593
constexpr auto nthElement(random_access_range auto &&range, auto nthIterator)
Definition algorithms.hpp:569
constexpr bool equal(input_range auto &&rangeA, input_range auto &&rangeB)
Definition algorithms.hpp:358
constexpr auto reduce(input_range auto &&range)
Definition algorithms.hpp:210
constexpr auto partitionPoint(forward_range auto &&partitionedRange, auto compare)
Definition algorithms.hpp:485
constexpr void stableSortByDescending(random_access_range auto &&range)
Definition algorithms.hpp:537
constexpr void generate(forward_range auto &&range, auto generate)
Definition algorithms.hpp:685
constexpr auto replaceIf(forward_range auto &&range, auto predicate, const auto &newValue)
Definition algorithms.hpp:613
constexpr void fill(forward_range auto &&range, const auto &value)
Definition algorithms.hpp:677
constexpr auto partitionCopy(input_range auto &&range, auto trueOutputIterator, auto falseOutputIterator, auto predicate)
Definition algorithms.hpp:565
constexpr auto replaceCopyIf(input_range auto &&range, auto outputIterator, auto predicate, const auto &newValue)
Definition algorithms.hpp:621
constexpr auto merge(input_range auto &&sortedRangeA, input_range auto &&sortedRangeB, auto outputIterator)
Definition algorithms.hpp:489
constexpr bool contains(input_range auto &&range, const auto &value)
Definition algorithms.hpp:299
constexpr void stableSortByAscending(random_access_range auto &&range)
Definition algorithms.hpp:532
auto sample(forward_range auto &&range, auto outputIterator, auto n, auto &&uniformRandomBitGenerator)
Definition algorithms.hpp:629
constexpr auto findEnd(forward_range auto &&range, forward_range auto &&values)
Definition algorithms.hpp:267
constexpr NotEqualTo NOT_EQUAL_TO
Definition algorithms.hpp:36
constexpr std::size_t getRangeHash(input_range auto &&range)
Definition algorithms.hpp:151
constexpr auto countIf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:238
constexpr auto mismatch(input_range auto &&range, input_range auto &&sequence)
Definition algorithms.hpp:283
constexpr void reverse(bidirectional_range auto &&range)
Definition algorithms.hpp:633
constexpr EqualTo EQUAL_TO
Definition algorithms.hpp:28
constexpr Multiply MULTIPLY
Definition algorithms.hpp:92
constexpr auto minElement(forward_range auto &&range)
Definition algorithms.hpp:374
constexpr auto shiftLeft(forward_range auto &&range, auto n)
Definition algorithms.hpp:649
constexpr auto uniqueCopy(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:585
constexpr auto findFirstOf(input_range auto &&range, forward_range auto &&values)
Definition algorithms.hpp:259
constexpr Modulo MODULO
Definition algorithms.hpp:108
constexpr std::size_t getHash(const T &value, const Ts &... otherValues)
Definition algorithms.hpp:147
constexpr bool includes(input_range auto &&sortedRange, input_range auto &&sortedSequence)
Definition algorithms.hpp:410
constexpr bool isPartitioned(input_range auto &&range, auto compare)
Definition algorithms.hpp:406
constexpr auto copy(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:657
constexpr auto adjacentFind(forward_range auto &&range)
Definition algorithms.hpp:291
void shuffle(random_access_range auto &&range, auto &&uniformRandomBitGenerator)
Definition algorithms.hpp:625
constexpr void iota(forward_range auto &&range, auto firstValue)
Definition algorithms.hpp:681
constexpr void swapRanges(input_range auto &&rangeA, input_range auto &&rangeB)
Definition algorithms.hpp:169
constexpr void iterSwap(auto itA, auto itB)
Definition algorithms.hpp:164
constexpr bool allOf(input_range auto &&range, auto predicate)
Definition algorithms.hpp:226
constexpr auto rotateCopy(forward_range auto &&range, auto middle, auto outputIterator)
Definition algorithms.hpp:645
constexpr auto findIfNot(input_range auto &&range, auto predicate)
Definition algorithms.hpp:255
constexpr LessThanOrEqualTo LESS_THAN_OR_EQUAL_TO
Definition algorithms.hpp:52
Definition algorithms.hpp:70
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a)+std::forward< B >(b))
Definition algorithms.hpp:72
Definition algorithms.hpp:94
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a)/std::forward< B >(b))
Definition algorithms.hpp:96
Definition algorithms.hpp:22
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a)==std::forward< B >(b))
Definition algorithms.hpp:24
Definition algorithms.hpp:62
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a) >=std::forward< B >(b))
Definition algorithms.hpp:64
Definition algorithms.hpp:54
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a) > std::forward< B >(b))
Definition algorithms.hpp:56
Definition algorithms.hpp:46
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a)<=std::forward< B >(b))
Definition algorithms.hpp:48
Definition algorithms.hpp:38
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a)< std::forward< B >(b))
Definition algorithms.hpp:40
Definition algorithms.hpp:102
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a) % std::forward< B >(b))
Definition algorithms.hpp:104
Definition algorithms.hpp:86
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a) *std::forward< B >(b))
Definition algorithms.hpp:88
Definition algorithms.hpp:110
constexpr auto operator()(A &&a) const -> decltype(-std::forward< A >(a))
Definition algorithms.hpp:112
Definition algorithms.hpp:30
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a) !=std::forward< B >(b))
Definition algorithms.hpp:32
Definition Pair.hpp:53
Definition algorithms.hpp:78
constexpr auto operator()(A &&a, B &&b) const -> decltype(std::forward< A >(a) - std::forward< B >(b))
Definition algorithms.hpp:80