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

The engineered intersection of two sorted sets, beyond the book. More...

Files

file  ADS/set/VectorIntersection.h
 The engineered intersection of two sorted sets, beyond the book [see SortedIntersection.h for the algorithms of Ferragina, chapter 6, and SortedListSet.h for the merge of section 8.3]: intersect() writes the items common to \( A \) of \( n \) items and \( B \) of \( m \) items, both strictly increasing, into out, and gives their number.

Functions

template<class T>
std::size_t ADS::intersect (const T *a, std::size_t n, const T *b, std::size_t m, T *out)
 the items common to \( a[ 0 .. n ) \) and \( b[ 0 .. m ) \), written to out, and their number
template<class T>
std::vector< T > ADS::intersect (const std::vector< T > &A, const std::vector< T > &B)
 the items common to \( A \) and \( B \), by intersect() above on their data

Detailed description

The engineered intersection of two sorted sets, beyond the book.

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

Function documentation

◆ intersect() [1/2]

template<class T>
std::vector< T > ADS::intersect ( const std::vector< T > & A,
const std::vector< T > & B )

the items common to \( A \) and \( B \), by intersect() above on their data

It is the function above on a vector of room enough:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersect}( A , B ) \cr 2 & \quad \textbf{return}\ \text{the first } \mathrm{intersect}( A , | A | , B , | B | ) \text{ items written} \end{array}} \]

Precondition
\( A \) and \( B \) strictly increasing

◆ intersect() [2/2]

template<class T>
std::size_t ADS::intersect ( const T * a,
std::size_t n,
const T * b,
std::size_t m,
T * out )

the items common to \( a[ 0 .. n ) \) and \( b[ 0 .. m ) \), written to out, and their number

In pseudocode (beyond the book, after Schlegel, Willhalm and Lehner and Lemire, Boytsov and Kurz), \( a \) the longer list; the merge with no branch shown, which the blocks of \( L \) items replace on the integers of 32 bits with Highway:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersect}( a , n , b , m ) \cr 2 & \quad \textbf{if}\ n < m\ \textbf{then}\ \text{swap } ( a , n ) \text{ with } ( b , m ) \cr 3 & \quad \textbf{if}\ 4 m < n\ \textbf{then return}\ \text{the items } \text{of } b \text{ found in } a \text{ by galloping, as in } \mathrm{intersectDoubling} \cr 4 & \quad i \gets 0;\ j \gets 0;\ k \gets 0 \cr 5 & \quad \textbf{while}\ i < n\ \textbf{and}\ j < m\ \textbf{do} \cr 6 & \qquad x \gets a[ i ];\ y \gets b[ j ];\ \mathit{out}[ k ] \gets x \cr 7 & \qquad k \gets k + [ x = y ];\ i \gets i + [ x \le y ];\ j \gets j + [ y \le x ] \cr 8 & \quad \textbf{return}\ k \end{array}} \]

Precondition
\( a \) and \( b \) strictly increasing; out has room for \( \min( n , m ) + 8 \) items