ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RankSelect.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File RankSelect.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
126/*--------------------------------------------------------------------------*/
127/*----------------------------- DEFINITIONS --------------------------------*/
128/*--------------------------------------------------------------------------*/
129
130#ifndef __ADS_RankSelect
131 #define __ADS_RankSelect
132/* self-identification: #endif constant */
133
134/*--------------------------------------------------------------------------*/
135/*------------------------------ INCLUDES ----------------------------------*/
136/*--------------------------------------------------------------------------*/
137
138#include "ADS/Contract.h"
139#include "ADS/Portable.h"
144
145#include <algorithm>
146#include <atomic>
147#include <bit>
148#include <cstddef>
149#include <cstdint>
150#include <limits>
151#include <memory>
152#include <mutex>
153#include <span>
154#include <string_view>
155#include <type_traits>
156#include <utility>
157#include <vector>
158
159/*--------------------------------------------------------------------------*/
160/*--------------------------- NAMESPACE ------------------------------------*/
161/*--------------------------------------------------------------------------*/
162
163namespace ADS
164{
165
166/*--------------------------------------------------------------------------*/
167/*------------------------- CLASS BasicRankSelect --------------------------*/
168/*--------------------------------------------------------------------------*/
169
173{
175 enum class Selects {
180 };
181 };
182
186template< bool Lazy >
188{
189
190/*--------------------------------------------------------------------------*/
191 public:
194
220
224 BasicRankSelect( std::span< const Word > w , std::size_t n ,
226
254 template< Executor E >
256
260 template< Executor E >
261 BasicRankSelect( E & ex , std::span< const Word > w , std::size_t n ,
263
266
269
272
275
277 ~BasicRankSelect( void ) = default;
278
280 [[nodiscard]] const BitVector & bits( void ) const;
281
283 [[nodiscard]] std::size_t size( void ) const;
284
287 [[nodiscard]] bool operator[]( std::size_t i ) const;
288
290 [[nodiscard]] std::size_t ones( void ) const;
291
293 [[nodiscard]] std::size_t zeros( void ) const;
294
315 [[nodiscard]] std::size_t rank1( std::size_t i ) const;
316
320 [[nodiscard]] std::size_t rank0( std::size_t i ) const;
321
351 [[nodiscard]] std::size_t select1( std::size_t k ) const;
352
358 [[nodiscard]] std::size_t select0( std::size_t k ) const;
359
362 [[nodiscard]] std::size_t directoryBits( void ) const;
363
364/*----------------------------- THE PERSISTENCE ----------------------------*/
365
367 static constexpr std::string_view persistName =
368 Lazy ? "LazyRankSelect" : "RankSelect";
369
371 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
372 return( detail::persistMix(
373 { kBlock , std::uint64_t( kRel ) , std::uint64_t( kTop ) , kGroup } ) );
374 }
375
378 template< class A >
379 void persist( A & a );
380
381/*------------------------- THE STANDARD INTERFACE -------------------------*/
382
384 using value_type = bool;
386 using size_type = std::size_t;
388 using difference_type = std::ptrdiff_t;
390 using reference = bool;
392 using const_reference = bool;
397
399 [[nodiscard]] const_iterator begin( void ) const {
400 return( const_iterator( this , 0 ) );
401 }
402
403 [[nodiscard]] const_iterator end( void ) const {
404 return( const_iterator( this , size() ) );
405 }
406
407 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
409 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
410
411/*--------------------------------------------------------------------------*/
412 private:
414 static constexpr std::size_t kBlock = 256;
415 static constexpr int kRel = 8;
416 static constexpr std::size_t kQuarter = kBlock / 4;
417 static constexpr std::uint64_t kRelMask = ( std::uint64_t( 1 ) << kRel ) - 1;
420#ifndef ADS_RANKSELECT_TOP
421 static constexpr int kTop = 64 - 3 * kRel;
422#else
423 static constexpr int kTop = ADS_RANKSELECT_TOP; // a test of the top level
424#endif
427#ifndef ADS_RANKSELECT_FUSE
428 static constexpr std::size_t kFuse = std::size_t( 1 ) << 23;
429#else
430 static constexpr std::size_t kFuse = ADS_RANKSELECT_FUSE; // a test
431#endif
432
434#ifndef ADS_RANKSELECT_CHUNK
435 static constexpr std::size_t kChunk = 4096;
436#else
437 static constexpr std::size_t kChunk = ADS_RANKSELECT_CHUNK; // a test
438#endif
439
442 static constexpr std::size_t kGroup = 4096;
443 static constexpr std::size_t kSparse = std::size_t( 1 ) << 16;
444
451 struct Inventory
452 {
453 int l = 0 ,
454 g = 0 ,
455 s = 0;
457 std::vector< std::uint64_t , MappableAllocator< std::uint64_t > > pos;
459 std::vector< std::uint16_t , detail::LineAllocator< std::uint16_t > > off;
460
462 template< class A >
463 void persist( A & a ) {
464 a( l , g , s , pos , off );
465 }
466 };
467
469 [[nodiscard]] std::size_t before( std::size_t t , bool b ) const;
470
473 static Word entry( Word x0 , Word x1 , Word x2 , Word x3 , std::uint64_t & c ,
474 std::uint64_t top );
475
478 void directory( Selects s , const Word * w );
479
482 void shape( bool b , Inventory & I , std::size_t & groups ) const;
483
485 const Inventory & inventory( bool b ) const;
486
488 void build( bool b ) const;
489
492 template< Executor E >
493 void directory( E & ex , Selects s , const Word * w );
494
497 template< Executor E >
498 void build( E & ex , bool b ) const;
499
502 template< class F >
503 void samples( bool b , std::size_t t0 , std::size_t t1 , int e ,
504 std::size_t total , F && f ) const;
505
507 void first( bool b ) const;
508
510 void adopt( const BasicRankSelect & o , bool move );
511
513 [[nodiscard]] std::size_t select( std::size_t k , bool b ) const;
514
517 [[nodiscard]] std::size_t selectSparse( std::size_t k , bool b ,
518 std::size_t p ,
519 std::size_t q ) const;
520
522 [[nodiscard]] bool consistent( void ) const;
523
526 [[nodiscard]] bool fits( void ) const;
527 [[nodiscard]] bool fits( bool b ) const;
528
529 BitVector f_b;
533 std::vector< std::uint64_t , detail::LineAllocator< std::uint64_t > > f_dir;
535 std::vector< std::uint64_t > f_top;
536 bool f_big = false;
537 mutable Inventory f_inv[ 2 ];
538 bool f_has[ 2 ] = { true , true };
539 std::size_t f_ones = 0;
542 struct Once
543 {
544 mutable std::once_flag flag[ 2 ];
546 mutable std::atomic< bool > ready[ 2 ] = { false , false };
547 };
548 struct None
549 { };
550 ADS_NO_UNIQUE_ADDRESS std::conditional_t< Lazy , Once , None > f_once;
551
552 }; // end( class BasicRankSelect )
553
557
562
563/*--------------------------------------------------------------------------*/
564
565} // end( namespace ADS )
566
567/*--------------------------------------------------------------------------*/
568/*-------------------------- IMPLEMENTATION --------------------------------*/
569/*--------------------------------------------------------------------------*/
570
571#include "ADS/succinct/impl/RankSelect.tpp"
572
573/*--------------------------------------------------------------------------*/
574
575#endif /* RankSelect.h included */
576
577/*--------------------------------------------------------------------------*/
578/*------------------------- End File RankSelect.h --------------------------*/
579/*--------------------------------------------------------------------------*/
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 layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
A file mapped in memory, shared by the structures that are read from it in place [see Persistence....
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
BasicRankSelect(void)
the directory of no bit
BasicRankSelect(const BasicRankSelect &o)
a copy, with the inventories that has built
const_iterator cbegin(void) const
the same as begin()
Definition RankSelect.h:407
SubscriptIterator< BasicRankSelect, bool > const_iterator
the constant iterator, random access
Definition RankSelect.h:394
std::size_t size_type
the type of a size
Definition RankSelect.h:386
BasicRankSelect(BitVector b, Selects s=Selects::BOTH)
the directory of the bits , which it keeps, with the selects
const_iterator cend(void) const
the same as end()
Definition RankSelect.h:409
~BasicRankSelect(void)=default
the directory and its bits released
static constexpr std::uint64_t persistTag(void)
the parameters of the layout of the directory, in a file
Definition RankSelect.h:371
static constexpr std::string_view persistName
Definition RankSelect.h:367
const_iterator iterator
the same as const_iterator
Definition RankSelect.h:396
const_iterator end(void) const
the iterator past the last item
Definition RankSelect.h:403
std::size_t rank0(std::size_t i) const
the number of 0s in the places , i.e.,
BasicRankSelect & operator=(const BasicRankSelect &o)
a copy, with the inventories that has built
std::size_t ones(void) const
the number of 1s
std::ptrdiff_t difference_type
the type of the distance of two places
Definition RankSelect.h:388
BasicRankSelect(BasicRankSelect &&o) noexcept
a move, with the inventories that has built
std::size_t zeros(void) const
the number of 0s
std::size_t select1(std::size_t k) const
the place of the -th 1
BasicRankSelect(E &ex, BitVector b, Selects s=Selects::BOTH)
the directory of BasicRankSelect( b , s ) above, the same entries and inventories,...
const BitVector & bits(void) const
the bits
bool value_type
the type of an item
Definition RankSelect.h:384
const_iterator begin(void) const
the iterator on the first item
Definition RankSelect.h:399
bool operator[](std::size_t i) const
the bit
bool const_reference
an item, returned by value
Definition RankSelect.h:392
std::size_t size(void) const
the number of bits,
std::size_t directoryBits(void) const
the bits of the directory, beyond those of the vector, with those of the inventories of the selects a...
BasicRankSelect(std::span< const Word > w, std::size_t n, Selects s=Selects::BOTH)
the directory of the bits of the words , copied as they are counted, with the selects
std::size_t rank1(std::size_t i) const
the number of 1s in the places
bool reference
an item, returned by value
Definition RankSelect.h:390
std::size_t select0(std::size_t k) const
the place of the -th 0, by the select of select1() above on the complemented words
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
BasicRankSelect & operator=(BasicRankSelect &&o) noexcept
a move, with the inventories that has built
BasicRankSelect(E &ex, std::span< const Word > w, std::size_t n, Selects s=Selects::BOTH)
the directory of BasicRankSelect( w , n , s ) above, the words copied by the chunks that count them,...
a vector of bits packed in words of 64 bits
Definition BitVector.h:479
the random-access constant iterator on the items T of the owner O, read by ( *o )[ i ]
Definition BitVector.h:372
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
BasicRankSelect< false > RankSelect
the directory with its inventories of select built in the constructor, the fastest select
Definition RankSelect.h:556
BasicRankSelect< true > LazyRankSelect
the directory with its inventories of select built at the first select of each value,...
Definition RankSelect.h:561
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73
the values whose select a directory answers: a structure that needs only rank, or one of the selects,...
Definition RankSelect.h:173
Selects
the selects that a directory answers
Definition RankSelect.h:175
@ ZEROS
select0()
Definition RankSelect.h:178
@ BOTH
select1() and select0()
Definition RankSelect.h:179
@ NONE
no select, the rank only
Definition RankSelect.h:176
@ ONES
select1()
Definition RankSelect.h:177