the search by interpolation of a sorted array of integers by bins
More...
#include <ADS/string/PrefixSearch.h>
|
| | BinnedSearch (std::vector< std::uint64_t > X) |
| | the bins of \( X \), in \( O( m ) \)
|
| std::size_t | position (std::uint64_t y) const |
| | the number of items smaller than \( y \)
|
| bool | contains (std::uint64_t y) const |
| | whether \( y \) is an item, i.e., \( X[ r ] = y \) at the place \( r \) that position( y ) gives
|
| std::size_t | bins (void) const |
| | the number of bins
|
| std::uint64_t | width (void) const |
| | the width \( b \) of a bin
|
| std::pair< std::size_t, std::size_t > | bin (std::size_t j) const |
| | the places \( [ \mathit{first} , \mathit{last} ) \) of \( X \) in the bin \( j \)
|
| std::optional< std::size_t > | binOf (std::uint64_t y) const |
| | the bin of \( y \), or none if \( y \) is out of \( [ x_1 , x_m ] \)
|
| std::size_t | largestBin (void) const |
| | the number of items of the largest bin
|
| std::size_t | size (void) const |
| | the number \( m \) of items
|
| const std::vector< std::uint64_t > & | items (void) const |
| | the sorted array \( X \)
|
the search by interpolation of a sorted array of integers by bins
◆ BinnedSearch()
| ADS::BinnedSearch::BinnedSearch |
( |
std::vector< std::uint64_t > | X | ) |
|
|
explicit |
the bins of \( X \), in \( O( m ) \)
In pseudocode (Demaine, Jones and Pătrașcu, beyond the book), \( \mathit{start}[ j ] \) the first place of \( X \) in the bin \( j \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{BinnedSearch}( X ) \cr
2 & \quad b \gets \lfloor ( x_m - x_1 ) / m \rfloor + 1;\ \mathit{nb}
\gets \lfloor ( x_m - x_1 ) / b \rfloor + 1 \cr
3 & \quad i \gets 0 \cr
4 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ \mathit{nb} - 1
\ \textbf{do} \cr
5 & \qquad \textbf{while}\ i < m\ \textbf{and}\ X[ i ] < x_1 + j b
\ \textbf{do}\ i \gets i + 1 \cr
6 & \qquad \mathit{start}[ j ] \gets i \cr
7 & \quad \mathit{start}[ \mathit{nb} ] \gets m
\end{array}}
\]
- Precondition
- \( X \) is sorted (audit)
◆ bin()
| std::pair< std::size_t, std::size_t > ADS::BinnedSearch::bin |
( |
std::size_t | j | ) |
const |
|
nodiscard |
the places \( [ \mathit{first} , \mathit{last} ) \) of \( X \) in the bin \( j \)
- Precondition
- \( j \) is less than bins()
◆ binOf()
| std::optional< std::size_t > ADS::BinnedSearch::binOf |
( |
std::uint64_t | y | ) |
const |
|
nodiscard |
the bin of \( y \), or none if \( y \) is out of \( [ x_1 , x_m ] \)
◆ bins()
| std::size_t ADS::BinnedSearch::bins |
( |
void | | ) |
const |
|
nodiscard |
◆ contains()
| bool ADS::BinnedSearch::contains |
( |
std::uint64_t | y | ) |
const |
|
nodiscard |
whether \( y \) is an item, i.e., \( X[ r ] = y \) at the place \( r \) that position( y ) gives
◆ items()
| const std::vector< std::uint64_t > & ADS::BinnedSearch::items |
( |
void | | ) |
const |
|
nodiscard |
◆ largestBin()
| std::size_t ADS::BinnedSearch::largestBin |
( |
void | | ) |
const |
|
nodiscard |
the number of items of the largest bin
◆ position()
| std::size_t ADS::BinnedSearch::position |
( |
std::uint64_t | y | ) |
const |
|
nodiscard |
the number of items smaller than \( y \)
In pseudocode (Demaine, Jones and Pătrașcu, beyond the book), a binary search in the bin of \( y \) alone:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{position}( y ) \cr
2 & \quad \textbf{if}\ m = 0\ \textbf{or}\ y \le x_1\ \textbf{then}
\ \textbf{return}\ 0 \cr
3 & \quad \textbf{if}\ y > x_m\ \textbf{then return}\ m \cr
4 & \quad j \gets \lfloor ( y - x_1 ) / b \rfloor \cr
5 & \quad \textbf{return}\ \text{the first place } r \in [
\mathit{start}[ j ] , \mathit{start}[ j + 1 ] ) \text{ with } y \le
X[ r ] \text{ (or } \mathit{start}[ j + 1 ] \text{), by a binary }
\text{search}
\end{array}}
\]
- Postcondition
- \( X[ r - 1 ] < y \le X[ r ] \)
◆ size()
| std::size_t ADS::BinnedSearch::size |
( |
void | | ) |
const |
|
nodiscard |
the number \( m \) of items
◆ width()
| std::uint64_t ADS::BinnedSearch::width |
( |
void | | ) |
const |
|
nodiscard |
the width \( b \) of a bin
The documentation for this class was generated from the following file: