|
ADS
Algorithms and Data Structures
|
The search of the first place where a monotone predicate becomes true, when no upper bound on that place is known: on the integers, on a sequence of unknown length, and on the integer square root. More...
#include "ADS/Contract.h"#include "ADS/sort/Sorted.h"#include <concepts>#include <cstddef>#include <limits>#include <type_traits>#include "ADS/search/impl/MonotoneSearch.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| template<std::integral I, class P> | |
| I | ADS::exponentialSearch (P &&p, I lo=I(0)) |
| the least \( i \ge \mathit{lo} \) with \( p( i ) \), \( p \) false and then true from \( \mathit{lo} \); \( O( \log( i - \mathit{lo} + 1 ) ) \) evaluations of \( p \) | |
| template<class G, class P> | |
| std::size_t | ADS::unboundedSearch (G &&get, P &&pred) |
| the first place \( i \) where get( i ), which gives an optional item, empty past the end of the sequence, gives no item or an item \( x \) with pred( x ); \( O( \log i ) \) calls of get() | |
| template<std::unsigned_integral U> | |
| U | ADS::integerSqrt (U n) |
| the largest \( r \) with \( r^2 \le n \), by a binary search with no overflow; \( w / 2 \) steps for \( w \) bits, none for a bool, which is its own root | |
| template<std::unsigned_integral U> | |
| bool | ADS::isPerfectSquare (U n) |
| true if \( n = k^2 \) for an integer \( k \), i.e., if the square of integerSqrt( n ) is \( n \) | |
The search of the first place where a monotone predicate becomes true, when no upper bound on that place is known: on the integers, on a sequence of unknown length, and on the integer square root.
The contracts [see Contract.h]: exponentialSearch() needs a place up to the largest integer where the predicate holds, a precondition checked when the probes reach it, and checks that \( p \) holds at the result and not before it, evaluations made with checkingContract() true [see Sorted.h]; integerSqrt() checks \( r^2 \le n < ( r + 1 )^2 \). The functions are defined in MonotoneSearch.tpp.