|
ADS
Algorithms and Data Structures
|
The intersection of two sorted sets, the core of the resolution of a query of several words by an inverted index (Ferragina, chapter 6). More...
Files | |
| file | ADS/set/SortedIntersection.h |
| The intersection of two sorted sets, the core of the resolution of a query of several words by an inverted index (Ferragina, chapter 6): \( A \) of \( n \) items and \( B \) of \( m \) items, strictly increasing, \( m \leq n \) (the roles are swapped otherwise). | |
Classes | |
| class | ADS::PermutedBuckets |
| a set of values below \( 2^w \), permuted by \( x \mapsto ( a x + c ) \bmod 2^w \) and split by the top bits of the images into buckets of \( L \) items on average More... | |
Functions | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::intersectMerge (const std::vector< T > &A, const std::vector< T > &B, C less=C()) |
| the items common to \( A \) and \( B \), by merging | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::intersectBinarySearch (const std::vector< T > &A, const std::vector< T > &B, C less=C()) |
| the items common to \( A \) and \( B \), by a binary search in the longer list of each item of the shorter one | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::intersectMutualPartition (const std::vector< T > &A, const std::vector< T > &B, C less=C()) |
| the items common to \( A \) and \( B \), by mutual partitions | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::intersectDoubling (const std::vector< T > &A, const std::vector< T > &B, C less=C()) |
| the items common to \( A \) and \( B \), by doubling searches | |
| template<class T, class C = std::less<>> | |
| std::vector< T > | ADS::intersectTwoLevel (const std::vector< T > &A, const std::vector< T > &B, std::size_t L, C less=C()) |
| the items common to \( A \) and \( B \), by the first items of the blocks of \( L \) items of \( A \) | |
| template<class T> | |
| void | ADS::twoLevelHeaders (const DiskFile< T > &A, std::size_t L, DiskFile< T > &H) |
| the first item of each block of \( L \) items of \( A \), into \( H \), by one scan | |
| template<class T, class C = std::less<>> | |
| void | ADS::intersectMergeExternal (const DiskFile< T > &A, const DiskFile< T > &B, DiskFile< T > &out, C less=C()) |
| the items common to the sorted files \( A \) and \( B \) into out, by merging, \( \lceil n / B \rceil + \lceil m / B \rceil \) reads | |
| template<class T, class C = std::less<>> | |
| void | ADS::intersectTwoLevelExternal (const DiskFile< T > &A, const DiskFile< T > &H, std::size_t L, const DiskFile< T > &B, DiskFile< T > &out, C less=C()) |
| the items common to the sorted files \( A \) and \( B \) into out, by the headers \( H \) of the blocks of \( L \) items of \( A \) [see twoLevelHeaders()] | |
The intersection of two sorted sets, the core of the resolution of a query of several words by an inverted index (Ferragina, chapter 6).
The definitions, the invariants and the costs of the area are in Theory of sets.
| std::vector< T > ADS::intersectBinarySearch | ( | const std::vector< T > & | A, |
| const std::vector< T > & | B, | ||
| C | less = C() ) |
the items common to \( A \) and \( B \), by a binary search in the longer list of each item of the shorter one
In pseudocode (Ferragina, chapter 6), \( X \) the longer list and \( Y \) the shorter:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersectBinarySearch}( A , B ) \cr 2 & \quad C \gets ( ) \cr 3 & \quad \textbf{foreach}\ y \in Y\ \textbf{do} \cr 4 & \qquad j \gets \text{the first place of } X \text{ not smaller than } y \text{, by a binary search} \cr 5 & \qquad \textbf{if}\ X[ j ] = y\ \textbf{then}\ \text{append } y \text{ to } C \cr 6 & \quad \textbf{return}\ C \end{array}} \]
| std::vector< T > ADS::intersectDoubling | ( | const std::vector< T > & | A, |
| const std::vector< T > & | B, | ||
| C | less = C() ) |
the items common to \( A \) and \( B \), by doubling searches
In pseudocode (Bentley and Yao, and Ferragina, chapter 6), \( X \) the longer list, of \( n \) items, and \( Y \) the shorter:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersectDoubling}( A , B ) \cr 2 & \quad i \gets 0;\ C \gets ( ) \cr 3 & \quad \textbf{foreach}\ y \in Y\ \textbf{do} \cr 4 & \qquad \ell \gets i;\ d \gets 1 \cr 5 & \qquad \textbf{while}\ i + d - 1 < n\ \textbf{and}\ X[ i + d - 1 ] < y\ \textbf{do}\ \ell \gets i + d;\ d \gets 2 d \cr 6 & \qquad j \gets \text{the first place of } X[ \ell .. \min( i + d , n ) ) \text{ not smaller than } y \cr 7 & \qquad \textbf{if}\ j < n\ \textbf{and}\ X[ j ] = y\ \textbf{then} \ \text{append } y \text{ to } C;\ j \gets j + 1 \cr 8 & \qquad i \gets j \cr 9 & \quad \textbf{return}\ C \end{array}} \]
| std::vector< T > ADS::intersectMerge | ( | const std::vector< T > & | A, |
| const std::vector< T > & | B, | ||
| C | less = C() ) |
the items common to \( A \) and \( B \), by merging
In pseudocode (Ferragina, chapter 6, and the procedure of section 8.3 of the book), each comparison passing at least one item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersectMerge}( A , B ) \cr 2 & \quad i \gets 0;\ j \gets 0;\ C \gets ( ) \cr 3 & \quad \textbf{while}\ i < n\ \textbf{and}\ j < m\ \textbf{do} \cr 4 & \qquad \textbf{if}\ A[ i ] < B[ j ]\ \textbf{then}\ i \gets i + 1 \cr 5 & \qquad \textbf{else if}\ B[ j ] < A[ i ]\ \textbf{then}\ j \gets j + 1 \cr 6 & \qquad \textbf{else}\ \text{append } A[ i ] \text{ to } C;\ i \gets i + 1;\ j \gets j + 1 \cr 7 & \quad \textbf{return}\ C \end{array}} \]
| void ADS::intersectMergeExternal | ( | const DiskFile< T > & | A, |
| const DiskFile< T > & | B, | ||
| DiskFile< T > & | out, | ||
| C | less = C() ) |
the items common to the sorted files \( A \) and \( B \) into out, by merging, \( \lceil n / B \rceil + \lceil m / B \rceil \) reads
In pseudocode (Ferragina, chapter 6), the merge of intersectMerge() on two readers and a writer of the disk:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{intersectMergeExternal}( A , B , \mathit{out} ) \cr 2 & \quad \textbf{while}\ \text{neither reader is at its end } \ \textbf{do} \cr 3 & \qquad \text{pass the smaller of the two items, or write the item } \text{to } \mathit{out} \text{ and pass both if they are equal} \end{array}} \]
| std::vector< T > ADS::intersectMutualPartition | ( | const std::vector< T > & | A, |
| const std::vector< T > & | B, | ||
| C | less = C() ) |
the items common to \( A \) and \( B \), by mutual partitions
In pseudocode (Baeza-Yates, and Ferragina, chapter 6), on the parts \( X[ a .. b ) \) and \( Y[ c .. d ) \), the result appended to \( C \) in increasing order:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{partition}( X , a , b , Y , c , d ) \cr 2 & \quad \textbf{if}\ a \ge b\ \textbf{or}\ c \ge d\ \textbf{then return} \cr 3 & \quad \textbf{if}\ d - c > b - a\ \textbf{then} \ \mathrm{partition}( Y , c , d , X , a , b );\ \textbf{return} \cr 4 & \quad k \gets \lfloor ( c + d ) / 2 \rfloor;\ j \gets \text{the first } \text{place of } X[ a .. b ) \text{ not smaller than } Y[ k ] \cr 5 & \quad \mathrm{partition}( X , a , j , Y , c , k ) \cr 6 & \quad \textbf{if}\ j < b\ \textbf{and}\ X[ j ] = Y[ k ] \ \textbf{then}\ \text{append } Y[ k ] \text{ to } C;\ j \gets j + 1 \cr 7 & \quad \mathrm{partition}( X , j , b , Y , k + 1 , d ) \cr 8 & \textbf{function}\ \mathrm{intersectMutualPartition}( A , B ) \cr 9 & \quad C \gets ( );\ \mathrm{partition}( A , 0 , n , B , 0 , m ); \ \textbf{return}\ C \end{array}} \]
| std::vector< T > ADS::intersectTwoLevel | ( | const std::vector< T > & | A, |
| const std::vector< T > & | B, | ||
| std::size_t | L, | ||
| C | less = C() ) |
the items common to \( A \) and \( B \), by the first items of the blocks of \( L \) items of \( A \)
In pseudocode (Ferragina, chapter 6), \( A' \) the first items of the blocks, \( h \) the block reached and \( p \) the place reached in \( A \), both only going forward:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersectTwoLevel}( A , B , L ) \cr 2 & \quad A' \gets ( A[ 0 ] , A[ L ] , A[ 2 L ] , \ldots );\ h \gets 0; \ p \gets 0;\ C \gets ( ) \cr 3 & \quad \textbf{foreach}\ b \in B\ \textbf{with}\ b \ge A'[ 0 ] \ \textbf{do} \cr 4 & \qquad \textbf{while}\ h + 1 < | A' |\ \textbf{and}\ A'[ h + 1 ] \le b\ \textbf{do}\ h \gets h + 1 \cr 5 & \qquad p \gets \max( p , h L );\ e \gets \min( ( h + 1 ) L , n ) \cr 6 & \qquad \textbf{while}\ p < e\ \textbf{and}\ A[ p ] < b \ \textbf{do}\ p \gets p + 1 \cr 7 & \qquad \textbf{if}\ p < e\ \textbf{and}\ A[ p ] = b\ \textbf{then} \ \text{append } b \text{ to } C \cr 8 & \quad \textbf{return}\ C \end{array}} \]
| void ADS::intersectTwoLevelExternal | ( | const DiskFile< T > & | A, |
| const DiskFile< T > & | H, | ||
| std::size_t | L, | ||
| const DiskFile< T > & | B, | ||
| DiskFile< T > & | out, | ||
| C | less = C() ) |
the items common to the sorted files \( A \) and \( B \) into out, by the headers \( H \) of the blocks of \( L \) items of \( A \) [see twoLevelHeaders()]
In pseudocode (Ferragina, chapter 6), intersectTwoLevel() with \( H \) read in order and \( A \) read only in the blocks met:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{intersectTwoLevelExternal}( A , H , L , B , \mathit{out} ) \cr 2 & \quad \textbf{foreach}\ b \in B\ \textbf{with}\ b \ge H[ 0 ] \ \textbf{do} \cr 3 & \qquad h \gets \text{the last block with } H[ h ] \le b \text{, } \text{reading } H \text{ forward} \cr 4 & \qquad \text{scan the block } h \text{ of } A \text{ forward from the } \text{place reached, up to } b \cr 5 & \qquad \textbf{if}\ \text{it holds } b\ \textbf{then}\ \text{write } b \text{ to } \mathit{out} \end{array}} \]