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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            HPACK Multi-System Archiver                        *
  4. *                            ===========================                        *
  5. *                                                                            *
  6. *                              LZSS Code Length Model                        *
  7. *                            MODEL2.C  Updated 08/04/92                        *
  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 - 1992  Peter C.Gutmann.  All rights reserved        *
  15. *                                                                            *
  16. ****************************************************************************/
  17.  
  18. #ifdef __MAC__
  19.   #include "defs.h"
  20.   #include "model2.h"
  21. #else
  22.   #include "defs.h"
  23.   #include "lza/model2.h"
  24. #endif /* __MAC__ */
  25.  
  26. /* Translation tables between length count and symbol indices */
  27.  
  28. int lengthToIndex[ NO_LENGTHS ];
  29. int indexToLength[ NO_LENGTHS + 1 ];
  30.  
  31. int lengthCumFreq[ NO_LENGTHS + 1 ];    /* Cumulative symbol frequencies */
  32. int lengthFreq[ NO_LENGTHS + 1 ];        /* Symbol frequencies */
  33.  
  34. /* The scale value */
  35.  
  36. #define SCALE_VALUE        10
  37.  
  38. /* Initalize the model */
  39.  
  40. void startLengthModel( void )
  41.     {
  42.     int i;
  43.  
  44.     /* Set up tables that translate between symbol indices and high positions */
  45.     for( i = 0; i < NO_LENGTHS; i++ )
  46.         {
  47.         lengthToIndex[ i ] = i + 1;
  48.         indexToLength[ i + 1 ] = i;
  49.         }
  50.  
  51.     /* Set up initial frequency counts to be one for all symbols */
  52.     for( i = 0; i <= NO_LENGTHS; i++ )
  53.         {
  54.         lengthFreq[ i ] = 1;
  55.         lengthCumFreq[ i ] = NO_LENGTHS - i;
  56.         }
  57.     lengthFreq[ 0 ] = 0;    /* Freq[ 0 ] must not be the same as freq[ 1 ] */
  58.     }
  59.  
  60. /* Update the model to account for a new symbol */
  61.  
  62. void updateLengthModel( const int symbol )
  63.     {
  64.     int i, cum = 0;                /* New index for symbol */
  65.  
  66.     /* See if frequency counts are at their maximum */
  67.     if( lengthCumFreq[ 0 ] > MAX_FREQ - SCALE_VALUE )
  68.         {
  69.         /* If so, halve all the counts (keeping them non-zero) */
  70.         for( i = NO_LENGTHS; i >= 0; i-- )
  71.             {
  72.             lengthFreq[ i ] = ( lengthFreq[ i ] + 1 ) / 2;
  73.             lengthCumFreq[ i ] = cum;
  74.             cum += lengthFreq[ i ];
  75.             }
  76.         }
  77.  
  78.     /* Find the symbol's new index */
  79.     for( i = symbol; lengthFreq[ i ] == lengthFreq[ i - 1 ]; i-- );
  80.  
  81.     /* Update the translation tables if the symbol has moved */
  82.     if( i < symbol )
  83.         {
  84.         int ch_i, ch_symbol;
  85.  
  86.         ch_i = indexToLength[ i ];
  87.         ch_symbol = indexToLength[ symbol ];
  88.         indexToLength[ i ] = ch_symbol;
  89.         indexToLength[ symbol ] = ch_i;
  90.         lengthToIndex[ ch_i ] = symbol;
  91.         lengthToIndex[ ch_symbol ] = i;
  92.         }
  93.  
  94.     /* Increment the freq.count for the symbol and update the cum.freq's */
  95.     lengthFreq[ i ] += SCALE_VALUE;
  96.     while( i > 0 )
  97.         {
  98.         i--;
  99.         lengthCumFreq[ i ] += SCALE_VALUE;
  100.         }
  101.     }
  102.