ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
HuffmanWaveletTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File HuffmanWaveletTree.h -----------------------*/
3/*--------------------------------------------------------------------------*/
67/*--------------------------------------------------------------------------*/
68/*----------------------------- DEFINITIONS --------------------------------*/
69/*--------------------------------------------------------------------------*/
70
71#ifndef __ADS_HuffmanWaveletTree
72 #define __ADS_HuffmanWaveletTree
73/* self-identification: #endif constant */
74
75/*--------------------------------------------------------------------------*/
76/*------------------------------ INCLUDES ----------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79#include "ADS/Contract.h"
85
86#include <algorithm>
87#include <cstddef>
88#include <cstdint>
89#include <span>
90#include <string_view>
91#include <type_traits>
92#include <utility>
93#include <vector>
94
95/*--------------------------------------------------------------------------*/
96/*--------------------------- NAMESPACE ------------------------------------*/
97/*--------------------------------------------------------------------------*/
98
99namespace ADS
100{
101
102/*--------------------------------------------------------------------------*/
103/*------------------------ CLASS HuffmanWaveletTree ------------------------*/
104/*--------------------------------------------------------------------------*/
105
110template< class RS = RankSelect >
112{
113
114/*--------------------------------------------------------------------------*/
115 public:
117 HuffmanWaveletTree( void ) = default;
118
150 explicit HuffmanWaveletTree( std::span< const std::uint32_t > x ,
151 bool withSelect = true );
152
181 template< Executor E >
182 HuffmanWaveletTree( E & ex , std::span< const std::uint32_t > x ,
183 bool withSelect = true );
184
186 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
187
189 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
190
192 [[nodiscard]] std::size_t sigma( void ) const { return( f_len.size() ); }
193
196 [[nodiscard]] std::uint32_t access( std::size_t i ) const;
197
199 [[nodiscard]] std::uint32_t operator[]( std::size_t i ) const {
200 return( access( i ) );
201 }
202
222 [[nodiscard]] std::pair< std::uint32_t , std::size_t >
223 accessRank( std::size_t i ) const;
224
245 [[nodiscard]] std::size_t rank( std::uint32_t c , std::size_t i ) const;
246
251 [[nodiscard]] std::pair< std::size_t , std::size_t >
252 rank( std::uint32_t c , std::size_t i , std::size_t j ) const;
253
276 [[nodiscard]] std::size_t select( std::uint32_t c , std::size_t k ) const;
277
280 [[nodiscard]] int codeLength( std::uint32_t c ) const {
281 return( c < f_len.size() ? f_len[ c ] : 0 );
282 }
283
286 [[nodiscard]] std::size_t sizeInBits( void ) const;
287
288/*----------------------------- THE PERSISTENCE ----------------------------*/
289
291 static constexpr std::string_view persistName = "HuffmanWaveletTree";
292
294 [[nodiscard]] static constexpr std::uint64_t persistTag( void ) {
295 return( detail::persistHash( RS::persistName ) );
296 }
297
300 template< class A >
301 void persist( A & a );
302
303/*--------------------------------------------------------------------------*/
304 private:
308 struct Node
309 {
310 std::uint64_t off = 0 ,
311 ones = 0;
312 std::int32_t child[ 2 ] = { 0 , 0 };
313
315 template< class A >
316 void persist( A & a ) {
317 a( off , ones , child );
318 }
319 };
320
322 [[nodiscard]] static RS directory( BitVector b , bool withSelect ) {
323 if constexpr( std::is_same_v< RS , RankSelect > )
324 return( RS( std::move( b ) , withSelect ? RankSelect::Selects::BOTH
326 else
327 return( RS( std::move( b ) ) );
328 }
329
332 template< Executor E >
333 [[nodiscard]] static RS directory( E & ex , BitVector b , bool withSelect ) {
334 if constexpr( std::is_same_v< RS , RankSelect > )
335 return( RS( ex , std::move( b ) ,
336 withSelect ? RankSelect::Selects::BOTH
338 else if constexpr( std::is_same_v< RS , CompactRankSelect > )
339 return( RS( ex , std::move( b ) ) );
340 else
341 return( directory( std::move( b ) , withSelect ) );
342 }
343
345#ifndef ADS_WAVELET_SLICE
346 static constexpr std::size_t kSlice = std::size_t( 1 ) << 16;
347#else
348 static constexpr std::size_t kSlice = ADS_WAVELET_SLICE; // a test
349#endif
350
353 void sequential( std::span< const std::uint32_t > x , bool withSelect );
354
357 [[nodiscard]] std::uint64_t layout( const std::vector< std::uint64_t > & f );
358
360 [[nodiscard]] bool bit( std::uint32_t c , int d ) const {
361 return( ( f_code[ c ] >> ( f_len[ c ] - 1 - d ) ) & 1 );
362 }
363
364 std::size_t f_n = 0;
365 std::vector< std::uint64_t > f_code;
366 std::vector< std::uint8_t > f_len;
367 std::vector< Node > f_node;
368 RS f_B;
369
370 }; // end( class HuffmanWaveletTree )
371
372/*--------------------------------------------------------------------------*/
373
374} // end( namespace ADS )
375
376/*--------------------------------------------------------------------------*/
377/*-------------------------- IMPLEMENTATION --------------------------------*/
378/*--------------------------------------------------------------------------*/
379
380#include "ADS/succinct/impl/HuffmanWaveletTree.tpp"
381
382/*--------------------------------------------------------------------------*/
383
384#endif /* HuffmanWaveletTree.h included */
385
386/*--------------------------------------------------------------------------*/
387/*---------------------- End File HuffmanWaveletTree.h ---------------------*/
388/*--------------------------------------------------------------------------*/
The vector of bits on which the succinct structures and the integer codes of ADS are built: the bits ...
Rank and select on a static vector of bits of bits in 3.125% of bits more for the rank and less th...
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....
Rank and select on a static vector of bits of bits with 1s (chapter 15, Compressed Data Structures...
int codeLength(std::uint32_t c) const
the length of the code of , 0 if is not in the sequence
Definition HuffmanWaveletTree.h:280
std::size_t select(std::uint32_t c, std::size_t k) const
the place of the -th occurrence of
std::size_t size(void) const
the number of items
Definition HuffmanWaveletTree.h:186
HuffmanWaveletTree(E &ex, std::span< const std::uint32_t > x, bool withSelect=true)
the tree of HuffmanWaveletTree( x , withSelect ) above, the same codes, nodes and bits,...
std::size_t rank(std::uint32_t c, std::size_t i) const
the occurrences of in
bool empty(void) const
whether there is no item
Definition HuffmanWaveletTree.h:189
static constexpr std::string_view persistName
the name of the structure in a file [see Persistence.h]
Definition HuffmanWaveletTree.h:291
HuffmanWaveletTree(void)=default
the empty sequence
static constexpr std::uint64_t persistTag(void)
the directory of the bits, in a file
Definition HuffmanWaveletTree.h:294
std::size_t sigma(void) const
the bound of the symbols, which are below it
Definition HuffmanWaveletTree.h:192
std::pair< std::size_t, std::size_t > rank(std::uint32_t c, std::size_t i, std::size_t j) const
the occurrences of in and in , by one descent that moves both places as rank() above moves one
std::pair< std::uint32_t, std::size_t > accessRank(std::size_t i) const
the item and its occurrences in , by one descent
std::uint32_t access(std::size_t i) const
the item , by accessRank()
HuffmanWaveletTree(std::span< const std::uint32_t > x, bool withSelect=true)
the sequence , of symbols below its largest one plus 1, with select if asked for (else the directory ...
void persist(A &a)
the members written to, or read from, the archive [see Persistence.h]
std::size_t sizeInBits(void) const
the bits of the tree: its vector with the directory, the nodes and the codes
std::uint32_t operator[](std::size_t i) const
the item , by access()
Definition HuffmanWaveletTree.h:199
Definition ADT.h:96
@ BOTH
select1() and select0()
Definition RankSelect.h:179
@ NONE
no select, the rank only
Definition RankSelect.h:176