ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CanonicalHuffman.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File CanonicalHuffman.h -------------------------*/
3/*--------------------------------------------------------------------------*/
86/*--------------------------------------------------------------------------*/
87/*----------------------------- DEFINITIONS --------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90#ifndef __ADS_CanonicalHuffman
91 #define __ADS_CanonicalHuffman
92/* self-identification: #endif constant */
93
94/*--------------------------------------------------------------------------*/
95/*------------------------------ INCLUDES ----------------------------------*/
96/*--------------------------------------------------------------------------*/
97
98#include "ADS/Contract.h"
100#include "ADS/string/Huffman.h"
101
102#include <algorithm>
103#include <cstddef>
104#include <cstdint>
105#include <map>
106#include <span>
107#include <string>
108#include <type_traits>
109#include <vector>
110
111/*--------------------------------------------------------------------------*/
112/*--------------------------- NAMESPACE ------------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115namespace ADS
116{
117
131template< class C , class F >
132[[nodiscard]] std::map< C , int >
133huffmanLengths( const std::map< C , F > & freq );
134
157template< class C , class F >
158[[nodiscard]] std::map< C ,
159 int > limitedLengths( const std::map< C , F > & freq ,
160 int L );
161
162/*--------------------------------------------------------------------------*/
163/*------------------------- CLASS CanonicalHuffman -------------------------*/
164/*--------------------------------------------------------------------------*/
165
167template< class C >
169{
170
171/*--------------------------------------------------------------------------*/
172 public:
173 using symbol_type = C;
174
197 explicit CanonicalHuffman( const std::map< C , int > & lengths );
198
201 template< class F >
202 [[nodiscard]] static CanonicalHuffman
203 fromFrequencies( const std::map< C , F > & freq );
204
206 [[nodiscard]] const std::map< C , int > & lengths( void ) const {
207 return( f_len );
208 }
209
211 [[nodiscard]] int maxLength( void ) const { return( f_max ); }
212
216 [[nodiscard]] std::uint64_t firstCode( int l ) const;
217
220 [[nodiscard]] std::string code( const C & c ) const;
221
224 void encode( const C & c , PackedBitWriter & w ) const;
225
240 [[nodiscard]] std::vector< std::uint8_t >
241 encode( std::span< const C > text ) const;
242
258 [[nodiscard]] C decode( PackedBitReader & r ) const;
259
262 [[nodiscard]] std::vector< C >
263 decode( const std::vector< std::uint8_t > & bits , std::size_t n ) const;
264
265/*--------------------------------------------------------------------------*/
266 private:
267 std::map< C , int > f_len;
268 std::map< C , std::uint64_t > f_code;
269 std::vector< std::uint64_t > f_fc;
270 std::vector< std::vector< C > > f_symb;
271 int f_max = 0;
272
273 }; // end( class CanonicalHuffman )
274
275/*--------------------------------------------------------------------------*/
276
277} // end( namespace ADS )
278
279/*--------------------------------------------------------------------------*/
280/*-------------------------- IMPLEMENTATION --------------------------------*/
281/*--------------------------------------------------------------------------*/
282
283#include "ADS/compression/impl/CanonicalHuffman.tpp"
284
285/*--------------------------------------------------------------------------*/
286
287#endif /* CanonicalHuffman.h included */
288
289/*--------------------------------------------------------------------------*/
290/*---------------------- End File CanonicalHuffman.h -----------------------*/
291/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The code of Huffman (reality check of section 14.4): a prefix code, no code being a prefix of another...
The bits of the compressors of this folder, packed in bytes, since their input and their output are s...
std::vector< std::uint8_t > encode(std::span< const C > text) const
the codes of the symbols of text, packed in bytes
std::uint64_t firstCode(int l) const
, the value of the first code of length
static CanonicalHuffman fromFrequencies(const std::map< C, F > &freq)
the canonical code of the code of Huffman of the frequencies
int maxLength(void) const
the longest length
Definition CanonicalHuffman.h:211
const std::map< C, int > & lengths(void) const
the lengths of the codes
Definition CanonicalHuffman.h:206
void encode(const C &c, PackedBitWriter &w) const
appends the code of to
std::string code(const C &c) const
the code of , a string of '0' and '1'
std::vector< C > decode(const std::vector< std::uint8_t > &bits, std::size_t n) const
the first symbols encoded in bits, each by decode() above
C decode(PackedBitReader &r) const
the symbol whose code is next in
CanonicalHuffman(const std::map< C, int > &lengths)
the canonical code of the lengths
C symbol_type
the type of the symbols
Definition CanonicalHuffman.h:173
reads the bits of a sequence of bytes in the order of a PackedBitWriter, and zeros past its end
Definition PackedBits.h:108
a growing sequence of bits, packed in bytes, the first bit in the most significant bit of the first b...
Definition PackedBits.h:70
std::map< C, int > limitedLengths(const std::map< C, F > &freq, int L)
the lengths of a code of Huffman of no length above , by halving the frequencies (rounding up) until ...
std::map< C, int > huffmanLengths(const std::map< C, F > &freq)
the lengths of the codes of Huffman [see Huffman.h] of the symbols with the frequencies freq
Definition ADT.h:96