Program Listing for File PPM.h

↰ Return to documentation for file (ADS/compression/PPM.h)

/*--------------------------------------------------------------------------*/
/*------------------------------- File PPM.h -------------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_PPM
 #define __ADS_PPM
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"
#include "ADS/compression/ArithmeticCoding.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <map>
#include <span>
#include <string>
#include <string_view>
#include <vector>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

/*--------------------------------------------------------------------------*/
/*----------------------------- CLASS PPMModel -----------------------------*/
/*--------------------------------------------------------------------------*/

class PPMModel {

/*--------------------------------------------------------------------------*/
 public:

 struct Step {
  std::uint64_t low , high , total;
  friend bool operator==( const Step & a , const Step & b ) = default;
  };

 PPMModel( int order , std::uint32_t sigma = 256 );

 [[nodiscard]] int order( void ) const { return( f_order ); }

 [[nodiscard]] std::uint32_t sigma( void ) const { return( f_sigma ); }

 [[nodiscard]] std::map< std::uint32_t , std::uint64_t > counts(
                              const std::vector< std::uint32_t > & ctx ) const;

 [[nodiscard]] std::vector< Step > steps( std::uint32_t c ) const;

 void encode( std::uint32_t c , RangeEncoder & e ) const;

 [[nodiscard]] std::uint32_t decode( RangeDecoder & d ) const;

 void update( std::uint32_t c );

/*--------------------------------------------------------------------------*/
 private:

 using Counts = std::map< std::uint32_t , std::uint64_t >;

 template< class Choose >
 std::uint32_t walk( Choose choose ) const;

 static constexpr std::uint64_t kLimit = std::uint64_t( 1 ) << 16;

 int f_order;
 std::uint32_t f_sigma;
 std::map< std::vector< std::uint32_t > , Counts > f_ctx;
 std::vector< std::uint32_t > f_hist;

 };  // end( class PPMModel )

/*--------------------------------------------------------------------------*/

[[nodiscard]] inline std::vector< std::uint8_t > ppmEncode(
                                                 std::string_view s , int K );

[[nodiscard]] inline std::string ppmDecode(
             std::span< const std::uint8_t > code , std::size_t n , int K );

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/compression/impl/PPM.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* PPM.h included */

/*--------------------------------------------------------------------------*/
/*----------------------------- End File PPM.h -----------------------------*/
/*--------------------------------------------------------------------------*/