E.V.E
v2023.02.15
 
Loading...
Searching...
No Matches

◆ for_each_selected

eve::algo::for_each_selected = function_with_traits<for_each_selected_>
inlineconstexpr

#include <eve/module/algo/algo/for_each_selected.hpp>

#include <eve/module/algo.hpp>

The scalar equivalent of this algorithm would be:

bool for_each_selected(std::ranges::forward_range auto&& r,
auto is_selected,
auto&& loop_body) {
for (auto f = std::ranges::begin(r); f != std::ranges::end(r); ++f) {
if (is_selected(*f) && loop_body(f)) {
return true;
}
}
return false;
}
constexpr auto for_each_selected
an algorithm to perform some scalar operation for every element, that matches a given SIMD predicate....
Definition: for_each_selected.hpp:127

Callable Signatures

namespace eve::algo
{
template<relaxed_range Rng, typename P, irregular_predicate<unaligned_iterator_t<Rng>> LoopBody>
bool for_each_selected(Rng&& rng, P is_selected, LoopBody loop_body);
}

Parameters

  • rng - relaxed range which we iterate
  • is_selected - simd predicate to check values
  • loop_body - operation to perform for each value (should return true to break),

Return value

returns true iff the iteration was interrupted.

Example

#include <eve/module/algo.hpp>
#include <eve/module/core.hpp>
#include <iostream>
#include <vector>
#include <span>
#include <string_view>
#include <tts/tts.hpp>
std::vector<std::string_view>
split_by(std::string_view s, char by)
{
std::vector<std::string_view> res;
std::string_view::iterator last = s.begin();
auto maybe_add_a_string = [&](std::string_view::iterator it) {
// skipping empty
if( it != last ) res.push_back({last, it});
};
// arm has weird char type, so we cast.
std::span<const std::uint8_t> us{ (const std::uint8_t*) s.data(), s.size() };
us,
[&](auto x) { return x == (std::uint8_t)by; },
[&](std::span<const std::uint8_t>::iterator us_it)
{
auto it = s.begin() + (us_it - us.begin());
maybe_add_a_string(it);
last = it + 1;
return false; // return true here if you want to break.
});
maybe_add_a_string(s.end());
return res;
}
int
main()
{
std::string_view in = "Words shall be split !";
std::cout << " -> in = "
<< in
<< "\n";
std::cout << " -> split = "
<< tts::as_string(split_by(in, ' '))
<< "\n";
}