ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
WaveletMatrix.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File WaveletMatrix.h --------------------------*/
3/*--------------------------------------------------------------------------*/
75/*--------------------------------------------------------------------------*/
76/*----------------------------- DEFINITIONS --------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79#ifndef __ADS_WaveletMatrix
80 #define __ADS_WaveletMatrix
81/* self-identification: #endif constant */
82
83/*--------------------------------------------------------------------------*/
84/*------------------------------ INCLUDES ----------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87#include "ADS/Contract.h"
91
92#include <algorithm>
93#include <bit>
94#include <cstddef>
95#include <cstdint>
96#include <memory>
97#include <span>
98#include <string_view>
99#include <type_traits>
100#include <utility>
101#include <vector>
102
103/*--------------------------------------------------------------------------*/
104/*--------------------------- NAMESPACE ------------------------------------*/
105/*--------------------------------------------------------------------------*/
106
107namespace ADS
108{
109
110/*--------------------------------------------------------------------------*/
111/*--------------------------- CLASS WaveletMatrix --------------------------*/
112/*--------------------------------------------------------------------------*/
113
117{
118
119/*--------------------------------------------------------------------------*/
120 public:
122 WaveletMatrix( void ) = default;
123
139 explicit WaveletMatrix( std::span< const std::uint64_t > x , int w = 0 );
140
193 template< Executor E >
194 WaveletMatrix( E & ex , std::span< const std::uint64_t > x , int w = 0 );
195
198 template< class U >
199 requires( std::is_same_v< U , std::uint8_t > ||
200 std::is_same_v< U , std::uint16_t > ||
201 std::is_same_v< U , std::uint32_t > )
202 explicit WaveletMatrix( std::span< const U > x , int w = 0 );
203
206 template< Executor E , class U >
207 requires( std::is_same_v< U , std::uint8_t > ||
208 std::is_same_v< U , std::uint16_t > ||
209 std::is_same_v< U , std::uint32_t > )
210 WaveletMatrix( E & ex , std::span< const U > x , int w = 0 );
211
213 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
214
216 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
217
219 [[nodiscard]] int width( void ) const { return( int( f_lev.size() ) ); }
220
237 [[nodiscard]] std::uint64_t access( std::size_t i ) const;
238
240 [[nodiscard]] std::uint64_t operator[]( std::size_t i ) const {
241 return( access( i ) );
242 }
243
263 [[nodiscard]] std::size_t rank( std::uint64_t c , std::size_t i ) const;
264
287 [[nodiscard]] std::size_t select( std::uint64_t c , std::size_t k ) const;
288
308 [[nodiscard]] std::uint64_t quantile( std::size_t i , std::size_t j ,
309 std::size_t k ) const;
310
331 [[nodiscard]] std::size_t countLess( std::size_t i , std::size_t j ,
332 std::uint64_t c ) const;
333
335 [[nodiscard]] std::size_t sizeInBits( void ) const;
336
337/*----------------------------- THE PERSISTENCE ----------------------------*/
338
340 static constexpr std::string_view persistName = "WaveletMatrix";
341
344 template< class A >
345 void persist( A & a );
346
347/*------------------------- THE STANDARD INTERFACE -------------------------*/
348
349 using value_type = std::uint64_t;
350 using size_type = std::size_t;
351 using difference_type = std::ptrdiff_t;
352 using reference = std::uint64_t;
353 using const_reference = std::uint64_t;
357
359 [[nodiscard]] const_iterator begin( void ) const {
360 return( const_iterator( this , 0 ) );
361 }
362
363 [[nodiscard]] const_iterator end( void ) const {
364 return( const_iterator( this , f_n ) );
365 }
366
367 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
369 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
370
371/*--------------------------------------------------------------------------*/
372 private:
374 [[nodiscard]] bool bit( std::uint64_t c , std::size_t l ) const {
375 return( ( c >> ( f_lev.size() - 1 - l ) ) & 1 );
376 }
377
379 [[nodiscard]] std::size_t start( std::uint64_t c ) const;
380
382#ifndef ADS_WAVELET_SLICE
383 static constexpr std::size_t kSlice = std::size_t( 1 ) << 16;
384#else
385 static constexpr std::size_t kSlice = ADS_WAVELET_SLICE; // a test
386#endif
387
389#ifndef ADS_WAVELET_SLICE_MAX
390 static constexpr std::size_t kSliceMax = std::size_t( 1 ) << 18;
391#else
392 static constexpr std::size_t kSliceMax = ADS_WAVELET_SLICE_MAX; // a test
393#endif
394
397 template< class U , Executor E >
398 void init( E & ex , std::span< const U > x , int w );
399
402 template< class T , class U , Executor E >
403 void build( E & ex , std::span< const U > x , int w );
404
405 std::size_t f_n = 0;
406 std::vector< RankSelect > f_lev;
407 std::vector< std::size_t > f_zeros;
408
409 }; // end( class WaveletMatrix )
410
411/*--------------------------------------------------------------------------*/
412
413} // end( namespace ADS )
414
415/*--------------------------------------------------------------------------*/
416/*-------------------------- IMPLEMENTATION --------------------------------*/
417/*--------------------------------------------------------------------------*/
418
419#include "ADS/succinct/impl/WaveletMatrix.tpp"
420
421/*--------------------------------------------------------------------------*/
422
423#endif /* WaveletMatrix.h included */
424
425/*--------------------------------------------------------------------------*/
426/*------------------------ End File WaveletMatrix.h ------------------------*/
427/*--------------------------------------------------------------------------*/
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...
Rank and select on a static vector of bits of bits with 1s (chapter 15, Compressed Data Structures...
the random-access constant iterator on the items T of the owner O, read by ( *o )[ i ]
Definition BitVector.h:372
WaveletMatrix(void)=default
the empty sequence
std::ptrdiff_t difference_type
the type of a distance
Definition WaveletMatrix.h:351
WaveletMatrix(std::span< const std::uint64_t > x, int w=0)
the sequence , its items of bits (0: the bits of the largest one), built by the passes of up to 8 le...
WaveletMatrix(std::span< const U > x, int w=0)
the sequence of items of 8, 16 or 32 bits, which the construction reads as they are,...
const_iterator cbegin(void) const
the first item
Definition WaveletMatrix.h:367
std::uint64_t operator[](std::size_t i) const
the item , by access()
Definition WaveletMatrix.h:240
const_iterator iterator
the items are read only
Definition WaveletMatrix.h:356
WaveletMatrix(E &ex, std::span< const U > x, int w=0)
the sequence of items of 8, 16 or 32 bits on the threads of ex, as WaveletMatrix( ex ,...
bool empty(void) const
whether there is no item
Definition WaveletMatrix.h:216
WaveletMatrix(E &ex, std::span< const std::uint64_t > x, int w=0)
the matrix of WaveletMatrix( x , w ) above, the same levels and directories, built on the threads of ...
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
std::uint64_t quantile(std::size_t i, std::size_t j, std::size_t k) const
the -th smallest item of the places , from
int width(void) const
the bits of an item
Definition WaveletMatrix.h:219
const_iterator end(void) const
one past the last item
Definition WaveletMatrix.h:363
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition WaveletMatrix.h:340
std::size_t countLess(std::size_t i, std::size_t j, std::uint64_t c) const
the number of the items below in the places
const_iterator begin(void) const
the first item
Definition WaveletMatrix.h:359
std::size_t select(std::uint64_t c, std::size_t k) const
the place of the -th
std::uint64_t const_reference
an item, by value
Definition WaveletMatrix.h:353
std::size_t size_type
the type of a size
Definition WaveletMatrix.h:350
std::size_t sizeInBits(void) const
the bits of the levels and of their directories
SubscriptIterator< WaveletMatrix, std::uint64_t > const_iterator
an iterator on the items, by access()
Definition WaveletMatrix.h:355
std::size_t size(void) const
the number of items
Definition WaveletMatrix.h:213
const_iterator cend(void) const
one past the last item
Definition WaveletMatrix.h:369
std::uint64_t access(std::size_t i) const
the item
std::uint64_t value_type
the type of an item
Definition WaveletMatrix.h:349
std::uint64_t reference
an item, by value
Definition WaveletMatrix.h:352
std::size_t rank(std::uint64_t c, std::size_t i) const
the number of in the places
Definition ADT.h:96