|
ADS
Algorithms and Data Structures
|
The piecewise geometric model index of Ferragina and Vinciguerra, beyond the book: a learned index on a sorted array of arithmetic keys, which finds the place of the first item not smaller than a key [see StaticSearch.h for the static B+-tree] in a space that depends on the regularity of the keys rather than on their number. More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/Wide.h"#include "ADS/external/MappedFile.h"#include "ADS/memory/HugePageAllocator.h"#include "ADS/memory/Prefetch.h"#include <bit>#include <cstddef>#include <cstdint>#include <string_view>#include <type_traits>#include <vector>#include "ADS/search/impl/PGMIndex.tpp"Go to the source code of this file.
Classes | |
| class | ADS::PGMIndex< K, E, R > |
| the PGM-index on a sorted array of arithmetic keys K, with the error \( \epsilon = E \) at the last level and \( \epsilon_r = R \) at the others More... | |
Namespaces | |
| namespace | ADS |
The piecewise geometric model index of Ferragina and Vinciguerra, beyond the book: a learned index on a sorted array of arithmetic keys, which finds the place of the first item not smaller than a key [see StaticSearch.h for the static B+-tree] in a space that depends on the regularity of the keys rather than on their number.
The items \( A[ 0 .. n ) \) are the points \( ( A[ i ] , i ) \) of the plane, a repeated key standing for its first place. An \( \epsilon \)-approximation is a sequence of segments, each a line \( f( x ) = a + b ( x - k_s ) \) over a range of keys from its first key \( k_s \), such that \( | f( A[ i ] ) - i | \le \epsilon \) for every key of its range; the segments are those of the optimal approximation, the fewest possible, built in one pass in \( O( n ) \) time by the algorithm of O'Rourke as engineered by Xie et al.: the lines through the points of the range with error at most \( \epsilon \) are those between the line of least slope and the line of largest slope that the convex hulls of the points moved up and down by \( \epsilon \) admit, the two hulls updated by a new point in amortized \( O( 1 ) \), and a range ends when a point leaves no line. The segment keeps the line of the average of the two extreme slopes through the point where they cross (any line of slope between them through that point stays within the error), its slope made not negative so that the prediction never decreases. The first keys of the segments are themselves approximated, with an error \( \epsilon_r \), by the segments of a level above, and so on up to a single segment: for \( m \) segments at the level below, a level has at most \( \lceil m / ( 2 \epsilon_r + 1 ) \rceil \) of them (each segment of the optimal approximation but the last covers points whose first places span at least \( 2 \epsilon + 1 \) places, since the horizontal line through the middle of any \( 2 \epsilon + 1 \) consecutive places is within the error), the levels are \( 1 + \lceil \log_{2 \epsilon_r + 1} m \rceil \) at most, and the index takes 28 bytes per segment (its first key, the two doubles of its line and its first place), \( m \) at most \( \lceil n / ( 2 \epsilon + 1 ) \rceil \) and much fewer on regular keys.
A search starts from the segment at the top, and at each level the prediction of its line, clamped to the range of the segment, is the place of the next segment within \( \pm ( \epsilon_r + 1 ) \) places, and the window searched has \( \epsilon_r + 2 \) on each side (one more for the rounding of the line in double), by a binary search with no branch of the last first key not larger than the key; at the last level the prediction is the place of the answer within \( \pm ( \epsilon + 2 ) \), whose lines of cache are all prefetched at once, so that their misses overlap, and searched in the same way: \( O( \log \epsilon_r \log_{2 \epsilon_r} m + \log \epsilon ) \) time. The items are a copy on huge pages [see HugePageAllocator.h], so that the window of a search costs no miss of the TLB of its own. On repeated keys, whose places a line cannot all approximate, the search goes on past the window by galloping. The keys are mapped to integers of 64 bits in the same order (the signed by flipping the top bit, the floating by their bits), so that the hulls are kept with exact products of 128 bits.
The contracts [see Contract.h]: the keys sorted, an audit; \( \epsilon \ge 1 \), \( \epsilon_r \ge 1 \); each result \( r \) is such that the items before \( r \) are smaller than the key and those from \( r \) on are not, a postcondition. The functions are defined in PGMIndex.tpp.