ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CompactRankSelect.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File CompactRankSelect.h ------------------------*/
3/*--------------------------------------------------------------------------*/
77/*--------------------------------------------------------------------------*/
78/*----------------------------- DEFINITIONS --------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81#ifndef __ADS_CompactRankSelect
82 #define __ADS_CompactRankSelect
83/* self-identification: #endif constant */
84
85/*--------------------------------------------------------------------------*/
86/*------------------------------ INCLUDES ----------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89#include "ADS/Contract.h"
93
94#include <algorithm>
95#include <array>
96#include <bit>
97#include <cstddef>
98#include <cstdint>
99#include <limits>
100#include <span>
101#include <string_view>
102#include <vector>
103
104/*--------------------------------------------------------------------------*/
105/*--------------------------- NAMESPACE ------------------------------------*/
106/*--------------------------------------------------------------------------*/
107
108namespace ADS
109{
110
111/*--------------------------------------------------------------------------*/
112/*------------------------- CLASS CompactRankSelect ------------------------*/
113/*--------------------------------------------------------------------------*/
114
118{
119
120/*--------------------------------------------------------------------------*/
121 public:
124
127
131 CompactRankSelect( std::span< const Word > w , std::size_t n );
132
156 template< Executor E >
158
162 template< Executor E >
163 CompactRankSelect( E & ex , std::span< const Word > w , std::size_t n );
164
166 [[nodiscard]] const BitVector & bits( void ) const { return( f_b ); }
167
169 [[nodiscard]] std::size_t size( void ) const { return( f_b.size() ); }
170
173 [[nodiscard]] bool operator[]( std::size_t i ) const { return( f_b[ i ] ); }
174
176 [[nodiscard]] std::size_t ones( void ) const { return( f_ones ); }
177
179 [[nodiscard]] std::size_t zeros( void ) const {
180 return( f_b.size() - f_ones );
181 }
182
202 [[nodiscard]] std::size_t rank1( std::size_t i ) const;
203
207 [[nodiscard]] std::size_t rank0( std::size_t i ) const {
208 return( i - rank1( i ) );
209 }
210
235 [[nodiscard]] std::size_t select1( std::size_t k ) const;
236
241 [[nodiscard]] std::size_t select0( std::size_t k ) const;
242
244 [[nodiscard]] std::size_t directoryBits( void ) const;
245
246/*------------------------- THE STANDARD INTERFACE -------------------------*/
247
249 using value_type = bool;
251 using size_type = std::size_t;
253 using difference_type = std::ptrdiff_t;
255 using reference = bool;
257 using const_reference = bool;
262
264 [[nodiscard]] const_iterator begin( void ) const {
265 return( const_iterator( this , 0 ) );
266 }
267
268 [[nodiscard]] const_iterator end( void ) const {
269 return( const_iterator( this , size() ) );
270 }
271
272 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
274 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
275
276/*----------------------------- THE PERSISTENCE ----------------------------*/
277
279 static constexpr std::string_view persistName = "CompactRankSelect";
280
282 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
283 return(
284 detail::persistMix( { kSuper , kBlock , kSample ,
285 std::uint64_t( kTop ) } ) );
286 }
287
290 template< class A >
291 void persist( A & a );
292
293/*--------------------------------------------------------------------------*/
294 private:
295 static constexpr std::size_t kSuper = 4096;
296 static constexpr std::size_t kBlock = 512;
297 static constexpr std::size_t kSample = 4096;
298#ifndef ADS_COMPACT_TOP
299 static constexpr int kTop = 32;
300#else
301 static constexpr int kTop = ADS_COMPACT_TOP; // a test of the top level
302#endif
303
307 struct Entry
308 {
309 using persist_unit = std::uint64_t;
310 std::uint64_t lo = 0 ,
311 hi = 0;
312 };
313
316 [[nodiscard]] static Entry entry( const std::uint64_t ( &v )[ 9 ] ,
317 std::uint64_t c );
318
320#ifndef ADS_COMPACT_SCALAR
321 static constexpr bool kScalar = false;
322#else
323 static constexpr bool kScalar = true; // a test of the scalar kernel
324#endif
325
327 [[nodiscard]] static std::size_t inSuper( const Entry & e , std::size_t k ) {
328 const std::size_t s = 32 + 12 * k; // 32 , 44 , ... , 116
329 const std::uint64_t v =
330 s < 64 ? ( e.lo >> s ) | ( e.hi << ( 64 - s ) ) : e.hi >> ( s - 64 );
331 return( std::size_t( v & 0xFFF ) );
332 }
333
336#ifndef ADS_RANKSELECT_FUSE
337 static constexpr std::size_t kFuse = std::size_t( 1 ) << 23;
338#else
339 static constexpr std::size_t kFuse = ADS_RANKSELECT_FUSE; // a test
340#endif
341
344 void directory( const Word * w );
345
347#ifndef ADS_COMPACT_CHUNK
348 static constexpr std::size_t kChunk = 256;
349#else
350 static constexpr std::size_t kChunk = ADS_COMPACT_CHUNK; // a test
351#endif
352
355 template< Executor E >
356 void directory( E & ex , const Word * w );
357
359 void closeSamples( void );
360
362 [[nodiscard]] std::size_t before( std::size_t s , bool b ) const;
363
365 [[nodiscard]] std::size_t select( std::size_t k , bool b ) const;
366
368 [[nodiscard]] bool consistent( void ) const;
369
370 BitVector f_b;
372 std::vector< Entry , MappableAllocator< Entry > > f_e;
373 std::vector< std::uint64_t > f_top;
375 std::vector< std::uint32_t , MappableAllocator< std::uint32_t > > f_s[ 2 ];
376 std::size_t f_ones = 0;
377
378 }; // end( class CompactRankSelect )
379
380/*--------------------------------------------------------------------------*/
381
382} // end( namespace ADS )
383
384/*--------------------------------------------------------------------------*/
385/*-------------------------- IMPLEMENTATION --------------------------------*/
386/*--------------------------------------------------------------------------*/
387
388#include "ADS/succinct/impl/CompactRankSelect.tpp"
389
390/*--------------------------------------------------------------------------*/
391
392#endif /* CompactRankSelect.h included */
393
394/*--------------------------------------------------------------------------*/
395/*---------------------- End File CompactRankSelect.h ----------------------*/
396/*--------------------------------------------------------------------------*/
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
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....
a vector of bits packed in words of 64 bits
Definition BitVector.h:479
std::size_t ones(void) const
the number of 1s
Definition CompactRankSelect.h:176
const_iterator iterator
the same as const_iterator
Definition CompactRankSelect.h:261
const_iterator cend(void) const
the same as end()
Definition CompactRankSelect.h:274
std::size_t rank1(std::size_t i) const
the number of 1s in the places
bool operator[](std::size_t i) const
the bit
Definition CompactRankSelect.h:173
static constexpr std::uint64_t persistTag(void)
the parameters of the layout of the directory, in a file
Definition CompactRankSelect.h:282
const_iterator cbegin(void) const
the same as begin()
Definition CompactRankSelect.h:272
bool value_type
the type of an item
Definition CompactRankSelect.h:249
std::ptrdiff_t difference_type
the type of the distance of two places
Definition CompactRankSelect.h:253
const_iterator begin(void) const
the iterator on the first item
Definition CompactRankSelect.h:264
std::size_t size(void) const
the number of bits,
Definition CompactRankSelect.h:169
std::size_t rank0(std::size_t i) const
the number of 0s in the places , i.e.,
Definition CompactRankSelect.h:207
std::size_t zeros(void) const
the number of 0s
Definition CompactRankSelect.h:179
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
CompactRankSelect(BitVector b)
the directory of the bits , which it keeps
CompactRankSelect(E &ex, std::span< const Word > w, std::size_t n)
the directory of CompactRankSelect( w , n ) above, the words copied by the chunks that count them,...
const BitVector & bits(void) const
the bits
Definition CompactRankSelect.h:166
std::size_t size_type
the type of a size
Definition CompactRankSelect.h:251
const_iterator end(void) const
the iterator past the last item
Definition CompactRankSelect.h:268
std::size_t select1(std::size_t k) const
the place of the -th 1
bool reference
an item, returned by value
Definition CompactRankSelect.h:255
SubscriptIterator< CompactRankSelect, bool > const_iterator
the constant iterator, random access
Definition CompactRankSelect.h:259
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition CompactRankSelect.h:279
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
bool const_reference
an item, returned by value
Definition CompactRankSelect.h:257
CompactRankSelect(E &ex, BitVector b)
the directory of CompactRankSelect( b ) above, the same entries and samples, built on the threads of ...
std::size_t directoryBits(void) const
the bits of the directory, beyond those of the vector
CompactRankSelect(std::span< const Word > w, std::size_t n)
the directory of the bits of the words , copied as they are counted
CompactRankSelect(void)
the directory of no bit
Definition CompactRankSelect.h:123
the random-access constant iterator on the items T of the owner O, read by ( *o )[ i ]
Definition BitVector.h:372
Definition ADT.h:96
std::uint64_t Word
a word of 64 bits
Definition Bitwise.h:73