home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / HPACK78S.ZIP / lza / model4.c < prev    next >
C/C++ Source or Header  |  1992-08-27  |  3KB  |  111 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            HPACK Multi-System Archiver                        *
  4. *                            ===========================                        *
  5. *                                                                            *
  6. *                                Low Position Model                            *
  7. *                            MODEL4.C  Updated 01/05/91                        *
  8. *                                                                            *
  9. * This program is protected by copyright and as such any use or copying of    *
  10. *  this code for your own purposes directly or indirectly is highly uncool    *
  11. *                      and if you do so there will be....trubble.                *
  12. *                 And remember: We know where your kids go to school.            *
  13. *                                                                            *
  14. *        Copyright 1990, 1991  Peter C.Gutmann.  All rights reserved            *
  15. *                                                                            *
  16. ****************************************************************************/
  17.  
  18. #ifdef __MAC__
  19.   #include "defs.h"
  20.   #include "model4.h"
  21. #else
  22.   #include "defs.h"
  23.   #include "lza/model4.h"
  24. #endif /* __MAC__ */
  25.  
  26. /* Translation tables between low positions and symbol indices */
  27.  
  28. int lowPosToIndex[ NO_LOW_POS ];
  29. int indexToLowPos[ NO_LOW_POS + 1 ];
  30.  
  31. int lowPosCumFreq[ NO_LOW_POS + 1 ];    /* Cumulative symbol frequencies */
  32. int lowPosFreq[ NO_LOW_POS + 1 ];        /* Symbol frequencies */
  33.  
  34. /* Initalize the model */
  35.  
  36. void startLowPosModel( void )
  37.     {
  38.     int i;
  39.  
  40.     /* Set up tables that translate between symbol indices and low positions */
  41.     for( i = 0; i < NO_LOW_POS; i++ )
  42.         {
  43.         lowPosToIndex[ i ] = i + 1;
  44.         indexToLowPos[ i + 1 ] = i;
  45.         }
  46.  
  47.     /* Set up initial frequency counts to be one for all symbols */
  48.     for( i = 0; i <= NO_LOW_POS; i++ )
  49.         {
  50.         lowPosFreq[ i ] = 1;
  51.         lowPosCumFreq[ i ] = NO_LOW_POS - i;
  52.         }
  53.     lowPosFreq[ 0 ] = 0;    /* Freq[ 0 ] must not be the same as freq[ 1 ] */
  54.     }
  55.  
  56. /* The counter used to trigger the adaptive count scaling */
  57.  
  58. extern unsigned int modelByteCount;
  59.  
  60. #define THRESHOLD    30000
  61.  
  62. #define SCALE1        1
  63. #define SCALE2        10
  64.  
  65. /* Update the model to account for a new symbol */
  66.  
  67. void updateLowPosModel( const int symbol )
  68.     {
  69.     int i, cum = 0;                /* New index for symbol */
  70.     int scaleValue;
  71.  
  72.     /* Change the scalevalue according to how many chars we have processed */
  73.     scaleValue = ( modelByteCount < THRESHOLD ) ? SCALE1 : SCALE2;
  74.  
  75.     /* See if frequency counts are at their maximum */
  76.     if( lowPosCumFreq[ 0 ] > MAX_FREQ - scaleValue )
  77.         {
  78.         /* If so, halve all the counts (keeping them non-zero) */
  79.         for( i = NO_LOW_POS; i >= 0; i-- )
  80.             {
  81.             lowPosFreq[ i ] = ( lowPosFreq[ i ] + 1 ) / 2;
  82.             lowPosCumFreq[ i ] = cum;
  83.             cum += lowPosFreq[ i ];
  84.             }
  85.         }
  86.  
  87.     /* Find the symbol's new index */
  88.     for( i = symbol; lowPosFreq[ i ] == lowPosFreq[ i - 1 ]; i-- );
  89.  
  90.     /* Update the translation tables if the symbol has moved */
  91.     if( i < symbol )
  92.         {
  93.         int ch_i, ch_symbol;
  94.  
  95.         ch_i = indexToLowPos[ i ];
  96.         ch_symbol = indexToLowPos[ symbol ];
  97.         indexToLowPos[ i ] = ch_symbol;
  98.         indexToLowPos[ symbol ] = ch_i;
  99.         lowPosToIndex[ ch_i ] = symbol;
  100.         lowPosToIndex[ ch_symbol ] = i;
  101.         }
  102.  
  103.     /* Increment the freq.count for the symbol and update the cum.freq's */
  104.     lowPosFreq[ i ] += scaleValue;
  105.     while( i > 0 )
  106.         {
  107.         i--;
  108.         lowPosCumFreq[ i ] += scaleValue;
  109.         }
  110.     }
  111.