FastLED 3.9.15
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/type_traits.h"
4
5namespace fl {
6
7template <typename Iterator>
8void reverse(Iterator first, Iterator last) {
9 while ((first != last) && (first != --last)) {
10 swap(*first++, *last);
11 }
12}
13
14template <typename Iterator>
15Iterator max_element(Iterator first, Iterator last) {
16 if (first == last) {
17 return last;
18 }
19
20 Iterator max_iter = first;
21 ++first;
22
23 while (first != last) {
24 if (*max_iter < *first) {
25 max_iter = first;
26 }
27 ++first;
28 }
29
30 return max_iter;
31}
32
33template <typename Iterator, typename Compare>
34Iterator max_element(Iterator first, Iterator last, Compare comp) {
35 if (first == last) {
36 return last;
37 }
38
39 Iterator max_iter = first;
40 ++first;
41
42 while (first != last) {
43 if (comp(*max_iter, *first)) {
44 max_iter = first;
45 }
46 ++first;
47 }
48
49 return max_iter;
50}
51
52template <typename Iterator>
53Iterator min_element(Iterator first, Iterator last) {
54 if (first == last) {
55 return last;
56 }
57
58 Iterator min_iter = first;
59 ++first;
60
61 while (first != last) {
62 if (*first < *min_iter) {
63 min_iter = first;
64 }
65 ++first;
66 }
67
68 return min_iter;
69}
70
71template <typename Iterator, typename Compare>
72Iterator min_element(Iterator first, Iterator last, Compare comp) {
73 if (first == last) {
74 return last;
75 }
76
77 Iterator min_iter = first;
78 ++first;
79
80 while (first != last) {
81 if (comp(*first, *min_iter)) {
82 min_iter = first;
83 }
84 ++first;
85 }
86
87 return min_iter;
88}
89
90
91
92template <typename Iterator1, typename Iterator2>
93bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2) {
94 while (first1 != last1) {
95 if (*first1 != *first2) {
96 return false;
97 }
98 ++first1;
99 ++first2;
100 }
101 return true;
102}
103
104template <typename Iterator1, typename Iterator2, typename BinaryPredicate>
105bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryPredicate pred) {
106 while (first1 != last1) {
107 if (!pred(*first1, *first2)) {
108 return false;
109 }
110 ++first1;
111 ++first2;
112 }
113 return true;
114}
115
116template <typename Iterator1, typename Iterator2>
117bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) {
118 while (first1 != last1 && first2 != last2) {
119 if (*first1 != *first2) {
120 return false;
121 }
122 ++first1;
123 ++first2;
124 }
125 return first1 == last1 && first2 == last2;
126}
127
128template <typename Iterator1, typename Iterator2, typename BinaryPredicate>
129bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, BinaryPredicate pred) {
130 while (first1 != last1 && first2 != last2) {
131 if (!pred(*first1, *first2)) {
132 return false;
133 }
134 ++first1;
135 ++first2;
136 }
137 return first1 == last1 && first2 == last2;
138}
139
140template <typename Container1, typename Container2>
141bool equal_container(const Container1& c1, const Container2& c2) {
142 size_t size1 = c1.size();
143 size_t size2 = c2.size();
144 if (size1 != size2) {
145 return false;
146 }
147 return equal(c1.begin(), c1.end(), c2.begin(), c2.end());
148}
149
150template <typename Container1, typename Container2, typename BinaryPredicate>
151bool equal_container(const Container1& c1, const Container2& c2, BinaryPredicate pred) {
152 size_t size1 = c1.size();
153 size_t size2 = c2.size();
154 if (size1 != size2) {
155 return false;
156 }
157 return equal(c1.begin(), c1.end(), c2.begin(), c2.end(), pred);
158}
159
160
161template <typename Iterator, typename T>
162void fill(Iterator first, Iterator last, const T& value) {
163 while (first != last) {
164 *first = value;
165 ++first;
166 }
167}
168
169} // namespace fl
void swap(array< T, N > &lhs, array< T, N > &rhs) noexcept(noexcept(lhs.swap(rhs)))
Definition array.h:140
Iterator min_element(Iterator first, Iterator last)
Definition algorithm.h:53
void reverse(Iterator first, Iterator last)
Definition algorithm.h:8
Iterator max_element(Iterator first, Iterator last)
Definition algorithm.h:15
bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2)
Definition algorithm.h:93
bool equal_container(const Container1 &c1, const Container2 &c2)
Definition algorithm.h:141
void fill(Iterator first, Iterator last, const T &value)
Definition algorithm.h:162
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16