9template <
typename Iterator>
10void reverse(Iterator first, Iterator last) {
11 while ((first != last) && (first != --last)) {
12 swap(*first++, *last);
16template <
typename Iterator>
22 Iterator max_iter = first;
25 while (first != last) {
26 if (*max_iter < *first) {
35template <
typename Iterator,
typename Compare>
36Iterator
max_element(Iterator first, Iterator last, Compare comp) {
41 Iterator max_iter = first;
44 while (first != last) {
45 if (comp(*max_iter, *first)) {
54template <
typename Iterator>
60 Iterator min_iter = first;
63 while (first != last) {
64 if (*first < *min_iter) {
73template <
typename Iterator,
typename Compare>
74Iterator
min_element(Iterator first, Iterator last, Compare comp) {
79 Iterator min_iter = first;
82 while (first != last) {
83 if (comp(*first, *min_iter)) {
94template <
typename Iterator1,
typename Iterator2>
95bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2) {
96 while (first1 != last1) {
97 if (*first1 != *first2) {
106template <
typename Iterator1,
typename Iterator2,
typename BinaryPredicate>
107bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryPredicate pred) {
108 while (first1 != last1) {
109 if (!pred(*first1, *first2)) {
118template <
typename Iterator1,
typename Iterator2>
119bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) {
120 while (first1 != last1 && first2 != last2) {
121 if (*first1 != *first2) {
127 return first1 == last1 && first2 == last2;
130template <
typename Iterator1,
typename Iterator2,
typename BinaryPredicate>
131bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, BinaryPredicate pred) {
132 while (first1 != last1 && first2 != last2) {
133 if (!pred(*first1, *first2)) {
139 return first1 == last1 && first2 == last2;
142template <
typename Container1,
typename Container2>
144 fl::size size1 = c1.size();
145 fl::size size2 = c2.size();
146 if (size1 != size2) {
149 return equal(c1.begin(), c1.end(), c2.begin(), c2.end());
152template <
typename Container1,
typename Container2,
typename BinaryPredicate>
153bool equal_container(
const Container1& c1,
const Container2& c2, BinaryPredicate pred) {
154 fl::size size1 = c1.size();
155 fl::size size2 = c2.size();
156 if (size1 != size2) {
159 return equal(c1.begin(), c1.end(), c2.begin(), c2.end(), pred);
163template <
typename Iterator,
typename T>
164void fill(Iterator first, Iterator last,
const T& value) {
165 while (first != last) {
171template <
typename Iterator,
typename T>
172Iterator
find(Iterator first, Iterator last,
const T& value) {
173 while (first != last) {
174 if (*first == value) {
182template <
typename Iterator,
typename UnaryPredicate>
183Iterator
find_if(Iterator first, Iterator last, UnaryPredicate pred) {
184 while (first != last) {
193template <
typename Iterator,
typename UnaryPredicate>
194Iterator
find_if_not(Iterator first, Iterator last, UnaryPredicate pred) {
195 while (first != last) {
204template <
typename Iterator,
typename T>
205Iterator
remove(Iterator first, Iterator last,
const T& value) {
207 while (first != last) {
208 if (!(*first == value)) {
219template <
typename Iterator,
typename UnaryPredicate>
220Iterator
remove_if(Iterator first, Iterator last, UnaryPredicate pred) {
222 while (first != last) {
237template <
typename Iterator,
typename Compare>
239 if (first == last)
return;
241 for (Iterator i = first + 1; i != last; ++i) {
245 while (j != first && comp(value, *(j - 1))) {
255template <
typename Iterator,
typename Compare>
256Iterator
median_of_three(Iterator first, Iterator middle, Iterator last, Compare comp) {
257 if (comp(*middle, *first)) {
258 if (comp(*last, *middle)) {
260 }
else if (comp(*last, *first)) {
266 if (comp(*last, *first)) {
268 }
else if (comp(*last, *middle)) {
277template <
typename Iterator,
typename Compare>
278Iterator
partition(Iterator first, Iterator last, Compare comp) {
279 Iterator middle = first + (last - first) / 2;
283 swap(*pivot_iter, *(last - 1));
284 Iterator pivot = last - 1;
287 for (Iterator j = first; j != pivot; ++j) {
288 if (comp(*j, *pivot)) {
299template <
typename Iterator,
typename Compare>
300void sift_down(Iterator first, Iterator start, Iterator
end, Compare comp) {
301 Iterator root = start;
303 while (root - first <= (
end - first - 2) / 2) {
304 Iterator child = first + 2 * (root - first) + 1;
305 Iterator swap_iter = root;
307 if (comp(*swap_iter, *child)) {
311 if (child + 1 <=
end && comp(*swap_iter, *(child + 1))) {
312 swap_iter = child + 1;
315 if (swap_iter == root) {
318 swap(*root, *swap_iter);
324template <
typename Iterator,
typename Compare>
325void heapify(Iterator first, Iterator last, Compare comp) {
326 Iterator start = first + (last - first - 2) / 2;
330 if (start == first) {
337template <
typename Iterator,
typename Compare>
338void heap_sort(Iterator first, Iterator last, Compare comp) {
341 Iterator
end = last - 1;
342 while (
end > first) {
350template <
typename Iterator,
typename Compare>
352 if (last - first <= 16) {
357 Iterator pivot =
partition(first, last, comp);
363template <
typename Iterator>
365 if (first == middle || middle == last) {
369 Iterator next = middle;
370 while (first != next) {
371 swap(*first++, *next++);
374 }
else if (first == middle) {
381template <
typename Iterator,
typename T,
typename Compare>
383 auto count = last - first;
385 auto step = count / 2;
386 Iterator it = first + step;
387 if (comp(*it, value)) {
398template <
typename Iterator,
typename Compare>
399void merge_inplace(Iterator first, Iterator middle, Iterator last, Compare comp) {
401 if (first == middle || middle == last) {
406 auto left_size = middle - first;
407 auto right_size = last - middle;
408 if (left_size + right_size <= 32) {
410 Iterator left = first;
411 Iterator right = middle;
413 while (left < middle && right < last) {
414 if (!comp(*right, *left)) {
420 Iterator shift_end = right;
421 Iterator shift_start = left;
424 while (shift_end > shift_start) {
425 *shift_end =
fl::move(*(shift_end - 1));
439 if (left_size == 0 || right_size == 0) {
443 if (left_size == 1) {
450 if (right_size == 1) {
458 Iterator left_mid = first + left_size / 2;
465 Iterator new_middle = left_mid + (right_mid - middle);
473template <
typename Iterator,
typename Compare>
475 auto size = last - first;
481 Iterator middle = first + size / 2;
490template <
typename Iterator,
typename Compare>
491void sort(Iterator first, Iterator last, Compare comp) {
492 if (first == last || first + 1 == last) {
500template <
typename Iterator>
501void sort(Iterator first, Iterator last) {
504 sort(first, last, [](
const value_type& a,
const value_type& b) {
return a < b; });
508template <
typename Iterator,
typename Compare>
510 if (first == last || first + 1 == last) {
518template <
typename Iterator>
522 stable_sort(first, last, [](
const value_type& a,
const value_type& b) {
return a < b; });
526template <
typename Iterator,
typename RandomGenerator>
527void shuffle(Iterator first, Iterator last, RandomGenerator& g) {
532 auto n = last - first;
533 for (
auto i = n - 1; i > 0; --i) {
535 auto j = g() % (i + 1);
538 swap(*(first + i), *(first + j));
543template <
typename Iterator>
549 auto n = last - first;
550 for (
auto i = n - 1; i > 0; --i) {
552 auto j = rng(
static_cast<u32
>(i + 1));
555 swap(*(first + i), *(first + j));
560template <
typename Iterator>
A random number generator class that wraps FastLED's random functions.
Result type for promise operations.
Iterator median_of_three(Iterator first, Iterator middle, Iterator last, Compare comp)
void merge_inplace(Iterator first, Iterator middle, Iterator last, Compare comp)
void sift_down(Iterator first, Iterator start, Iterator end, Compare comp)
void heap_sort(Iterator first, Iterator last, Compare comp)
Iterator partition(Iterator first, Iterator last, Compare comp)
void heapify(Iterator first, Iterator last, Compare comp)
void rotate_impl(Iterator first, Iterator middle, Iterator last)
void insertion_sort(Iterator first, Iterator last, Compare comp)
void quicksort_impl(Iterator first, Iterator last, Compare comp)
Iterator lower_bound_impl(Iterator first, Iterator last, const T &value, Compare comp)
void mergesort_impl(Iterator first, Iterator last, Compare comp)
constexpr remove_reference< T >::type && move(T &&t) noexcept
Iterator find(Iterator first, Iterator last, const T &value)
void swap(array< T, N > &lhs, array< T, N > &rhs) noexcept(noexcept(lhs.swap(rhs)))
void shuffle(Iterator first, Iterator last, RandomGenerator &g)
Iterator min_element(Iterator first, Iterator last)
constexpr T * end(T(&array)[N]) noexcept
fl_random & default_random()
Global default random number generator instance.
Iterator remove(Iterator first, Iterator last, const T &value)
Iterator find_if_not(Iterator first, Iterator last, UnaryPredicate pred)
void reverse(Iterator first, Iterator last)
Iterator find_if(Iterator first, Iterator last, UnaryPredicate pred)
Iterator max_element(Iterator first, Iterator last)
void sort(Iterator first, Iterator last, Compare comp)
void stable_sort(Iterator first, Iterator last, Compare comp)
bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2)
bool equal_container(const Container1 &c1, const Container2 &c2)
void fill(Iterator first, Iterator last, const T &value)
Iterator remove_if(Iterator first, Iterator last, UnaryPredicate pred)