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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            HPACK Multi-System Archiver                        *
  4. *                            ===========================                        *
  5. *                                                                            *
  6. *                                High Position Model                            *
  7. *                             MODEL3.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 "model3.h"
  21. #else
  22.   #include "defs.h"
  23.   #include "lza/model3.h"
  24. #endif /* __MAC__ */
  25.  
  26. /* Translation tables between high positions and symbol indices */
  27.  
  28. int highPosToIndex[ NO_HIGH_POS ];
  29. int indexToHighPos[ NO_HIGH_POS + 1 ];
  30.  
  31. int highPosCumFreq[ NO_HIGH_POS + 1 ];    /* Cumulative symbol frequencies */
  32. int highPosFreq[ NO_HIGH_POS + 1 ];        /* Symbol frequencies */
  33.  
  34. /* The size of the model, set depending on the window size */
  35.  
  36. extern int windowSize;
  37.  
  38. static int noHighPos;
  39.  
  40. /* Initalize the model */
  41.  
  42. void startHighPosModel( void )
  43.     {
  44.     int i;
  45.  
  46.     /* Set the size of the model depending on the window size */
  47.     noHighPos = NO_HIGH_POS >> windowSize;
  48.  
  49.     /* Set up tables that translate between symbol indices and high positions */
  50.     for( i = 0; i < noHighPos; i++ )
  51.         {
  52.         highPosToIndex[ i ] = i + 1;
  53.         indexToHighPos[ i + 1 ] = i;
  54.         }
  55.  
  56.     /* Set up initial frequency counts to be one for all symbols */
  57.     for( i = 0; i <= noHighPos; i++ )
  58.         {
  59.         highPosFreq[ i ] = 1;
  60.         highPosCumFreq[ i ] = noHighPos - i;
  61.         }
  62.     highPosFreq[ 0 ] = 0;    /* Freq[ 0 ] must not be the same as freq[ 1 ] */
  63.     }
  64.  
  65. /* The counter used to trigger the adaptive count scaling */
  66.  
  67. extern unsigned int modelByteCount;
  68.  
  69. #define THRESHOLD    20000
  70.  
  71. #define SCALE1        2
  72. #define SCALE2        5
  73.  
  74. /* Update the model to account for a new symbol */
  75.  
  76. void updateHighPosModel( const int symbol )
  77.     {
  78.     int i, cum = 0;                /* New index for symbol */
  79.     int scaleValue;
  80.  
  81.     /* Change the scalevalue according to how many chars we have processed */
  82.     scaleValue = ( modelByteCount < THRESHOLD ) ? SCALE1 : SCALE2;
  83.  
  84.     /* See if frequency counts are at their maximum */
  85.     if( highPosCumFreq[ 0 ] > MAX_FREQ - scaleValue )
  86.         {
  87.         /* If so, halve all the counts (keeping them non-zero) */
  88.         for( i = noHighPos; i >= 0; i-- )
  89.             {
  90.             highPosFreq[ i ] = ( highPosFreq[ i ] + 1 ) / 2;
  91.             highPosCumFreq[ i ] = cum;
  92.             cum += highPosFreq[ i ];
  93.             }
  94.         }
  95.  
  96.     /* Find the symbol's new index */
  97.     for( i = symbol; highPosFreq[ i ] == highPosFreq[ i - 1 ]; i-- );
  98.  
  99.     /* Update the translation tables if the symbol has moved */
  100.     if( i < symbol )
  101.         {
  102.         int ch_i, ch_symbol;
  103.  
  104.         ch_i = indexToHighPos[ i ];
  105.         ch_symbol = indexToHighPos[ symbol ];
  106.         indexToHighPos[ i ] = ch_symbol;
  107.         indexToHighPos[ symbol ] = ch_i;
  108.         highPosToIndex[ ch_i ] = symbol;
  109.         highPosToIndex[ ch_symbol ] = i;
  110.         }
  111.  
  112.     /* Increment the freq.count for the symbol and update the cum.freq's */
  113.     highPosFreq[ i ] += scaleValue;
  114.     while( i > 0 )
  115.         {
  116.         i--;
  117.         highPosCumFreq[ i ] += scaleValue;
  118.         }
  119.     }
  120.