Go to the documentation of this file. 1 #ifndef EAGINE_MEMORY_SPAN_ALGO_HPP
9 #define EAGINE_MEMORY_SPAN_ALGO_HPP
15 #include <type_traits>
18 namespace eagine::memory {
20 template <
typename T,
typename P,
typename S>
21 static constexpr
auto clamp_span_iterator(basic_span<T, P, S> s, P p) noexcept
23 return (p < s.begin()) ? s.begin() : (p > s.end()) ? s.end() : p;
26 template <
typename T,
typename P,
typename S,
typename I>
27 static constexpr
auto clamp_span_position(basic_span<T, P, S> s, I p) noexcept
28 -> std::enable_if_t<std::is_integral_v<I>, P> {
29 return clamp_span_iterator(s, s.begin() + p);
32 template <
typename T,
typename P,
typename S,
typename B,
typename E>
33 static constexpr
auto subspan(basic_span<T, P, S> s, B b, E e) noexcept
35 std::is_integral_v<B> && std::is_integral_v<E>,
36 basic_span<T, P, S>> {
37 return {clamp_span_position(s, b), clamp_span_position(s, e)};
46 template <
typename T,
typename P,
typename S,
typename I,
typename L>
49 return {clamp_span_position(s, i), clamp_span_position(s, i + l)};
59 template <
typename T,
typename P,
typename S,
typename L>
62 return slice(s, l, s.size() - l);
72 template <
typename T,
typename P,
typename S,
typename L>
75 return head(s, s.size() - l);
85 template <
typename T,
typename P,
typename S,
typename L>
98 template <
typename T,
typename P,
typename S,
typename L>
101 return slice(s, S(0), l);
118 static constexpr
auto
121 return head(s, l.size());
131 template <
typename T,
typename P,
typename S,
typename L>
134 return skip(s, s.size() - l);
151 static constexpr
auto
154 return tail(s, l.size());
169 static constexpr
auto
171 return are_equal(
head(spn, with.size()), with);
186 static constexpr
auto
188 return are_equal(
tail(spn, with.size()), with);
202 static constexpr
auto
219 static constexpr
auto
222 return ends_with(spn, suffix) ?
snip(spn, suffix.size()) : spn;
236 static constexpr
auto
239 for(S1 i = 0; i < spn.size(); ++i) {
259 static constexpr
auto
263 while(pos < spn.size()) {
278 template <
typename T,
typename P,
typename S,
typename E>
282 while(pos < spn.size()) {
283 if(are_equal(spn[pos], what)) {
297 template <
typename T,
typename P,
typename S,
typename F>
298 static constexpr
auto
302 while(pos < spn.size()) {
303 if(predicate(spn[pos])) {
314 template <
typename T,
typename P,
typename S,
typename Predicate>
315 static constexpr
auto
327 template <
typename T,
typename P,
typename S,
typename Predicate>
328 static constexpr
auto
351 auto pos = spn.size();
414 spn, extract_or(
find_position(spn, what), spn.size()) + what.size());
430 const auto pos{extract_or(
find_position(spn, what), spn.size())};
431 return {
head(spn, pos),
skip(spn, pos + what.size())};
482 return {
head(spn, pos),
skip(spn, pos + what.size())};
488 template <
typename T,
typename P,
typename S,
typename B>
497 while((pos < spn.size()) && (depth > 0)) {
498 if(are_equal(spn[pos], left)) {
500 }
else if(are_equal(spn[pos], right)) {
525 EAGINE_ASSERT(from.size() <= to.size());
526 std::copy(from.begin(), from.end(), to.begin());
527 return head(to, from.size());
535 template <
typename T,
typename P,
typename S,
typename V>
547 template <
typename T,
typename P,
typename S>
549 std::is_integral_v<T> || std::is_floating_point_v<T>,
560 template <
typename T,
typename P,
typename S>
571 template <
typename T,
typename P,
typename S,
typename Transform>
574 std::transform(spn.begin(), spn.end(), spn.begin(), std::move(
function));
583 template <
typename T,
typename P,
typename S,
typename Generator>
595 template <
typename T,
typename P,
typename S,
typename RandGen>
608 template <
typename T,
typename P,
typename S>
620 template <
typename T,
typename P,
typename S,
typename Compare>
623 std::sort(spn.begin(), spn.end(), std::move(compare));
643 if(spn.size() == idx.size()) {
644 std::sort(idx.begin(), idx.end(), [spn, &compare](
auto& l,
auto& r) {
645 return compare(spn[l], spn[r]);
656 template <
typename T,
typename P,
typename S,
typename I,
typename PI,
typename SI>
667 template <
typename T,
typename P,
typename S>
677 template <
typename T,
typename P,
typename S,
typename Compare>
679 return std::is_sorted(spn.begin(), spn.end(), std::move(compare));
686 template <
typename T,
typename P,
typename S,
typename Predicate>
689 return std::all_of(spn.begin(), spn.end(), std::move(predicate));
696 template <
typename T,
typename P,
typename S,
typename Predicate>
699 return std::any_of(spn.begin(), spn.end(), std::move(predicate));
706 template <
typename T,
typename P,
typename S,
typename Predicate>
709 return std::none_of(spn.begin(), spn.end(), std::move(predicate));
722 typename UnaryOperation>
726 UnaryOperation unary_op) {
738 template <
typename T,
typename P,
typename S,
typename UnaryOperation>
742 UnaryOperation unary_op) {
744 while(pos < spn.
size()) {
745 unary_op(
slice(spn, pos, len));
759 typename BinaryFunction>
763 BinaryFunction get_distance) -> Result {
764 const auto l1 = s1.size();
765 const auto l2 = s2.size();
772 auto prev_diagonal = Result(x) - 1;
775 const auto curr_diagonal =
column[y];
776 const auto prev_min = std::min(
column[y] + 1,
column[y - 1] + 1);
777 const auto new_dist =
778 prev_diagonal + Result(get_distance(s1[y - 1], s2[x - 1]));
779 column[y] = std::min(prev_min, new_dist);
780 prev_diagonal = curr_diagonal;
786 template <
typename T,
typename P1,
typename S1,
typename P2,
typename S2>
787 static auto default_edit_distance(
790 return basic_edit_distance<span_size_t>(
791 s1, s2, [](
auto e1,
auto e2) {
return e1 == e2 ? 0 : 1; });
795 #endif // EAGINE_MEMORY_SPAN_ALGO_HPP
static constexpr auto find_position(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) noexcept -> optionally_valid< S1 >
Finds the position of the first occurrence of what in a span.
Definition: span_algo.hpp:260
static constexpr auto slice(basic_span< T, P, S > s, I i, L l) noexcept -> basic_span< T, P, S >
Returns a slice of span starting at specified index with specified length.
Definition: span_algo.hpp:47
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static constexpr auto ends_with(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > with) -> bool
Indicates if span spn ends with the content of with.
Definition: span_algo.hpp:187
static auto slice_after(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> basic_span< T1, P1, S1 >
Returns a slice of span after the first occurrence of what.
Definition: span_algo.hpp:411
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
static constexpr auto find_element(basic_span< T, P, S > spn, E what) noexcept -> optionally_valid< S >
Finds the position of the first occurrence of what in a span.
Definition: span_algo.hpp:279
Primary template for conditionally valid values.
Definition: decl.hpp:49
static constexpr auto strip_prefix(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > prefix) -> basic_span< T1, P1, S1 >
Strips the specified prefix from a span.
Definition: span_algo.hpp:203
static constexpr auto reverse_find_position(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) noexcept -> optionally_valid< S1 >
Finds the position of the last occurrence of what in a span.
Definition: span_algo.hpp:348
static constexpr auto find_element_if(basic_span< T, P, S > spn, F predicate) noexcept -> optionally_valid< S >
Finds the position of the first element satisfying predicate in a span.
Definition: span_algo.hpp:299
static auto find(basic_span< T1, P1, S1 > where, basic_span< T2, P2, S2 > what) -> basic_span< T1, P1, S1 >
Finds the position of the last occurrence of what in a span.
Definition: span_algo.hpp:374
static auto shuffle(basic_span< T, P, S > spn, RandGen rg) -> basic_span< T, P, S >
Shuffles the elements of a span.
Definition: span_algo.hpp:596
static constexpr auto take_until(basic_span< T, P, S > spn, Predicate predicate) noexcept -> basic_span< T, P, S >
Takes the elements from the front of a span until predicate is satisfied.
Definition: span_algo.hpp:329
static auto none_of(basic_span< T, P, S > spn, Predicate predicate) -> bool
Indicates if no elements in a span satisfy predicate.
Definition: span_algo.hpp:707
static auto basic_edit_distance(basic_span< const T, P1, S1 > s1, basic_span< const T, P2, S2 > s2, BinaryFunction get_distance) -> Result
Calculates the edit distance of s1 and s2 according to get_distance.
Definition: span_algo.hpp:760
static auto all_of(basic_span< T, P, S > spn, Predicate predicate) -> bool
Indicates if all elements in a span satisfy predicate.
Definition: span_algo.hpp:687
static auto split_by_last(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> std::tuple< basic_span< T1, P1, S1 >, basic_span< T1, P1, S1 >>
Splits a span by the last occurrence of what (before and after, what)
Definition: span_algo.hpp:479
static auto reverse(basic_span< T, P, S > spn) -> basic_span< T, P, S >
Reverses the elements in a span.
Definition: span_algo.hpp:561
static constexpr auto snip(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Snips a specified count of elements from the back of a span.
Definition: span_algo.hpp:73
static auto slice_before_last(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> basic_span< T1, P1, S1 >
Returns a slice of span before the last occurrence of what.
Definition: span_algo.hpp:445
static auto slice_inside_brackets(basic_span< T, P, S > spn, B left, B right) noexcept -> basic_span< T, P, S >
Returns a slice of span within a pair of brackets.
Definition: span_algo.hpp:490
static auto any_of(basic_span< T, P, S > spn, Predicate predicate) -> bool
Indicates if any elements in a span satisfy predicate.
Definition: span_algo.hpp:697
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
static constexpr auto skip(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Skips a specified count of elements from the front of a span.
Definition: span_algo.hpp:60
static auto fill(basic_span< T, P, S > spn, const V &v) -> basic_span< T, P, S >
Fills a span with copies of the specified value.
Definition: span_algo.hpp:536
static auto generate(basic_span< T, P, S > spn, Generator gen) -> basic_span< T, P, S >
Fills a span with elements generated by a generator callable.
Definition: span_algo.hpp:584
static auto transform(basic_span< T, P, S > spn, Transform function) -> basic_span< T, P, S >
Transforms the elements of a span with a function.
Definition: span_algo.hpp:572
static auto copy(basic_span< TF, PF, SF > from, basic_span< TT, PT, ST > to) -> basic_span< TT, PT, ST >
Copies the elements from one span to another compatible span.
Definition: span_algo.hpp:523
static void for_each_delimited(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > delim, UnaryOperation unary_op)
Scans span for parts split by delimiter, calls a function for each part.
Definition: span_algo.hpp:723
static constexpr auto head(basic_span< Ts, Ps, Ss > s, basic_span< Tl, Pl, Sl > l) noexcept -> basic_span< Ts, Ps, Ss >
Returns the head of s l.size() elements long.
Definition: span_algo.hpp:119
static auto split_by_first(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> std::tuple< basic_span< T1, P1, S1 >, basic_span< T1, P1, S1 >>
Splits a span by the first occurrence of what (before and after, what)
Definition: span_algo.hpp:428
static auto is_sorted(basic_span< T, P, S > spn, Compare compare) -> bool
Tests if the elements in a span are sorted according to compare.
Definition: span_algo.hpp:678
static auto zero(basic_span< T, P, S > spn) -> std::enable_if_t< std::is_integral_v< T >||std::is_floating_point_v< T >, basic_span< T, P, S >>
Fills a span with zero value of type T.
Definition: span_algo.hpp:548
static auto slice_after_last(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> basic_span< T1, P1, S1 >
Returns a slice of span after the last occurrence of what.
Definition: span_algo.hpp:461
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the span.
Definition: span.hpp:246
static constexpr auto column(const matrix< T, C, R, false, V > &m) noexcept -> vector< T, R, V >
Returns the I-th column vector of a matrix. Column-major implementation.
Definition: matrix.hpp:428
static constexpr auto contains(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) noexcept -> S1
Indicates if a span contains the contents of what.
Definition: span_algo.hpp:237
static auto sort(basic_span< T, P, S > spn, Compare compare) -> basic_span< T, P, S >
Sorts the elements of a span according to compare.
Definition: span_algo.hpp:621
static constexpr auto tail(basic_span< Ts, Ps, Ss > s, basic_span< Tl, Pl, Sl > l) noexcept -> basic_span< Ts, Ps, Ss >
Returns the tail of s l.size() elements long.
Definition: span_algo.hpp:152
static constexpr auto shrink(basic_span< T, P, S > s, L l) noexcept -> basic_span< T, P, S >
Shrinks a span by removing a specified count of elements from both sides.
Definition: span_algo.hpp:86
static void for_each_chunk(basic_span< T, P, S > spn, const span_size_t len, UnaryOperation unary_op)
Splits span into parts of equal length, calls a function for each part.
Definition: span_algo.hpp:739
static constexpr auto starts_with(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > with) -> bool
Indicates if span spn starts with the content of with.
Definition: span_algo.hpp:170
static constexpr auto strip_suffix(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > suffix) -> basic_span< T1, P1, S1 >
Strips the specified suffix from a span.
Definition: span_algo.hpp:220
static auto make_index(basic_span< T, P, S > spn, basic_span< I, PI, SI > idx) -> bool
Makes the index of a span, into another span.
Definition: span_algo.hpp:658
static constexpr auto skip_until(basic_span< T, P, S > spn, Predicate predicate) noexcept -> basic_span< T, P, S >
Skips the elements from the front of a span until predicate is satisfied.
Definition: span_algo.hpp:316
static auto slice_before(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> basic_span< T1, P1, S1 >
Returns a slice of span before the first occurrence of what.
Definition: span_algo.hpp:394