ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StaticSearch.h

The engineered searches of a sorted array that does not change, beyond the book. More...

Files

file  ADS/search/StaticSearch.h
 The engineered searches of a sorted array that does not change, beyond the book [see BinarySearch.h for the binary search of section 1.2]: the place of the first item not smaller than a key, the lower bound, in the fewest misses of the cache and with no branch that the processor may mispredict.

Classes

class  ADS::SPlusTree< T, Less, Bytes >
 the static B+-tree of Slotin on a sorted array, by the order less() More...

Functions

template<class T, class C = std::less<>>
std::size_t ADS::branchlessLowerBound (const std::vector< T > &A, const T &v, C less=C())
 the first place of an item not smaller than \( v \) in the sorted \( A \), \( n \) if none, by the binary search with no branch

Detailed description

The engineered searches of a sorted array that does not change, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of searching.

Function documentation

◆ branchlessLowerBound()

template<class T, class C = std::less<>>
std::size_t ADS::branchlessLowerBound ( const std::vector< T > & A,
const T & v,
C less = C() )

the first place of an item not smaller than \( v \) in the sorted \( A \), \( n \) if none, by the binary search with no branch

In pseudocode (Khuong and Morin, beyond the book), the part still to examine being \( A[ b .. b + \ell - 1 ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{branchlessLowerBound}( A , v ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ 0 \cr 3 & \quad b \gets 0;\ \ell \gets n \cr 4 & \quad \textbf{while}\ \ell > 1\ \textbf{do} \cr 5 & \qquad h \gets \lfloor \ell / 2 \rfloor;\ \text{prefetch the two } \text{middles of the next step} \cr 6 & \qquad b \gets b + h \cdot [ A[ b + h ] < v ]\quad \triangleright \ \text{a conditional move} \cr 7 & \qquad \ell \gets \ell - h \cr 8 & \quad \textbf{return}\ b + [ A[ b ] < v ] \end{array}} \]

Precondition
\( A \) is sorted (audit)
Postcondition
\( A[ r - 1 ] < v \le A[ r ] \)