ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FrontCoding.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File FrontCoding.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
84/*--------------------------------------------------------------------------*/
85/*----------------------------- DEFINITIONS --------------------------------*/
86/*--------------------------------------------------------------------------*/
87
88#ifndef __ADS_FrontCoding
89 #define __ADS_FrontCoding
90/* self-identification: #endif constant */
91
92/*--------------------------------------------------------------------------*/
93/*------------------------------ INCLUDES ----------------------------------*/
94/*--------------------------------------------------------------------------*/
95
96#include "ADS/Contract.h"
98
99#include <algorithm>
100#include <cstddef>
101#include <cstdint>
102#include <iterator>
103#include <string>
104#include <string_view>
105#include <utility>
106#include <vector>
107
108/*--------------------------------------------------------------------------*/
109/*--------------------------- NAMESPACE ------------------------------------*/
110/*--------------------------------------------------------------------------*/
111
112namespace ADS
113{
114
117using FrontPair = std::pair< std::size_t , std::string >;
118
134[[nodiscard]] inline std::vector< FrontPair >
135frontCode( const std::vector< std::string > & D );
136
153[[nodiscard]] inline std::vector< std::string >
154frontDecode( const std::vector< FrontPair > & F );
155
175inline void encodeLength( std::uint32_t l , std::string & out );
176
193[[nodiscard]] inline std::uint32_t decodeLength( std::string_view in ,
194 std::size_t & pos );
195
196/*--------------------------------------------------------------------------*/
197/*---------------------- CLASS FrontCodedDictionary ------------------------*/
198/*--------------------------------------------------------------------------*/
199
201class FrontCodedDictionary
202{
203
204/*--------------------------------------------------------------------------*/
205 public:
225 [[nodiscard]] static FrontCodedDictionary
226 bucketed( const std::vector< std::string > & D , std::size_t b );
227
254 [[nodiscard]] static FrontCodedDictionary
255 localityPreserving( const std::vector< std::string > & D , double c );
256
272 [[nodiscard]] std::string access( std::size_t i ) const;
273
277 [[nodiscard]] std::size_t decodeCost( std::size_t i ) const;
278
292 [[nodiscard]] std::size_t lexicographicPosition( std::string_view Q ) const;
293
307 [[nodiscard]] std::size_t prefixEnd( std::string_view P ) const;
308
311 [[nodiscard]] std::pair< std::size_t , std::size_t >
312 prefixRange( std::string_view P ) const;
313
337 [[nodiscard]] std::size_t rankInBucket( std::size_t k , std::string_view P ,
338 bool upper ) const;
339
341 [[nodiscard]] std::size_t size( void ) const;
342
344 [[nodiscard]] std::size_t anchors( void ) const;
345
348 [[nodiscard]] std::size_t anchor( std::size_t k ) const;
349
352 [[nodiscard]] std::string_view anchorString( std::size_t k ) const;
353
355 [[nodiscard]] std::size_t characters( void ) const;
356
358 [[nodiscard]] std::size_t bytes( void ) const;
359
360/*------------------------- THE STANDARD INTERFACE -------------------------*/
361
365 {
366 public:
367 using reference = const std::string &;
368 using value_type = std::string;
369 using difference_type = std::ptrdiff_t;
371 using iterator_concept = std::forward_iterator_tag;
374 using iterator_category = std::input_iterator_tag;
375
377 const_iterator( void ) = default;
380 const_iterator( const FrontCodedDictionary * d , std::size_t i )
381 : f_d( d ) , f_i( i ) {
382 load();
383 }
384
386 reference operator*( void ) const { return( f_s ); }
389 ++f_i;
390 load();
391 return( *this );
392 }
393
395 auto t = *this;
396 ++*this;
397 return( t );
398 }
399
400 bool operator==( const const_iterator & o ) const {
401 return( f_i == o.f_i );
402 }
403
404 private:
406 void load( void ) {
407 if( f_d && ( f_i < f_d->size() ) ) {
408 f_s.resize( f_d->f_l[ f_i ] );
409 f_s.append( f_d->rest( f_i ) );
410 }
411 }
412
413 const FrontCodedDictionary * f_d = nullptr;
414 std::size_t f_i = 0;
415 std::string f_s;
416 };
417
418 using value_type = std::string;
419 using size_type = std::size_t;
420 using difference_type = std::ptrdiff_t;
421 using reference = const std::string &;
422 using const_reference = const std::string &;
424
426 [[nodiscard]] const_iterator begin( void ) const {
427 return( const_iterator( this , 0 ) );
428 }
429
430 [[nodiscard]] const_iterator end( void ) const {
431 return( const_iterator( this , size() ) );
432 }
433
434 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
436 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
437
438/*--------------------------------------------------------------------------*/
439 private:
440 FrontCodedDictionary( void ) = default;
441
445 template< class A >
446 static FrontCodedDictionary build( const std::vector< std::string > & D ,
447 A anchor );
448
450 [[nodiscard]] std::string_view rest( std::size_t i ) const;
451
453 [[nodiscard]] std::size_t anchorBefore( std::size_t i ) const;
454
456 [[nodiscard]] std::size_t anchorRank( std::string_view P , bool upper ) const;
457
458 std::vector< std::size_t > f_l;
459 std::vector< std::size_t > f_off;
460 std::string f_chars;
461 std::vector< std::size_t > f_a;
462 std::size_t f_bytes = 0;
463
464 }; // end( class FrontCodedDictionary )
465
466/*--------------------------------------------------------------------------*/
467
468} // end( namespace ADS )
469
470/*--------------------------------------------------------------------------*/
471/*-------------------------- IMPLEMENTATION --------------------------------*/
472/*--------------------------------------------------------------------------*/
473
474#include "ADS/string/impl/FrontCoding.tpp"
475
476/*--------------------------------------------------------------------------*/
477
478#endif /* FrontCoding.h included */
479
480/*--------------------------------------------------------------------------*/
481/*------------------------- End File FrontCoding.h -------------------------*/
482/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The search by prefix in a sorted dictionary of strings, beyond the book: the strings prefixed by ar...
a constant view of the strings in their order, decoded one after the other, forward; *it is the strin...
Definition FrontCoding.h:365
std::string value_type
a string
Definition FrontCoding.h:368
const_iterator(void)=default
an iterator on no dictionary
bool operator==(const const_iterator &o) const
whether the two iterators are at the same place
Definition FrontCoding.h:400
std::forward_iterator_tag iterator_concept
a forward iterator for the concepts
Definition FrontCoding.h:371
std::ptrdiff_t difference_type
a distance of iterators
Definition FrontCoding.h:369
const std::string & reference
the string decoded
Definition FrontCoding.h:367
const_iterator operator++(int)
the next string, this one returned
Definition FrontCoding.h:394
std::input_iterator_tag iterator_category
an input iterator for the algorithms of C++17, since *it is a reference into the iterator
Definition FrontCoding.h:374
const_iterator(const FrontCodedDictionary *d, std::size_t i)
the iterator on the string of *d, decoded from the string 0
Definition FrontCoding.h:380
const_iterator & operator++(void)
the next string, decoded from this one
Definition FrontCoding.h:388
reference operator*(void) const
the string decoded
Definition FrontCoding.h:386
a sorted dictionary of strings in the front coding, with anchors
Definition FrontCoding.h:202
std::string access(std::size_t i) const
the string , decoded from the anchor before it
std::size_t prefixEnd(std::string_view P) const
the number of strings smaller than
std::size_t lexicographicPosition(std::string_view Q) const
the number of strings smaller than
std::string value_type
a string
Definition FrontCoding.h:418
const_iterator iterator
the constant view
Definition FrontCoding.h:423
std::ptrdiff_t difference_type
a distance of places
Definition FrontCoding.h:420
const_iterator end(void) const
past the last string
Definition FrontCoding.h:430
std::size_t anchors(void) const
the number of anchors, the strings stored whole
const std::string & const_reference
a string decoded
Definition FrontCoding.h:422
const_iterator cbegin(void) const
the first string
Definition FrontCoding.h:434
std::size_t rankInBucket(std::size_t k, std::string_view P, bool upper) const
the number of strings smaller than (or if upper), given that anchors are: the scan of the bucket
static FrontCodedDictionary bucketed(const std::vector< std::string > &D, std::size_t b)
the front coding of with an anchor every strings
std::size_t decodeCost(std::size_t i) const
the characters that access( i ) reads, those of the rests from the anchor before to
const std::string & reference
a string decoded
Definition FrontCoding.h:421
static FrontCodedDictionary localityPreserving(const std::vector< std::string > &D, double c)
the locality-preserving front coding of , of parameter
std::size_t anchor(std::size_t k) const
the place in the dictionary of the anchor
std::string_view anchorString(std::size_t k) const
the string of the anchor
std::size_t size(void) const
the number of strings
std::size_t bytes(void) const
the bytes of the coding, the characters and the codes of the lengths
const_iterator begin(void) const
the first string
Definition FrontCoding.h:426
const_iterator cend(void) const
past the last string
Definition FrontCoding.h:436
std::pair< std::size_t, std::size_t > prefixRange(std::string_view P) const
the range of the strings prefixed by , by lexicographicPosition() and prefixEnd()
std::size_t size_type
a number of strings
Definition FrontCoding.h:419
std::size_t characters(void) const
the characters of the coding, those of the rests and of the anchors
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition Cereal.h:238
std::vector< FrontPair > frontCode(const std::vector< std::string > &D)
the front coding of , a pair per string
std::uint32_t decodeLength(std::string_view in, std::size_t &pos)
the length coded at in[ pos ], with pos moved past its code
void encodeLength(std::uint32_t l, std::string &out)
appends to out the code of in 1 to 4 bytes, the first two bits giving the number of bytes minus 1
std::vector< std::string > frontDecode(const std::vector< FrontPair > &F)
the strings of the front coding
std::pair< std::size_t, std::string > FrontPair
a pair of the front coding, the length of the prefix shared with the string before and the rest
Definition FrontCoding.h:117
Definition ADT.h:96