10 const auto equal = [](
auto a,
auto b) {
11 return std::equal(std::begin(
a), std::end(
a), std::begin(b), std::end(b));
13 SECTION(
"erase and erase_if") {
14 const std::vector<unsigned> erase_test{2, 2, 3, 4, 5, 7, 8, 11};
15 const std::vector<unsigned> no_2s = {3, 4, 5, 7, 8, 11};
16 auto erase_copy = erase_test;
18 CHECK(
equal(no_2s, erase_copy));
19 erase_copy = erase_test;
20 const std::vector<unsigned> no_evens = {3, 5, 7, 11};
22 CHECK(
equal(no_evens, erase_copy));
28 int vec[] = {0, 3, 4, 5, 6, 1, 2, 4, 4, 5};
29 auto begin = std::begin(vec);
30 auto end = std::end(vec);
40 CHECK(
kblib::find_if(begin, end, [](
int i) {
return i == 2; }) == begin + 6);
42 CHECK(
kblib::find_if(begin, end, [](
int) {
return false; }) == end);
64 int none[] = {7, 8, -1};
69 int vec[] = {0, 3, 4, 5, 6, 1, 2, 4, 4, 5};
70 auto begin = std::begin(vec);
71 auto end = std::end(vec);
96 using namespace std::literals;
99 SECTION(
"search_replace_copy_1") {
100 const auto haystack =
"a string with words"s;
101 const auto needle =
"with"s;
102 const auto replace =
"comprising"s;
105 end(needle), begin(replace), end(replace),
106 std::back_inserter(result));
107 CHECK(result ==
"a string comprising words");
109 SECTION(
"search_replace_copy_2(no match)") {
110 const auto haystack =
"a string with words"s;
111 const auto needle =
"without"s;
112 const auto replace =
"comprising"s;
115 end(needle), begin(replace), end(replace),
116 std::back_inserter(result));
117 CHECK(result == haystack);
119 SECTION(
"search_replace_copy_3(multiple matches)") {
120 const auto haystack =
"a string with multiple 'with's"s;
121 const auto needle =
"with"s;
122 const auto replace =
"containing"s;
125 end(needle), begin(replace), end(replace),
126 std::back_inserter(result));
127 CHECK(result ==
"a string containing multiple 'containing's");
129 SECTION(
"search_replace_copy_4(empty match)") {
130 const auto haystack =
"a string with words"s;
131 const auto needle =
""s;
132 const auto replace =
"comprising"s;
135 end(needle), begin(replace), end(replace),
136 std::back_inserter(result));
137 CHECK(result == haystack);
139 SECTION(
"search_replace_copy_5(match at beginning)") {
140 const auto haystack =
"a string with words"s;
141 const auto needle =
"a"s;
142 const auto replace =
"another"s;
145 end(needle), begin(replace), end(replace),
146 std::back_inserter(result));
147 CHECK(result ==
"another string with words");
149 SECTION(
"search_replace_copy_6(match at end)") {
150 const auto haystack =
"a string with words"s;
151 const auto needle =
"words"s;
152 const auto replace =
"letters"s;
155 end(needle), begin(replace), end(replace),
156 std::back_inserter(result));
157 CHECK(result ==
"a string with letters");
159 SECTION(
"search_replace_copy_7(only matches)") {
160 const auto haystack =
"abababababab"s;
161 const auto needle =
"ab"s;
162 const auto replace =
"ba"s;
165 end(needle), begin(replace), end(replace),
166 std::back_inserter(result));
167 CHECK(result ==
"babababababa");
169 SECTION(
"search_replace_copy_8(match is whole string)") {
170 const auto haystack =
"string"s;
171 const auto& needle = haystack;
172 const auto replace =
"replace"s;
175 end(needle), begin(replace), end(replace),
176 std::back_inserter(result));
177 CHECK(result == replace);
179 SECTION(
"search_replace_copy_9(overlap)") {
180 const auto haystack =
"abcabcab"s;
181 const auto needle =
"abcab"s;
182 const auto replace =
"a"s;
185 end(needle), begin(replace), end(replace),
186 std::back_inserter(result));
187 CHECK(result ==
"acab");
193 std::array<int, 11> arr{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
196 = kblib::get_max_n_old<std::vector<int>>(arr.begin(), arr.end(), 2);
197 CHECK(max_two.size() == 2);
198 CHECK(
std::max(max_two[0], max_two[1]) == 10);
199 CHECK(
std::min(max_two[0], max_two[1]) == 9);
203 = kblib::get_max_n_old<std::set<int>>(arr.begin(), arr.end(), 2);
204 CHECK(max_two.size() == 2);
205 CHECK(max_two.find(10) != max_two.end());
206 CHECK(max_two.find(9) != max_two.end());
210 = kblib::get_max_n<std::vector<int>>(arr.begin(), arr.end(), 2);
211 CHECK(max_two.size() == 2);
212 CHECK(
std::max(max_two[0], max_two[1]) == 10);
213 CHECK(
std::min(max_two[0], max_two[1]) == 9);
216 auto max_two = kblib::get_max_n<std::set<int>>(arr.begin(), arr.end(), 2);
217 CHECK(max_two.size() == 2);
218 CHECK(max_two.find(10) != max_two.end());
219 CHECK(max_two.find(9) != max_two.end());
228 auto equal = [](
auto r1,
auto r2) {
229 auto r1b = r1.begin();
231 auto r2b = r2.begin();
233 return (std::distance(r1b, r1e) == std::distance(r2b, r2e))
236 std::array<int, 10> haystack{1, 1, 2, 5, 6, 2, 4, 7, 0, 6};
238 std::array<int, N> maxN_target{7, 6, 6, 5, 4};
241 haystack.begin(), haystack.end(), N)));
242 std::vector<int> maxN_copy;
244 std::back_inserter(maxN_copy), 5);
245 CHECK(
equal(maxN_target, maxN_copy));
249 haystack.begin(), haystack.end(), N)));
251 std::vector<int> maxN_psc(N);
252 std::partial_sort_copy(haystack.begin(), haystack.end(), maxN_psc.begin(),
253 maxN_psc.end(), std::greater<>{});
254 CHECK(
equal(maxN_target, maxN_psc));
265 CHECK(
equal(
range, std::vector<int>{0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30,
266 33, 36, 39, 42, 45, 48}));
268 = kblib::get_max_n<std::vector<int>>(
range.begin(),
range.end(), 5);
269 CHECK(maxN_range == std::vector<int>{48, 45, 42, 39, 36});
273 SECTION(
"non-overlapping") {
274 std::vector<int> input1{1, 2, 3, 4, 5, 6};
275 std::vector<short> input2{2, 3, 4, 5, 6, 7};
277 for (
auto t :
kblib::zip(input1.begin(), input1.end(), input2.begin())) {
278 auto&
a = std::get<0>(t);
279 auto& b = std::get<1>(t);
283 SECTION(
"identical") {
284 std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8};
285 for (
auto t :
kblib::zip(input.begin(), input.end(), input.begin())) {
286 auto&
a = std::get<0>(t);
287 auto& b = std::get<1>(t);
291 SECTION(
"overlapping") {
292 std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8};
294 kblib::zip(input.begin(), input.end() - 1, input.begin() + 1)) {
295 auto&
a = std::get<0>(t);
296 auto& b = std::get<1>(t);
Provides general-purpose algorithms, similar to the <algorithms> header.
constexpr auto size(const C &c) -> decltype(c.size())
constexpr struct kblib::nums::min_t min
constexpr struct kblib::nums::max_t max
constexpr auto contains(InputIt begin, InputIt end, const Value &val) noexcept(noexcept(*begin==val)) -> enable_if_t< is_input_iterator< InputIt >::value, bool >
Determine if a range contains a value.
auto zip(InputIt1 begin1, EndIt end1, InputIt2 begin2) noexcept(zip_iterator< InputIt1, EndIt, InputIt2 >::is_nothrow_copyable) -> zip_iterator< InputIt1, EndIt, InputIt2 >
Iterate over two ranges in lockstep, like Python's zip.
constexpr auto find_in_if_not(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> size_t
Find the offset of the first element for which p returns false. It also allows for a sentinel end ite...
constexpr auto find_last_if_not(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> ForwardIt
Searches a range for the last element on which a predicate returns false. It also allows for a sentin...
constexpr auto a(const std::initializer_list< T > &a) -> auto
Index an array literal without naming its type.
constexpr auto find_last_in_if(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> size_t
constexpr auto find_if(ForwardIt begin, EndIt end, UnaryPredicate &&pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> ForwardIt
Finds the first value in range [begin, end) for which pred returns true. If not found,...
constexpr auto find_in(ForwardIt begin, EndIt end, const Elem &value) noexcept(noexcept(*begin==value)) -> size_t
Find the offset of the first ocurrence of v in a range from the beginning. It also allows for a senti...
constexpr auto range(Value min, Value max, Delta step=0) -> range_t< Value, Delta >
Constructs a range from beginning, end, and step amount. The range is half-open, that is min is in th...
constexpr auto get_max_n(It first, It last, std::size_t count, Comp cmp={}) -> SequenceContainer
Returns a container of the greatest count elements according to cmp of the range [first,...
constexpr auto contains_any(InputIt1 begin, InputIt1 end, InputIt2 n_begin, InputIt2 n_end) -> enable_if_t< is_input_iterator< InputIt1 >::value and is_input_iterator< InputIt2 >::value, bool >
constexpr auto find_last(ForwardIt begin, EndIt end, const Elem &value) noexcept(noexcept(*begin==value)) -> ForwardIt
Searches a range for the last occurence of a match, and returns an iterator to it....
constexpr auto find_last_in_if_not(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> size_t
Find the offset of the last element for which p returns false. It also allows for a sentinel end iter...
constexpr auto erase_if(Container &c, UnaryPredicate p) noexcept(noexcept(c.erase(std::remove_if(c.begin(), c.end(), std::ref(p)), c.end()))) -> void
Abbreviation of the erase-remove idiom as a free function.
constexpr auto erase(Container &c, const Elem &val) noexcept(noexcept(c.erase(std::remove(c.begin(), c.end(), val), c.end()))) -> void
Abbreviation of the erase-remove idiom as a free function.
constexpr auto find(ForwardIt begin, EndIt end, const Elem &value) noexcept(noexcept(*begin==value)) -> ForwardIt
Finds a value in range [begin, end). If not found, returns end. It also allows for a sentinel end ite...
constexpr auto search_replace_copy(ForwardIt1 h_begin, ForwardIt1 h_end, ForwardIt2 n_begin, ForwardIt2 n_end, ForwardIt3 r_begin, ForwardIt3 r_end, OutputIt d_begin, BinaryPredicate Compare={}) -> OutputIt
constexpr auto find_if_not(ForwardIt begin, EndIt end, UnaryPredicate &&pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> ForwardIt
Finds the first value in range [begin, end) for which pred returns false. If not found,...
constexpr auto find_last_in(ForwardIt begin, EndIt end, const Elem &value) noexcept(noexcept(*begin==value)) -> size_t
Find the offset of the last ocurrence of v in a range from the beginning. It also allows for a sentin...
constexpr auto find_in_if(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> size_t
Find the offset of the first element for which p returns true. It also allows for a sentinel end iter...
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
constexpr auto find_last_if(ForwardIt begin, EndIt end, UnaryPredicate pred) noexcept(noexcept(kblib::invoke(pred, *begin))) -> ForwardIt
Searches a range for the last element on which a predicate returns true. It also allows for a sentine...