home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c019 / 1.ddi / AR001 / MAKETBL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-22  |  1.3 KB  |  50 lines

  1. /***********************************************************
  2.     maketbl.c
  3. ***********************************************************/
  4. #include "ar.h"
  5.  
  6. static short c, n, tblsiz, len, depth, maxdepth, avail;
  7. static ushort codeword, bit, *tbl;
  8. static uchar *blen;
  9.  
  10. static short mktbl(void)
  11. {
  12.     short i;
  13.  
  14.     if (len == depth) {
  15.         while (++c < n)
  16.             if (blen[c] == len) {
  17.                 i = codeword;  codeword += bit;
  18.                 if (codeword > tblsiz) error("Bad table (1)");
  19.                 while (i < codeword) tbl[i++] = c;
  20.                 return c;
  21.             }
  22.         c = -1;  len++;  bit >>= 1;
  23.     }
  24.     depth++;
  25.     if (depth < maxdepth) {
  26.         (void) mktbl();  (void) mktbl();
  27.     } else if (depth > USHRT_BIT) {
  28.         error("Bad table (2)");
  29.     } else {
  30.         if ((i = avail++) >= 2 * n - 1) error("Bad table (3)");
  31.         left[i] = mktbl();  right[i] = mktbl();
  32.         if (codeword >= tblsiz) error("Bad table (4)");
  33.         if (depth == maxdepth) tbl[codeword++] = i;
  34.     }
  35.     depth--;
  36.     return i;
  37. }
  38.  
  39. void make_table(short nchar, uchar bitlen[],
  40.                 short tablebits, ushort table[])
  41. {
  42.     n = avail = nchar;  blen = bitlen;  tbl = table;
  43.     tblsiz = 1U << tablebits;  bit = tblsiz / 2;
  44.     maxdepth = tablebits + 1;
  45.     depth = len = 1;  c = -1;  codeword = 0;
  46.     (void) mktbl();  /* left subtree */
  47.     (void) mktbl();  /* right subtree */
  48.     if (codeword != tblsiz) error("Bad table (5)");
  49. }
  50.