home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume04 / spl < prev    next >
Encoding:
Text File  |  1991-08-27  |  6.2 KB  |  304 lines

  1. Path: uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!cwjcc!hal!ncoast!allbery
  2. From: br@laura.irb.informatik.uni-dortmund.de.UUCP (Bodo Rueskamp)
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i039: splay tree compression routines
  5. Message-ID: <8808251946.AA11945@laura.irb.informatik.uni-dortmund.de>
  6. Date: 25 Aug 88 23:46:57 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: br@laura.irb.informatik.uni-dortmund.de.UUCP (Bodo Rueskamp)
  9. Lines: 291
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. Posting-number: Volume 4, Issue 39
  13. Submitted-by: "Bodo Rueskamp" <br@laura.irb.informatik.uni-dortmund.de.UUCP>
  14. Archive-name: spl
  15.  
  16. Splay tree compression routines from the article "Application Of Splay
  17. Trees To Data Compression", Communications of the ACM, August 1988.
  18.  
  19.  
  20.  
  21. #--------------------------------CUT HERE-------------------------------------
  22. #! /bin/sh
  23. #
  24. # This is a shell archive.  Save this into a file, edit it
  25. # and delete all lines above this comment.  Then give this
  26. # file to sh by executing the command "sh file".  The files
  27. # will be extracted into the current directory owned by
  28. # you with default permissions.
  29. #
  30. # The files contained herein are:
  31. #
  32. # -rw-------   1 br       group         83 Aug 25 13:37 Makefile
  33. # -rw-r--r--   1 br       group       1530 Aug 25 13:26 spl.c
  34. # -rw-r--r--   1 br       group       1550 Aug 25 13:28 unspl.c
  35. # -rw-------   1 br       group        251 Aug 25 13:56 RESULTS
  36. #
  37. echo 'x - Makefile'
  38. if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
  39. sed 's/^X//' << '________This_Is_The_END________' > Makefile
  40. X
  41. Xall: spl unspl
  42. X
  43. Xspl: spl.o
  44. X    cc -o spl spl.o
  45. X
  46. Xunspl: unspl.o
  47. X    cc -o unspl unspl.o
  48. X
  49. ________This_Is_The_END________
  50. if test `wc -l < Makefile` -ne 9; then
  51.     echo 'shar: Makefile was damaged during transit (should have been 9 bytes)'
  52. fi
  53. fi        ; : end of overwriting check
  54. echo 'x - spl.c'
  55. if test -f spl.c; then echo 'shar: not overwriting spl.c'; else
  56. sed 's/^X//' << '________This_Is_The_END________' > spl.c
  57. X/*
  58. X    Splay Tree Compression
  59. X
  60. X    from: "Application Of Splay Trees To Data Compression"
  61. X          by Douglas W. Jones, Communications of the ACM, August 1988
  62. X
  63. X    implemented in C by Bodo Rueskamp, <br@unido.uucp>
  64. X*/
  65. X
  66. X/*  splay tree compress  */
  67. X
  68. X#include <stdio.h>
  69. X
  70. Xint left[257], right[257], up[514];
  71. X
  72. Xstatic int bitnum = 0;
  73. X
  74. Xvoid bit_output(bit)
  75. Xint bit;
  76. X{
  77. X    static int byte = 0;
  78. X
  79. X    byte = (byte << 1) | bit;
  80. X    ++bitnum;
  81. X    if (bitnum == 8) {
  82. X        putchar(byte);
  83. X        byte = 0;
  84. X        bitnum = 0;
  85. X    }
  86. X}
  87. X
  88. Xvoid initialize()
  89. X{
  90. X    int i;
  91. X
  92. X    for (i=0; i<514; ++i)
  93. X        up[i] = i / 2;
  94. X    for (i=0; i<257; ++i) {
  95. X        left[i] = i * 2;
  96. X        right[i] = i * 2 + 1;
  97. X    }
  98. X}
  99. X
  100. Xvoid splay(plain)
  101. Xint plain;
  102. X{
  103. X    int a, b, c, d;
  104. X
  105. X    a = plain + 257;
  106. X    while (a) {  /*  walk up the tree semi-rotating pairs  */
  107. X        c = up[a];
  108. X        if (c) {  /*  a pair remains  */
  109. X            d = up[c];
  110. X            /*  exchange children of pair  */
  111. X            b = left[d];
  112. X            if (c == b) {
  113. X                b = right[d];
  114. X                right[d] = a;
  115. X            } else {
  116. X                left[d] = a;
  117. X            }
  118. X            if (a == left[c]) {
  119. X                left[c] = b;
  120. X            } else {
  121. X                right[c] = b;
  122. X            }
  123. X            up[a] = d;
  124. X            up[b] = c;
  125. X            a = d;
  126. X        } else {  /*  handle odd node at end  */
  127. X            a = c;
  128. X        }
  129. X    }
  130. X}
  131. X
  132. Xvoid compress(plain)
  133. Xint plain;
  134. X{
  135. X    int sp;
  136. X    char stack[514];
  137. X    int a;
  138. X
  139. X    a = plain + 257;
  140. X    sp = 0;
  141. X    while (a) {
  142. X        stack[sp++] = a == right[up[a]];
  143. X        a = up[a];
  144. X    }
  145. X    while (sp--)
  146. X        bit_output(stack[sp]);
  147. X    splay(plain);
  148. X}
  149. X
  150. Xmain()
  151. X{
  152. X    int c;
  153. X
  154. X    putchar(0x1f);
  155. X    putchar('S');
  156. X    initialize();
  157. X    while ((c = getchar()) != -1)
  158. X        compress(c);
  159. X    compress(256);
  160. X    while (bitnum)
  161. X        bit_output(0);
  162. X    return(0);
  163. X}
  164. ________This_Is_The_END________
  165. if test `wc -l < spl.c` -ne 107; then
  166.     echo 'shar: spl.c was damaged during transit (should have been 107 bytes)'
  167. fi
  168. fi        ; : end of overwriting check
  169. echo 'x - unspl.c'
  170. if test -f unspl.c; then echo 'shar: not overwriting unspl.c'; else
  171. sed 's/^X//' << '________This_Is_The_END________' > unspl.c
  172. X/*
  173. X    Splay Tree Compression
  174. X
  175. X    from: "Application Of Splay Trees To Data Compression"
  176. X          by Douglas W. Jones, Communications of the ACM, August 1988
  177. X
  178. X    implemented in C by Bodo Rueskamp, <br@unido.uucp>
  179. X*/
  180. X
  181. X/*  splay tree uncompress  */
  182. X
  183. X#include <stdio.h>
  184. X
  185. Xint left[257], right[257], up[514];
  186. X
  187. Xint bit_input()
  188. X{
  189. X    static int bitnum = 0;
  190. X    static int byte;
  191. X
  192. X    if (bitnum == 0) {
  193. X        if ((byte = getchar()) == -1) {
  194. X            fprintf(stderr, "unexpected end of input\n");
  195. X            exit(1);
  196. X        }
  197. X        bitnum = 8;
  198. X    }
  199. X    byte <<= 1;
  200. X    --bitnum;
  201. X    return((byte >> 8) & 1);
  202. X}
  203. X
  204. Xvoid initialize()
  205. X{
  206. X    int i;
  207. X
  208. X    for (i=0; i<514; ++i)
  209. X        up[i] = i / 2;
  210. X    for (i=0; i<257; ++i) {
  211. X        left[i] = i * 2;
  212. X        right[i] = i * 2 + 1;
  213. X    }
  214. X}
  215. X
  216. Xvoid splay(plain)
  217. Xint plain;
  218. X{
  219. X    int a, b, c, d;
  220. X
  221. X    a = plain + 257;
  222. X    while (a) {  /*  walk up the tree semi-rotating pairs  */
  223. X        c = up[a];
  224. X        if (c) {  /*  a pair remains  */
  225. X            d = up[c];
  226. X            /*  exchange children of pair  */
  227. X            b = left[d];
  228. X            if (c == b) {
  229. X                b = right[d];
  230. X                right[d] = a;
  231. X            } else {
  232. X                left[d] = a;
  233. X            }
  234. X            if (a == left[c]) {
  235. X                left[c] = b;
  236. X            } else {
  237. X                right[c] = b;
  238. X            }
  239. X            up[a] = d;
  240. X            up[b] = c;
  241. X            a = d;
  242. X        } else {  /*  handle odd node at end  */
  243. X            a = c;
  244. X        }
  245. X    }
  246. X}
  247. X
  248. Xint uncompress()
  249. X{
  250. X    int a;
  251. X
  252. X    a = 0;
  253. X    while (a < 257)
  254. X        a = bit_input() ? right[a] : left[a];
  255. X    a -= 257;
  256. X    splay(a);
  257. X    return(a);
  258. X}
  259. X
  260. Xmain()
  261. X{
  262. X    int c;
  263. X
  264. X    if ((getchar() != 0x1f) || (getchar() != 'S')) {
  265. X        fprintf(stderr, "stdin not in compressed format\n");
  266. X        exit(1);
  267. X    }
  268. X    initialize();
  269. X    while ((c = uncompress()) != 256)
  270. X        putchar(c);
  271. X    return(0);
  272. X}
  273. ________This_Is_The_END________
  274. if test `wc -l < unspl.c` -ne 101; then
  275.     echo 'shar: unspl.c was damaged during transit (should have been 101 bytes)'
  276. fi
  277. fi        ; : end of overwriting check
  278. echo 'x - RESULTS'
  279. if test -f RESULTS; then echo 'shar: not overwriting RESULTS'; else
  280. sed 's/^X//' << '________This_Is_The_END________' > RESULTS
  281. X
  282. Xsome results of LZW and splay tree compression:
  283. X
  284. Xfilename       length  factor
  285. X
  286. XMakefile      83     0%
  287. XMakefile.S      62    25%
  288. XMakefile.Z      59    29%
  289. X
  290. Xspl.c        1530     0%
  291. Xspl.c.S        1128    26%
  292. Xspl.c.Z          958    37%
  293. X
  294. Xunspl.c        1550     0%
  295. Xunspl.c.S    1152    26%
  296. Xunspl.c.Z    1007    35%
  297. X
  298. ________This_Is_The_END________
  299. if test `wc -l < RESULTS` -ne 17; then
  300.     echo 'shar: RESULTS was damaged during transit (should have been 17 bytes)'
  301. fi
  302. fi        ; : end of overwriting check
  303. exit 0
  304.