Iros
 
Loading...
Searching...
No Matches
lower_bound.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace di::container {
8namespace detail {
10 template<concepts::ForwardIterator It, concepts::SentinelFor<It> Sent, typename T,
11 typename Proj = function::Identity,
12 concepts::IndirectStrictWeakOrder<T const*, meta::Projected<It, Proj>> Comp = function::Compare>
13 constexpr auto operator()(It first, Sent last, T const& needle, Comp comp = {}, Proj proj = {}) const -> It {
14 auto const distance = container::distance(first, last);
15 return lower_bound_with_size(util::move(first), needle, util::ref(comp), util::ref(proj), distance);
16 }
17
18 template<concepts::ForwardContainer Con, typename T, typename Proj = function::Identity,
21 constexpr auto operator()(Con&& container, T const& needle, Comp comp = {}, Proj proj = {}) const
24 return lower_bound_with_size(container::begin(container), needle, util::ref(comp), util::ref(proj),
25 distance);
26 }
27
28 private:
29 friend struct EqualRangeFunction;
30
31 template<typename It, typename T, typename Proj, typename Comp,
32 typename SSizeType = meta::IteratorSSizeType<It>>
33 constexpr static auto lower_bound_with_size(It first, T const& needle, Comp comp, Proj proj,
35 while (n != 0) {
36 SSizeType left_length = n >> 1;
37 auto* mid = container::next(first, left_length);
38 if (function::invoke(comp, needle, function::invoke(proj, *mid)) <= 0) {
39 // needle is less than or equal to every element in the range [mid, last).
40 n = left_length;
41 } else {
42 // needle is greater than every element in the range [first, mid].
43 n -= left_length + 1;
44 first = ++mid;
45 }
46 }
47 return first;
48 }
49 };
50}
51
52constexpr inline auto lower_bound = detail::LowerBoundFunction {};
53}
54
55namespace di {
57}
Definition forward_container.h:8
Definition indirect_strict_weak_order.h:12
Definition sequence.h:13
Definition sequence.h:12
constexpr auto next
Definition next.h:35
constexpr auto distance
Definition distance.h:44
constexpr auto lower_bound
Definition lower_bound.h:52
constexpr auto begin
Definition begin.h:44
constexpr auto invoke
Definition invoke.h:100
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 >, ContainerIterator< Con >, container::Dangling > BorrowedIterator
Definition borrowed_iterator.h:11
decltype(container::iterator_ssize_type(types::in_place_type< meta::RemoveCVRef< T > >)) IteratorSSizeType
Definition iterator_ssize_type.h:8
constexpr auto ref
Definition reference_wrapper.h:98
Definition zstring_parser.h:9
constexpr auto proj
Definition proj.h:59
constexpr auto operator()(Con &&container, T const &needle, Comp comp={}, Proj proj={}) const -> meta::BorrowedIterator< Con >
Definition lower_bound.h:21
constexpr auto operator()(It first, Sent last, T const &needle, Comp comp={}, Proj proj={}) const -> It
Definition lower_bound.h:13
friend struct EqualRangeFunction
Definition lower_bound.h:29
Definition compare.h:8
Definition identity.h:7