ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LAVector.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File LAVector.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
89/*--------------------------------------------------------------------------*/
90/*----------------------------- DEFINITIONS --------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93#ifndef __ADS_LAVector
94 #define __ADS_LAVector
95/* self-identification: #endif constant */
96
97/*--------------------------------------------------------------------------*/
98/*------------------------------ INCLUDES ----------------------------------*/
99/*--------------------------------------------------------------------------*/
100
101#include "ADS/Contract.h"
103#include "ADS/search/PGMIndex.h"
105
106#include <bit>
107#include <cstddef>
108#include <cstdint>
109#include <cstring>
110#include <string_view>
111#include <utility>
112#include <vector>
113
114/*--------------------------------------------------------------------------*/
115/*--------------------------- NAMESPACE ------------------------------------*/
116/*--------------------------------------------------------------------------*/
117
118namespace ADS
119{
120
121/*--------------------------------------------------------------------------*/
122/*---------------------------- CLASS LAVector ------------------------------*/
123/*--------------------------------------------------------------------------*/
124
128{
129
130/*--------------------------------------------------------------------------*/
131 public:
133 LAVector( void ) = default;
134
159 explicit LAVector( const std::vector< std::uint64_t > & x , int c = 7 );
160
181 [[nodiscard]] static LAVector tuned( const std::vector< std::uint64_t > & x );
182
184 [[nodiscard]] std::size_t size( void ) const;
185
187 [[nodiscard]] bool empty( void ) const;
188
190 [[nodiscard]] int correctionBits( void ) const;
191
193 [[nodiscard]] std::size_t segments( void ) const;
194
208 [[nodiscard]] std::uint64_t access( std::size_t i ) const;
209
212 [[nodiscard]] std::uint64_t operator[]( std::size_t i ) const;
213
217 [[nodiscard]] std::uint64_t select( std::size_t k ) const;
218
246 [[nodiscard]] std::size_t nextGEQ( std::uint64_t x ) const;
247
249 [[nodiscard]] std::size_t rank( std::uint64_t x ) const;
250
252 [[nodiscard]] bool contains( std::uint64_t x ) const;
253
255 [[nodiscard]] std::size_t correctionsInBits( void ) const;
256
258 [[nodiscard]] std::size_t sizeInBits( void ) const;
259
261 [[nodiscard]] std::vector< std::uint64_t > decode( void ) const;
262
263/*----------------------------- THE PERSISTENCE ----------------------------*/
264
266 static constexpr std::string_view persistName = "LAVector";
267
270 template< class A >
271 void persist( A & a );
272
273/*------------------------- THE STANDARD INTERFACE -------------------------*/
274
276 using value_type = std::uint64_t;
278 using size_type = std::size_t;
280 using difference_type = std::ptrdiff_t;
282 using reference = std::uint64_t;
284 using const_reference = std::uint64_t;
289
291 [[nodiscard]] const_iterator begin( void ) const {
292 return( const_iterator( this , 0 ) );
293 }
294
295 [[nodiscard]] const_iterator end( void ) const {
296 return( const_iterator( this , f_n ) );
297 }
298
299 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
301 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
302
303/*--------------------------------------------------------------------------*/
304 private:
307 struct Segment
308 {
309 std::uint64_t base;
310 double slope;
311 std::uint64_t ow;
312 using persist_unit = std::uint64_t;
313 };
314
318 struct Packed
319 {
320 std::vector< std::uint64_t > words;
321 int width = 0;
322 std::uint64_t mask = 0;
323 std::size_t bits = 0;
324
326 template< class A >
327 void persist( A & a ) {
328 a( words , width , mask , bits );
329 a.require( ( width >= 0 ) && ( width <= 64 ) &&
330 ( words.size() * 64 >= bits ) ,
331 "the integers of a LAVector" );
332 }
333
335 void pack( const std::vector< std::uint64_t > & v ) {
336 std::uint64_t top = 0;
337 for( const auto x : v )
338 top = std::max( top , x );
339 width = top == 0 ? 0 : lambda( top ) + 1;
340 ADS_PRE( width <= 56 );
341 mask = ( std::uint64_t( 1 ) << width ) - 1;
342 bits = v.size() * std::size_t( width );
343 words.assign( bits / 64 + 2 , 0 );
344 for( std::size_t i = 0 , b = 0 ; i < v.size() ; ++i , b += width ) {
345 words[ b / 64 ] |= v[ i ] << ( b % 64 );
346 if( b % 64 + std::size_t( width ) > 64 )
347 words[ b / 64 + 1 ] |= v[ i ] >> ( 64 - b % 64 );
348 }
349 }
352 [[nodiscard]] std::pair< std::size_t , std::size_t >
353 pair( std::size_t i ) const {
354 if constexpr( std::endian::native == std::endian::little )
355 if( width <= 28 ) {
356 const std::size_t b = i * std::size_t( width );
357 std::uint64_t x;
358 std::memcpy(
359 &x , reinterpret_cast< const unsigned char * >( words.data() ) + b / 8 ,
360 8 );
361 x >>= b % 8;
362 return( std::make_pair( std::size_t( x & mask ) ,
363 std::size_t( ( x >> width ) & mask ) ) );
364 }
365 return( std::make_pair( ( *this )[ i ] , ( *this )[ i + 1 ] ) );
366 }
368 [[nodiscard]] std::size_t operator[]( std::size_t i ) const {
369 const std::size_t b = i * std::size_t( width );
370 if constexpr( std::endian::native == std::endian::little ) {
371 std::uint64_t x;
372 std::memcpy(
373 &x , reinterpret_cast< const unsigned char * >( words.data() ) + b / 8 ,
374 8 );
375 return( std::size_t( ( x >> ( b % 8 ) ) & mask ) );
376 }
377 else {
378 const std::size_t o = b % 64;
379 return( std::size_t( ( ( words[ b / 64 ] >> o ) |
380 ( ( words[ b / 64 + 1 ] << 1 ) << ( 63 - o ) ) ) &
381 mask ) );
382 }
383 }
384 };
385
391 [[nodiscard]] static int widest( const std::vector< std::uint64_t > & x );
392
394 [[nodiscard]] static std::int64_t error( int c );
395
398 void append( const std::vector< std::uint64_t > & x , std::size_t f ,
399 std::size_t l , double a );
400
402 void finish( const std::vector< std::uint64_t > & x );
403
406 [[nodiscard]] static std::uint64_t whole( double v );
407
409 [[nodiscard]] std::size_t segmentOf( std::size_t i , std::size_t & p ) const;
410
412 [[nodiscard]] std::uint64_t item( std::size_t j , std::size_t d ) const;
413
414 std::size_t f_n = 0;
415 int f_c = 0;
416 BitVector f_C;
417 std::vector< Segment > f_seg;
418 Packed f_p;
419 std::vector< std::uint64_t > f_first;
420 Packed f_J;
421 int f_t = 0;
422 Packed f_V;
423 int f_l = 0;
424 std::uint64_t f_x0 = 0;
425 std::uint64_t f_last = 0;
426
427 }; // end( class LAVector )
428
429/*--------------------------------------------------------------------------*/
430
431} // end( namespace ADS )
432
433/*--------------------------------------------------------------------------*/
434/*-------------------------- IMPLEMENTATION --------------------------------*/
435/*--------------------------------------------------------------------------*/
436
437#include "ADS/succinct/impl/LAVector.tpp"
438
439/*--------------------------------------------------------------------------*/
440
441#endif /* LAVector.h included */
442
443/*--------------------------------------------------------------------------*/
444/*-------------------------- End File LAVector.h ---------------------------*/
445/*--------------------------------------------------------------------------*/
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
The broadword tricks of Knuth (section 7.1.3 of The Art of Computer Programming), beyond the book: op...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The piecewise geometric model index of Ferragina and Vinciguerra, beyond the book: a learned index on...
SubscriptIterator< LAVector, std::uint64_t > const_iterator
the constant iterator, random access
Definition LAVector.h:286
std::size_t nextGEQ(std::uint64_t x) const
the place of the first item , size() if there is none, i.e., the number of items ,...
const_iterator cend(void) const
the same as end()
Definition LAVector.h:301
const_iterator begin(void) const
the iterator on the first item
Definition LAVector.h:291
std::ptrdiff_t difference_type
the type of the distance of two places
Definition LAVector.h:280
std::size_t size_type
the type of a size
Definition LAVector.h:278
bool contains(std::uint64_t x) const
whether is an item, by nextGEQ()
LAVector(const std::vector< std::uint64_t > &x, int c=7)
the sequence with corrections of bits, i.e., the error (0 for )
std::size_t segments(void) const
the number of segments,
std::size_t size(void) const
the number of items,
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition LAVector.h:266
bool empty(void) const
whether there is no item
std::uint64_t const_reference
an item, returned by value
Definition LAVector.h:284
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
std::uint64_t reference
an item, returned by value
Definition LAVector.h:282
std::vector< std::uint64_t > decode(void) const
the items, decoded in time
LAVector(void)=default
the empty sequence
std::uint64_t select(std::size_t k) const
the -th smallest item, i.e., access( k - 1 ), the select of the vector of bits whose 1s are the items
const_iterator end(void) const
the iterator past the last item
Definition LAVector.h:295
std::size_t rank(std::uint64_t x) const
the number of items , as nextGEQ()
const_iterator cbegin(void) const
the same as begin()
Definition LAVector.h:299
int correctionBits(void) const
the bits of the corrections asked for
std::uint64_t access(std::size_t i) const
the item
std::uint64_t operator[](std::size_t i) const
the item , as access()
const_iterator iterator
the same as const_iterator
Definition LAVector.h:288
std::size_t correctionsInBits(void) const
the bits of the corrections
std::size_t sizeInBits(void) const
all the bits: the corrections, the segments and the tables
std::uint64_t value_type
the type of an item
Definition LAVector.h:276
static LAVector tuned(const std::vector< std::uint64_t > &x)
the sequence with the of the fewest bits, among the odd ones up to of widest() and the two around ...
the random-access constant iterator on the items T of the owner O, read by ( *o )[ i ]
Definition BitVector.h:372
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
constexpr int lambda(Word x)
the place of the leading 1 of ,
Definition ADT.h:96
a segment of the array and its sum, with the place first and the length
Definition MaximumSegments.h:131