Iros
 
Loading...
Searching...
No Matches
search_n.h
Go to the documentation of this file.
1#pragma once
2
7#include "di/function/equal.h"
8
9namespace di::container {
10namespace detail {
12 template<concepts::ForwardIterator It, concepts::SentinelFor<It> Sent, typename T,
13 typename Pred = function::Equal, typename Proj = function::Identity,
14 typename SSizeType = meta::IteratorSSizeType<It>>
16 constexpr auto operator()(It first, Sent last, meta::TypeIdentity<SSizeType> n, T const& value, Pred pred = {},
17 Proj proj = {}) const -> View<It> {
18 if (n <= 0) {
19 return { first, first };
20 }
21 for (; first != last; ++first) {
22 if (!function::invoke(pred, function::invoke(proj, *first), value)) {
23 continue;
24 }
25
26 // Have one match, try to keep going.
27 auto* start = first;
28 SSizeType count = 1;
29 for (;; ++count) {
30 if (count == n) {
31 return { start, container::next(first) };
32 }
33 if (++first == last) {
34 return { first, first };
35 }
36 if (!function::invoke(pred, function::invoke(proj, *first), value)) {
37 break;
38 }
39 }
40 }
41 return { first, first };
42 }
43
44 template<concepts::ForwardContainer Con, typename T, typename Pred = function::Equal,
45 typename Proj = function::Identity>
46 requires(concepts::IndirectlyComparable<meta::ContainerIterator<Con>, T const*, Pred, Proj>)
47 constexpr auto operator()(Con&& container, meta::ContainerSSizeType<Con> n, T const& needle, Pred pred = {},
48 Proj proj = {}) const -> meta::BorrowedView<Con> {
49 return (*this)(container::begin(container), container::end(container), n, needle, util::ref(pred),
51 }
52 };
53}
54
55constexpr inline auto search_n = detail::SearchNFunction {};
56}
57
58namespace di {
60}
Definition view.h:35
Definition indirectly_comparable.h:10
Definition sequence.h:13
constexpr auto first(concepts::detail::ConstantVector auto &vector, size_t count)
Definition vector_first.h:13
Definition sequence.h:12
constexpr auto next
Definition next.h:35
constexpr auto search_n
Definition search_n.h:55
constexpr auto count
Definition count.h:37
constexpr auto end
Definition end.h:47
constexpr auto begin
Definition begin.h:44
constexpr auto start
Definition start.h:20
constexpr auto invoke
Definition invoke.h:100
IteratorSSizeType< ContainerIterator< T > > ContainerSSizeType
Definition container_ssize_type.h:8
Type< TypeConstant< T > > TypeIdentity
This is a helper template to prevent C++ from deducing the type of template argument.
Definition core.h:32
Conditional< concepts::BorrowedContainer< Con >, container::View< ContainerIterator< Con > >, container::Dangling > BorrowedView
Definition borrowed_view.h:12
meta::Type< SSizeTypeHelper< T > > SSizeType
Definition strong_int.h:33
constexpr auto ref
Definition reference_wrapper.h:98
Definition zstring_parser.h:9
constexpr auto proj
Definition proj.h:59
@ T
Definition key.h:29