9 const auto equal = [](
auto a,
auto b) {
10 return std::equal(std::begin(
a), std::end(
a), std::begin(b), std::end(b));
13 const std::vector<unsigned> erase_test{2, 2, 3, 4, 5, 7, 8, 11};
14 const std::vector<unsigned> no_2s = {3, 4, 5, 7, 8, 11};
15 auto erase_copy = erase_test;
18 erase_copy = erase_test;
19 const std::vector<unsigned> no_evens = {3, 5, 7, 11};
27 int vec[] = {0, 3, 4, 5, 6, 1, 2, 4, 4, 5};
28 auto begin = std::begin(vec);
29 auto end = std::end(vec);
63 int none[] = {7, 8, -1};
68 int vec[] = {0, 3, 4, 5, 6, 1, 2, 4, 4, 5};
69 auto begin = std::begin(vec);
70 auto end = std::end(vec);
95 using namespace std::literals;
98 SECTION(
"search_replace_copy_1") {
99 const auto haystack =
"a string with words"s;
100 const auto needle =
"with"s;
101 const auto replace =
"comprising"s;
104 end(needle), begin(replace), end(replace),
105 std::back_inserter(result));
106 CHECK(result ==
"a string comprising words");
108 SECTION(
"search_replace_copy_2(no match)") {
109 const auto haystack =
"a string with words"s;
110 const auto needle =
"without"s;
111 const auto replace =
"comprising"s;
114 end(needle), begin(replace), end(replace),
115 std::back_inserter(result));
116 CHECK(result == haystack);
118 SECTION(
"search_replace_copy_3(multiple matches)") {
119 const auto haystack =
"a string with multiple 'with's"s;
120 const auto needle =
"with"s;
121 const auto replace =
"containing"s;
124 end(needle), begin(replace), end(replace),
125 std::back_inserter(result));
126 CHECK(result ==
"a string containing multiple 'containing's");
128 SECTION(
"search_replace_copy_4(empty match)") {
129 const auto haystack =
"a string with words"s;
130 const auto needle =
""s;
131 const auto replace =
"comprising"s;
134 end(needle), begin(replace), end(replace),
135 std::back_inserter(result));
136 CHECK(result == haystack);
138 SECTION(
"search_replace_copy_5(match at beginning)") {
139 const auto haystack =
"a string with words"s;
140 const auto needle =
"a"s;
141 const auto replace =
"another"s;
144 end(needle), begin(replace), end(replace),
145 std::back_inserter(result));
146 CHECK(result ==
"another string with words");
148 SECTION(
"search_replace_copy_6(match at end)") {
149 const auto haystack =
"a string with words"s;
150 const auto needle =
"words"s;
151 const auto replace =
"letters"s;
154 end(needle), begin(replace), end(replace),
155 std::back_inserter(result));
156 CHECK(result ==
"a string with letters");
158 SECTION(
"search_replace_copy_7(only matches)") {
159 const auto haystack =
"abababababab"s;
160 const auto needle =
"ab"s;
161 const auto replace =
"ba"s;
164 end(needle), begin(replace), end(replace),
165 std::back_inserter(result));
166 CHECK(result ==
"babababababa");
168 SECTION(
"search_replace_copy_8(match is whole string)") {
169 const auto haystack =
"string"s;
170 const auto& needle = haystack;
171 const auto replace =
"replace"s;
174 end(needle), begin(replace), end(replace),
175 std::back_inserter(result));
176 CHECK(result == replace);
178 SECTION(
"search_replace_copy_9(overlap)") {
179 const auto haystack =
"abcabcab"s;
180 const auto needle =
"abcab"s;
181 const auto replace =
"a"s;
184 end(needle), begin(replace), end(replace),
185 std::back_inserter(result));
186 CHECK(result ==
"acab");
192 std::array<int, 11> arr{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
195 = kblib::get_max_n_old<std::vector<int>>(arr.begin(), arr.end(), 2);
196 CHECK(max_two.size() == 2);
202 = kblib::get_max_n_old<std::set<int>>(arr.begin(), arr.end(), 2);
203 CHECK(max_two.size() == 2);
204 CHECK(max_two.find(10) != max_two.end());
205 CHECK(max_two.find(9) != max_two.end());
209 = kblib::get_max_n<std::vector<int>>(arr.begin(), arr.end(), 2);
210 CHECK(max_two.size() == 2);
215 auto max_two = kblib::get_max_n<std::set<int>>(arr.begin(), arr.end(), 2);
216 CHECK(max_two.size() == 2);
217 CHECK(max_two.find(10) != max_two.end());
218 CHECK(max_two.find(9) != max_two.end());
227 auto equal = [](
auto r1,
auto r2) {
228 auto r1b = r1.begin();
230 auto r2b = r2.begin();
232 return (std::distance(r1b, r1e) == std::distance(r2b, r2e))
235 std::array<int, 10> haystack{1, 1, 2, 5, 6, 2, 4, 7, 0, 6};
237 std::array<int, N> maxN_target{7, 6, 6, 5, 4};
240 haystack.begin(), haystack.end(), N)));
241 std::vector<int> maxN_copy;
243 std::back_inserter(maxN_copy), 5);
248 haystack.begin(), haystack.end(), N)));
250 std::vector<int> maxN_psc(N);
251 std::partial_sort_copy(haystack.begin(), haystack.end(), maxN_psc.begin(),
252 maxN_psc.end(), std::greater<>{});
264 CHECK(
equal(
range, std::vector<int>{0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30,
265 33, 36, 39, 42, 45, 48}));
267 = kblib::get_max_n<std::vector<int>>(
range.begin(),
range.end(), 5);
268 CHECK(maxN_range == std::vector<int>{48, 45, 42, 39, 36});
273 std::vector<int> input1{1, 2, 3, 4, 5, 6};
274 std::vector<short> input2{2, 3, 4, 5, 6, 7};
276 for (
auto t :
kblib::zip(input1.begin(), input1.end(), input2.begin())) {
277 auto&
a = std::get<0>(t);
278 auto& b = std::get<1>(t);
283 std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8};
284 for (
auto t :
kblib::zip(input.begin(), input.end(), input.begin())) {
285 auto&
a = std::get<0>(t);
286 auto& b = std::get<1>(t);
291 std::vector<int> input{1, 2, 3, 4, 5, 6, 7, 8};
293 kblib::zip(input.begin(), input.end() - 1, input.begin() + 1)) {
294 auto&
a = std::get<0>(t);
295 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...