home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / hufftab.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  90 lines

  1. ############################################################################
  2. #
  3. #    File:     hufftab.icn
  4. #
  5. #    Subject:  Program to compute Huffman state transitions 
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     November 14, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #      Each input line should be a string of 0s & 1s followed by a value
  18. #   field.  Output is a list of items in a form suitable for inclusion
  19. #   by a C program as initialization for an array.  Each pair of items
  20. #   indicates the action to be taken on receipt of a 0 or 1 bit from the
  21. #   corresponding state; this is either a state number if more decoding
  22. #   is needed or the value field from the input if not.  State 0 is the
  23. #   initial state;  0 is output only for undefined states.  States are
  24. #   numbered by two to facilitate use of a one-dimensional array.
  25. #
  26. #   sample input:        corresponding output:
  27. #    00 a                /*  0 */  2, c, a, 4, 0, b,
  28. #    011 b
  29. #    1 c            [new line started every 10 entries]
  30. #
  31. #   Interpretation:
  32. #    from state 0,  input=0 => go to state 2,  input=1 => return c
  33. #    from state 2,  input=0 => return a,  input=1 => go to state 4
  34. #    from state 4,  input=0 => undefined,  input=1 => return b
  35. #
  36. ############################################################################
  37.  
  38. global curstate, sttab, line
  39.  
  40. procedure main()
  41.    local code, val, n
  42.  
  43.    sttab := list()
  44.    put(sttab)
  45.    put(sttab)
  46.    while line := read() do {
  47.       line ? {
  48.          if ="#" | pos(0) then next
  49.          (code := tab(many('01'))) | (write(&errout, "bad: ", line) & next)
  50.          tab(many(' \t'))
  51.          val := tab(0)
  52.       }
  53.       curstate := 1
  54.       every bit(!code[1:-1])
  55.       curstate +:= code[-1]
  56.       if \sttab[curstate] then write(&errout, "dupl: ", line)
  57.       sttab[curstate] := val
  58.    }
  59.    write("/* generated by machine -- do not edit! */")
  60.    write()
  61.    writes("/*  0 */")
  62.    out(sttab[1])
  63.    every n := 2 to *sttab do {
  64.       if n % 10 = 1 then writes("\n/* ", n-1, " */")
  65.       out(sttab[n])
  66.    }
  67.    write()
  68. end
  69.  
  70.  
  71. procedure bit(c)
  72.    curstate +:= c
  73.    if integer(sttab[curstate]) then {
  74.       curstate := sttab[curstate]
  75.       return
  76.    }
  77.    if type(sttab[curstate]) == "string" then write(&errout, "dupl: ", line)
  78.    curstate := sttab[curstate] := *sttab + 1
  79.    put(sttab)
  80.    put(sttab)
  81. end
  82.  
  83.  
  84. procedure out(v)
  85.    if type(v) == "integer" then
  86.       writes(right(v-1, 6), ",")
  87.    else
  88.       writes(right(\v | "0", 6), ",")
  89. end
  90.