ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DynamicPGMIndex.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File DynamicPGMIndex.h -------------------------*/
3/*--------------------------------------------------------------------------*/
87/*--------------------------------------------------------------------------*/
88/*----------------------------- DEFINITIONS --------------------------------*/
89/*--------------------------------------------------------------------------*/
90
91#ifndef __ADS_DynamicPGMIndex
92 #define __ADS_DynamicPGMIndex
93/* self-identification: #endif constant */
94
95/*--------------------------------------------------------------------------*/
96/*------------------------------ INCLUDES ----------------------------------*/
97/*--------------------------------------------------------------------------*/
98
99#include "ADS/Contract.h"
101#include "ADS/memory/Prefetch.h"
102#include "ADS/search/PGMIndex.h"
104
105#include <algorithm>
106#include <cstddef>
107#include <cstdint>
108#include <optional>
109#include <type_traits>
110#include <utility>
111#include <vector>
112
113/*--------------------------------------------------------------------------*/
114/*--------------------------- NAMESPACE ------------------------------------*/
115/*--------------------------------------------------------------------------*/
116
117namespace ADS
118{
119
120/*--------------------------------------------------------------------------*/
121/*------------------------- CLASS DynamicPGMIndex --------------------------*/
122/*--------------------------------------------------------------------------*/
123
128template< class K , class V , std::size_t E = 16 , std::size_t B = 8 ,
129 std::size_t I = ( std::size_t( 1 ) << 24 ) >
130 requires std::is_arithmetic_v< K >
132{
133
134 static_assert( B >= 2 , "the ratio of the levels is at least 2" );
135
136/*--------------------------------------------------------------------------*/
137 public:
138 using key_type = K;
139 using mapped_type = V;
140
142 static constexpr std::size_t kBuffer = 128;
143
146 static constexpr std::size_t kIndexed = I;
147
149 static constexpr std::size_t kFiltered = std::size_t( 1 ) << 12;
150
152 DynamicPGMIndex( void ) = default;
153
156 explicit DynamicPGMIndex( const std::vector< std::pair< K , V > > & P );
157
183 void insert( const K & k , const V & v );
184
186 void erase( const K & k );
187
209 [[nodiscard]] const V * find( const K & k ) const;
210
212 [[nodiscard]] bool contains( const K & k ) const;
213
234 [[nodiscard]] std::optional< std::pair< K , V > >
235 lowerBound( const K & k ) const;
236
239 template< class F >
240 void forEach( const K & lo , const K & hi , F && f ) const;
241
244 [[nodiscard]] std::size_t entries( void ) const;
245
247 [[nodiscard]] std::size_t levels( void ) const;
248
249/*--------------------------------------------------------------------------*/
250 private:
253 struct Level
254 {
255 std::vector< K > key;
257 bool indexed = false;
258 std::vector< V > val;
259 std::vector< K > dead;
261 bool filtered = false;
262 std::size_t room = 0;
263
265 [[nodiscard]] std::size_t size( void ) const {
266 return( indexed ? idx.size() : key.size() );
267 }
269 [[nodiscard]] const K & at( std::size_t i ) const {
270 return( indexed ? idx[ i ] : key[ i ] );
271 }
273 [[nodiscard]] const K * keys( void ) const {
274 return( indexed ? ( idx.empty() ? nullptr : &idx[ 0 ] ) : key.data() );
275 }
278 [[nodiscard]] bool isDead( const K & k ) const {
279 return( ! dead.empty() &&
280 std::binary_search( dead.begin() , dead.end() , k ) );
281 }
285 [[nodiscard]] std::size_t lowerBound( const K & k ) const;
286 };
287
290 struct Run
291 {
292 std::vector< K > key;
293 std::vector< V > val;
294 std::vector< K > dead;
295 };
296
298 [[nodiscard]] static std::size_t capacity( std::size_t i );
299
302 [[nodiscard]] std::size_t bufferBound( const K & k ) const;
303
306 void put( const K & k , const V & v , bool dead );
307
312 static void merge( const Run & M , const Level & L , Run & T );
313
316 [[nodiscard]] static std::uint64_t hashOf( const K & k );
317
320 template< class F >
321 void scan( const K & lo , F && f ) const;
322
323 Run f_buf;
324 std::vector< Level > f_lev;
325 Run f_run[ 2 ];
326
327 }; // end( class DynamicPGMIndex )
328
329/*--------------------------------------------------------------------------*/
330
331} // end( namespace ADS )
332
333/*--------------------------------------------------------------------------*/
334/*-------------------------- IMPLEMENTATION --------------------------------*/
335/*--------------------------------------------------------------------------*/
336
337#include "ADS/search/impl/DynamicPGMIndex.tpp"
338
339/*--------------------------------------------------------------------------*/
340
341#endif /* DynamicPGMIndex.h included */
342
343/*--------------------------------------------------------------------------*/
344/*----------------------- End File DynamicPGMIndex.h -----------------------*/
345/*--------------------------------------------------------------------------*/
The blocked filter of Bloom, beyond the book: the engineered filter of ADS [see BloomFilter....
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7....
The piecewise geometric model index of Ferragina and Vinciguerra, beyond the book: a learned index on...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
V mapped_type
the type of the values
Definition DynamicPGMIndex.h:139
DynamicPGMIndex(const std::vector< std::pair< K, V > > &P)
the map of the pairs , put in the one level that holds them
const V * find(const K &k) const
the value of , nullptr if absent; valid until the next insertion or removal
std::size_t entries(void) const
the pairs stored, the tombstones and the pairs hidden by newer ones included
void forEach(const K &lo, const K &hi, F &&f) const
calls on the pairs with , by increasing key, by the merge of lowerBound()
static constexpr std::size_t kFiltered
the least capacity of a level with a filter of its keys
Definition DynamicPGMIndex.h:149
void erase(const K &k)
removes , if there, by the insertion of a tombstone
DynamicPGMIndex(void)=default
an empty map
std::optional< std::pair< K, V > > lowerBound(const K &k) const
the pair of the least key , none if there is none
K key_type
the type of the keys
Definition DynamicPGMIndex.h:138
bool contains(const K &k) const
whether is there, by find()
static constexpr std::size_t kIndexed
the least capacity of a level with a PGM-index, , the smaller ones being searched by a binary search ...
Definition DynamicPGMIndex.h:146
void insert(const K &k, const V &v)
maps to , whether was there or not
std::size_t levels(void) const
the levels below the buffer, empty or not
static constexpr std::size_t kBuffer
the pairs in the buffer,
Definition DynamicPGMIndex.h:142
the PGM-index on a sorted array of arithmetic keys K, with the error at the last level and at the o...
Definition PGMIndex.h:124
bool empty(void) const
true if there are no items
std::size_t size(void) const
the number of the items
std::size_t lowerBound(const std::vector< T > &A, const T &v, std::size_t lo, std::size_t hi, C less=C())
the first place in of an item not smaller than , if none
SortedListSet< T > merge(const SortedListSet< T > &A, const SortedListSet< T > &B, bool a, bool b, bool both)
the merge of the sorted lists of and : an item of only, of only or of both goes into the result if...
Definition ADT.h:96