mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-16 15:14:13 +00:00
add doc: Binary search algorithms
This commit is contained in:
parent
958e2cb7a4
commit
c4e3ce3d5e
21 changed files with 1204 additions and 78 deletions
53
source/libs/sprout/algorithm/binary_search.rst
Normal file
53
source/libs/sprout/algorithm/binary_search.rst
Normal file
|
@ -0,0 +1,53 @@
|
|||
.. _sprout-algorithm-binary_search:
|
||||
###############################################################################
|
||||
binary_search
|
||||
###############################################################################
|
||||
|
||||
Interface
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
binary_search(ForwardIterator first, ForwardIterator last, T const& value);
|
||||
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
binary_search(ForwardIterator first, ForwardIterator last, T const& value, Compare comp);
|
||||
|
||||
Requires
|
||||
========================================
|
||||
|
||||
| The elements e of [first,last) are partitioned with respect to the expressions ``e < value`` and ``!(value < e)`` or ``comp(e, value)`` and ``!comp(value, e)``.
|
||||
| Also, for all elements e of [first,last), ``e < value`` implies ``!(value < e)`` or ``comp(e, value)`` implies ``!comp(value, e)``.
|
||||
|
||||
Returns
|
||||
========================================
|
||||
|
||||
| true if there is an iterator i in the range [first,last) that satisfies the corresponding conditions: ``!(*i < value) && !(value < *i)`` or ``comp(*i, value) && comp(value, *i)``.
|
||||
|
||||
Examples
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
#include <sprout/algorithm/binary_search.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
using namespace sprout;
|
||||
|
||||
SPROUT_STATIC_CONSTEXPR auto input = array<int, 10>{{1, 2, 3, 5, 5, 5, 5, 8, 9, 10}};
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::binary_search(begin(input), end(input), 5);
|
||||
static_assert(result, "found 5 by binary search.");
|
||||
|
||||
Complexity
|
||||
========================================
|
||||
|
||||
| At most ``log2(last - first) + O(1)`` comparisons.
|
||||
| Recursive function invocations in *O(logN)* (logarithmic) depth.
|
||||
|
||||
Header
|
||||
========================================
|
||||
|
||||
| ``sprout/algorithm/binary_search.hpp``
|
||||
| Convenience header: ``sprout/algorithm.hpp``
|
||||
|
58
source/libs/sprout/algorithm/equal_range.rst
Normal file
58
source/libs/sprout/algorithm/equal_range.rst
Normal file
|
@ -0,0 +1,58 @@
|
|||
.. _sprout-algorithm-equal_range:
|
||||
###############################################################################
|
||||
equal_range
|
||||
###############################################################################
|
||||
|
||||
Interface
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value);
|
||||
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp);
|
||||
|
||||
Requires
|
||||
========================================
|
||||
|
||||
| The elements e of [first,last) shall be partitioned with respect to the expressions ``e < value`` and ``!(value < e)`` or ``comp(e, value)`` and ``!comp(value, e)``.
|
||||
| Also, for all elements e of [first, last), ``e < value`` shall imply ``!(value < e)`` or ``comp(e, value)`` shall imply ``!comp(value, e)``.
|
||||
|
||||
Returns
|
||||
========================================
|
||||
|
||||
| ``make_pair(lower_bound(first, last, value), upper_bound(first, last, value))``
|
||||
| or
|
||||
| ``make_pair(lower_bound(first, last, value, comp), upper_bound(first, last, value, comp))``
|
||||
|
||||
Examples
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
#include <sprout/algorithm/equal_range.hpp>
|
||||
#include <sprout/algorithm/all_of_equal.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
using namespace sprout;
|
||||
|
||||
SPROUT_STATIC_CONSTEXPR auto input = array<int, 10>{{1, 2, 3, 5, 5, 5, 5, 8, 9, 10}};
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::equal_range(begin(input), end(input), 5);
|
||||
static_assert(result.first - begin(input) == 3, "a lower bound position is 3.");
|
||||
static_assert(result.second - begin(input) == 7, "a upper bound position is 7.");
|
||||
static_assert(sprout::all_of_equal(result.first, result.second. 5), "all of result range is equal to 5.");
|
||||
|
||||
Complexity
|
||||
========================================
|
||||
|
||||
| At most ``2 * log2(last - first) + O(1)`` comparisons.
|
||||
| Recursive function invocations in *O(logN)* (logarithmic) depth.
|
||||
|
||||
Header
|
||||
========================================
|
||||
|
||||
| ``sprout/algorithm/equal_range.hpp``
|
||||
| Convenience header: ``sprout/algorithm.hpp``
|
||||
|
|
@ -33,6 +33,10 @@ Sprout.Algorithm
|
|||
is_decreasing
|
||||
is_strictly_increasing
|
||||
is_strictly_decreasing
|
||||
lower_bound
|
||||
upper_bound
|
||||
equal_range
|
||||
binary_search
|
||||
|
||||
.. _sprout-algorithm-non_modifying:
|
||||
*******************************************************************************
|
||||
|
|
52
source/libs/sprout/algorithm/lower_bound.rst
Normal file
52
source/libs/sprout/algorithm/lower_bound.rst
Normal file
|
@ -0,0 +1,52 @@
|
|||
.. _sprout-algorithm-lower_bound:
|
||||
###############################################################################
|
||||
lower_bound
|
||||
###############################################################################
|
||||
|
||||
Interface
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(ForwardIterator first, ForwardIterator last, T const& value);
|
||||
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp);
|
||||
|
||||
Requires
|
||||
========================================
|
||||
|
||||
| The elements e of [first,last) shall be partitioned with respect to the expression ``e < value`` or ``comp(e, value)``.
|
||||
|
||||
Returns
|
||||
========================================
|
||||
|
||||
| The furthermost iterator i in the range [first,last] such that for any iterator j in the range [first,i) the following corresponding conditions hold: ``*j < value`` or ``comp(*j, value)``.
|
||||
|
||||
Examples
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
#include <sprout/algorithm/lower_bound.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
using namespace sprout;
|
||||
|
||||
SPROUT_STATIC_CONSTEXPR auto input = array<int, 10>{{1, 2, 3, 5, 5, 5, 5, 8, 9, 10}};
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::lower_bound(begin(input), end(input), 5);
|
||||
static_assert(result - begin(input) == 3, "a lower bound position is 3.");
|
||||
|
||||
Complexity
|
||||
========================================
|
||||
|
||||
| At most ``log2(last - first) + O(1)`` comparisons.
|
||||
| Recursive function invocations in *O(logN)* (logarithmic) depth.
|
||||
|
||||
Header
|
||||
========================================
|
||||
|
||||
| ``sprout/algorithm/lower_bound.hpp``
|
||||
| Convenience header: ``sprout/algorithm.hpp``
|
||||
|
52
source/libs/sprout/algorithm/upper_bound.rst
Normal file
52
source/libs/sprout/algorithm/upper_bound.rst
Normal file
|
@ -0,0 +1,52 @@
|
|||
.. _sprout-algorithm-upper_bound:
|
||||
###############################################################################
|
||||
upper_bound
|
||||
###############################################################################
|
||||
|
||||
Interface
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(ForwardIterator first, ForwardIterator last, T const& value);
|
||||
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp);
|
||||
|
||||
Requires
|
||||
========================================
|
||||
|
||||
| The elements e of [first,last) shall be partitioned with respect to the expression ``!(value < e)`` or ``!comp(value, e)``.
|
||||
|
||||
Returns
|
||||
========================================
|
||||
|
||||
| The furthermost iterator i in the range [first,last] such that for any iterator j in the range [first,i) the following corresponding conditions hold: ``!(value < *j)`` or ``!comp(value, *j)``.
|
||||
|
||||
Examples
|
||||
========================================
|
||||
.. sourcecode:: c++
|
||||
|
||||
#include <sprout/algorithm/upper_bound.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/container.hpp>
|
||||
using namespace sprout;
|
||||
|
||||
SPROUT_STATIC_CONSTEXPR auto input = array<int, 10>{{1, 2, 3, 5, 5, 5, 5, 8, 9, 10}};
|
||||
SPROUT_STATIC_CONSTEXPR auto result = sprout::upper_bound(begin(input), end(input), 5);
|
||||
static_assert(result - begin(input) == 7, "a upper bound position is 7.");
|
||||
|
||||
Complexity
|
||||
========================================
|
||||
|
||||
| At most ``log2(last - first) + O(1)`` comparisons.
|
||||
| Recursive function invocations in *O(logN)* (logarithmic) depth.
|
||||
|
||||
Header
|
||||
========================================
|
||||
|
||||
| ``sprout/algorithm/upper_bound.hpp``
|
||||
| Convenience header: ``sprout/algorithm.hpp``
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue