home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / FREEZE-2.ZIP / statist.c < prev    next >
C/C++ Source or Header  |  1992-07-18  |  6KB  |  281 lines

  1. #include "freeze.h"
  2. #include "lz.h"
  3.  
  4. /* This program calculates the distribution of the matched strings'
  5. positions and lengths using nearly the same code as `freeze'.
  6. */
  7.  
  8. #define N_POS 62
  9. #define T (N_POS * 2 - 1)
  10. #define R (T - 1)
  11.  
  12. #define update(c) (freq[c]++)
  13.  
  14. long in_count, refers = 0;
  15.  
  16. long indc_count;
  17. short reduceflag = 0, greedy = 0;
  18.  
  19. int lens[F2+1];
  20.  
  21. us_t bits[9];
  22.  
  23. short   prnt[T];
  24. ul_t freq[T];
  25. short used[T];
  26.  
  27. void freeze(), StartHuff();
  28.  
  29. SIGTYPE giveres();
  30.  
  31. int main(argc, argv) char ** argv; {
  32.     argv++;
  33.     while (argc > 1) {
  34.         if (**argv == '-') {
  35.             while (*++(*argv) == 'g')
  36.                 greedy++;
  37.             if (**argv)
  38.                 goto usage;
  39.             argc--; argv++;
  40.         } else
  41.             break;
  42.     }
  43.     usage:
  44.     if(argc != 1) {
  45.         fprintf(stderr, "Usage: statist [-g...] < sample_file\n");
  46.         fprintf(stderr, "Press INTR to display current values\n");
  47.         exit(0);
  48.     }
  49.     signal(SIGINT, giveres);
  50.  
  51. #ifdef DOS
  52.     setmode(fileno(stdin), O_BINARY);       /* Oh this MS-DOS ... */
  53. #endif  /* DOS */
  54.  
  55.     freeze();
  56.     giveres();
  57.     return 0;
  58. }
  59.  
  60. ul_t isqrt(val)
  61. ul_t val;
  62. {
  63.   ul_t result = 0;
  64.   ul_t side = 0;
  65.   ul_t left = 0;
  66.   int digit = 0;
  67.   int i;
  68.   for (i=0; i<sizeof(ul_t)*4; i++)
  69.   {
  70.     left = (left << 2) + (val >> (sizeof(ul_t) * 8 - 2));
  71.     val <<= 2;
  72.     if (left >= side*2 + 1)
  73.     {
  74.       left -= side*2+1;
  75.       side = (side+1)*2;
  76.       result <<= 1;
  77.       result |= 1;
  78.     }
  79.     else
  80.     {
  81.       side *= 2;
  82.       result <<= 1;
  83.     }
  84.   }
  85.   return result;
  86. }
  87.  
  88.  
  89. /* Prints the (current) values of tunable parameters. Uncertainty is
  90. the number of missequencings (algorithm assumes the probabilities
  91. of references decrease uniformly when distance increases). Ideally
  92. it should be 0, but somewhat about 5 or less denotes the given 8 values
  93. could improve the compression rate when using them.
  94. */
  95.  
  96. SIGTYPE giveres() {
  97.     us_t c;
  98.     register int i, j, k, pr, f, average, sum;
  99.     ul_t cumul, sigma2;
  100.     short r, percent;
  101.     signal(SIGINT, giveres);
  102.     newtry:
  103.     StartHuff(N_POS);
  104.     pr = f = 0;
  105.     i = N_POS;
  106.     r = N_POS * 2 - 2;
  107.     while (i <= r) {
  108.         j = findmin(i);
  109.         k = findmin(i);
  110.         freq[i] = freq[j] + freq[k];
  111.         prnt[j] = prnt[k] = i++;
  112.     }
  113.  
  114.     for (c = 1; c <= 6; c++) bits[c] = 0;
  115.  
  116.     for(c = 0; c < N_POS; c++) {
  117.         j = 0;
  118.         k = c;
  119.         do j++; while ((k = prnt[k]) != r);
  120.         if (j <= 6)
  121.             bits[j]++;
  122.         if (j < pr)
  123.             f += pr - j;
  124.         else
  125.             pr = j;
  126.     }
  127.  
  128.     k = bits[1] + bits[2] + bits[3] + bits[4] +
  129.     bits[5] + bits[6];
  130.  
  131.     k = 62 - k;     /* free variable length codes for 7 & 8 bits */
  132.  
  133.     j = 128 * bits[1] + 64 * bits[2] + 32 * bits[3] +
  134.     16 * bits[4] + 8 * bits[5] + 4 * bits[6];
  135.  
  136.     j = 256 - j;    /* free byte images for these codes */
  137.  
  138. /*      Equation:
  139.         bits[7] + bits[8] = k
  140.     2 * bits[7] + bits[8] = j
  141. */
  142.     j -= k;
  143.     if (j < 0 || k < j) {
  144.         printf("Huffman tree has more than 8 levels, reducing...\n");
  145.         for (i = 0; i < N_POS; i++)
  146.             if (!freq[i])
  147.                 freq[i] = 1;
  148.             else if (reduceflag)
  149.                 freq[i] = (freq[i] + 1) / 2;
  150.         reduceflag = 1;
  151.         goto newtry;
  152.     } else {
  153.         bits[7] = j;
  154.         bits[8] = k - j;
  155.         printf("%d %d %d %d %d %d %d %d (uncertainty = %d)\n",
  156.             bits[1], bits[2], bits[3], bits[4],
  157.             bits[5], bits[6], bits[7], bits[8], f);
  158.     }
  159.     sum = 0; cumul = 0;
  160.     for(i = 3; i <= F2; i++) {
  161.         cumul += (ul_t) i * lens[i];
  162.         sum += lens[i];
  163.     }
  164.     sum || sum++;
  165.     printf("Average match length: %d.%02d\n",
  166.         average = cumul / sum, i = cumul * 100 / sum % 100);
  167.     if (i >= 50) average++;
  168.     j = sum;
  169.     percent = 0;
  170.     for (i = F2; i >= 3; i--) {
  171.         static pcs[] = { 999, 995, 990, 970, 950, 900, 800, 700, 500 };
  172.         j -= lens[i];
  173.         newpcs:
  174.         if (j <= sum * pcs[percent] / 1000) {
  175.             printf("Percentile %d.%d: %d\n",
  176.                 pcs[percent] / 10, pcs[percent] % 10, i);
  177.             if (percent == sizeof(pcs)/sizeof(int) - 1)
  178.                 break;
  179.             else {
  180.                 percent++;
  181.                 goto newpcs;
  182.             }
  183.         }
  184.     }
  185.     for (sigma2 = 0, i = 3; i <= F2; i++)
  186.         sigma2 += (ul_t)(i - average)*(i - average)*lens[i];
  187.     sigma2 = sigma2 * 100 / sum;
  188.     j = (int)isqrt(sigma2);
  189.     printf("Sigma: %d.%1d\n", j / 10, j % 10);
  190.     printf("References: %ld\n", refers);
  191.     fflush(stdout);
  192. }
  193.  
  194.  
  195. void freeze ()
  196. {
  197.     register us_t i, len, r, s;
  198.     register short c;
  199.     StartHuff(0);
  200.     InitTree();
  201.     s = 0;
  202.     r = N2 - F2;
  203.     for (i = s; i < r; i++)
  204.         text_buf[i] = ' ';
  205.     for (len = 0; len < F2 && (c = getchar()) != EOF; len++)
  206.         text_buf[r + len] = c;
  207.     in_count = len;
  208.     for (i = 0; i <= F2; i++)
  209.         InsertNode(r + i - F2);
  210.     while (len != 0) {
  211.         match_length = THRESHOLD;
  212.         Get_Next_Match(r,1);
  213.         if (match_length > len)
  214.             match_length = len;
  215.         if (match_length <= THRESHOLD) {
  216.             match_length = 1;
  217.         } else if (greedy) {
  218.             lens[match_length] ++;
  219.             update((us_t)match_position >> 7);
  220.             refers ++;
  221.         } else {
  222.             register us_t orig_length, orig_position;
  223.             orig_length = match_length;
  224.             orig_position = match_position;
  225.             DeleteNode(s);
  226.             Next_Char(N2, F2);
  227.             Get_Next_Match(r,2);
  228.             if (match_length > len) match_length = len;
  229.             if (orig_length > match_length) {
  230.                 lens[orig_length] ++;
  231.                 update((us_t)orig_position >> 7);
  232.                 match_length = orig_length - 1;
  233.             } else  {
  234.                 lens[match_length] ++;
  235.                 update(match_position >> 7);
  236.             }
  237.             refers ++;
  238.         }
  239.         for (i = 0; i < match_length &&
  240.                 (c = getchar()) != EOF; i++) {
  241.             DeleteNode(s);
  242.             text_buf[s] = c;
  243.             if (s < F2 - 1)
  244.                 text_buf[s + N2] = c;
  245.             s = (s + 1) & (N2 - 1);
  246.             r = (r + 1) & (N2 - 1);
  247.             InsertNode(r);
  248.         }
  249.         in_count += i;
  250.         if ((in_count > indc_count)) {
  251.             fprintf(stderr, "%5dK\b\b\b\b\b\b", in_count / 1024);
  252.             fflush (stderr);
  253.             indc_count += 4096;
  254.         }
  255.         while (i++ < match_length) {
  256.             DeleteNode(s);
  257.             s = (s + 1) & (N2 - 1);
  258.             r = (r + 1) & (N2 - 1);
  259.             if (--len) InsertNode(r);
  260.         }
  261.     }
  262. }
  263.  
  264. void StartHuff(beg) {
  265.     int i;
  266.     for (i = beg; i < N_POS * 2 - 1; i++)
  267.         freq[i] = 0;
  268.     for (i = 0; i < N_POS * 2 - 1; i++)
  269.         used[i] = prnt[i] = 0;
  270. }
  271.  
  272. int findmin(range) {
  273.     long min = (1 << 30) - 1, argmin = -1, i;
  274.     for (i = 0; i < range; i++) {
  275.         if(!used[i] && freq[i] < min)
  276.             min = freq[argmin = i];
  277.     }
  278.     used[argmin] = 1;
  279.     return argmin;
  280. }
  281.