Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. (de lzwCompress (Lst File)
  2. (let (Codes 65535 Dict)
  3. (balance 'Dict
  4. (make
  5. (for C Codes
  6. (link (cons (char C) C)) ) ) )
  7. (out File
  8. (let W (pop 'Lst)
  9. (for C Lst
  10. (let WC (pack W C)
  11. (if (lup Dict WC)
  12. (setq W WC)
  13. (printsp (cdr (lup Dict W)))
  14. (idx 'Dict (cons WC (inc 'Codes)) T)
  15. (setq W C) ) ) )
  16. (and W (printsp (cdr (lup Dict W)))) ) ) ) )
  17.  
  18. (de lzwDecompress (File)
  19. (let (Codes 65535 Dict)
  20. (balance 'Dict
  21. (make
  22. (for C Codes
  23. (link (list C (char C))) ) ) )
  24. (make
  25. (let W NIL
  26. (in File
  27. (while (read)
  28. (let WC (if (lup Dict @) (cdr @) (cons (last W) W))
  29. (chain (reverse WC))
  30. (when W
  31. (idx 'Dict (cons (inc 'Codes) (cons (last WC) W)) T) )
  32. (setq W WC) ) ) ) ) ) ) )
  33.  
  34. NIL
  35.  
  36. : (lzwCompress (chop "TOBEORNOTTOBEORTOBEORNOT") "a")
  37. -> 65543
  38. : (in "a" (echo))
  39. 84 79 66 69 79 82 78 79 84 65536 65538 65540 65545 65539 65541 65543 -> T
  40. : (pack (lzwDecompress "a"))
  41. -> "TOBEORNOTTOBEORTOBEORNOT"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement