Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. #!/afs/cats.ucsc.edu/courses/cmps112-wm/usr/smalltalk/bin/gst -f
  2. "$Id: tree.st,v 1.10 2012-02-23 18:57:51-08 - - $"
  3.  
  4. nl := Character nl.
  5.  
  6.  
  7.  
  8.  
  9. "------------------------------------------------------------"
  10. Object subclass: OutBits [
  11. |bitIndex currentByte myStream|
  12. OutBits class >> new [
  13. self shouldNotImplement.
  14. ]
  15. OutBits class >> new: fileStream [
  16. |result|
  17. result := super new.
  18. result init: fileStream.
  19. ^result
  20. ]
  21. clearByte [
  22. bitIndex := 8.
  23. currentByte := 0.
  24. ]
  25. init: fileStream [
  26. myStream := fileStream.
  27. self clearByte.
  28. ]
  29. flushByte [
  30. bitIndex = 8 ifFalse: [
  31. myStream nextPutByte: currentByte.
  32. self clearByte.
  33. ]
  34. ]
  35. writeBit: bit [
  36. currentByte := currentByte bitAt: bitIndex put: bit.
  37. stdout << (currentByte bitAt: bitIndex).
  38. bitIndex := bitIndex - 1.
  39. bitIndex = 0 ifTrue: [self flushByte].
  40. ]
  41. ]
  42.  
  43. "----------------------------------------------------------"
  44. Object subclass: Leaf [
  45. |char count|
  46. char [ ^ char ]
  47. count [ ^ count ]
  48.  
  49. Leaf class >> new [
  50. self shouldNotImplement
  51. ]
  52.  
  53. Leaf class >> new: aChar count: aCount [
  54. |result|
  55. result := super new.
  56. result setChar: aChar andCount: aCount.
  57. ^result
  58. ]
  59.  
  60. setChar: aChar andCount: aCount [
  61. char := aChar.
  62. count := aCount.
  63. ]
  64.  
  65. <= other [
  66. ^ (count < other count)
  67. | ((count = other count) & (char <= other char))
  68. ]
  69.  
  70. printBase: aStream [
  71. ^ aStream << self class << '(' << char << ',' << count
  72. ]
  73.  
  74. printOn: aStream [
  75. (self printBase: aStream) << ')'.
  76. ]
  77.  
  78. inorder: visitor prefix: string [
  79. visitor value: char value: string.
  80. ]
  81.  
  82.  
  83. treeAsBit: visitor sum: qlength queue: stackq [
  84. stdout<< '1'.
  85. stackq nextPut: 1.
  86.  
  87. stackq nextPut: ((char asInteger) bitAt: 8).
  88. stackq nextPut: ((char asInteger) bitAt: 7).
  89. stackq nextPut: ((char asInteger) bitAt: 6).
  90. stackq nextPut: ((char asInteger) bitAt: 5).
  91. stackq nextPut: ((char asInteger) bitAt: 4).
  92. stackq nextPut: ((char asInteger) bitAt: 3).
  93. stackq nextPut: ((char asInteger) bitAt: 2).
  94. stackq nextPut: ((char asInteger) bitAt: 1).
  95.  
  96. stdout << ((char asInteger) bitAt: 8).
  97. stdout << ((char asInteger) bitAt: 7).
  98. stdout << ((char asInteger) bitAt: 6).
  99. stdout << ((char asInteger) bitAt: 5).
  100. stdout << ((char asInteger) bitAt: 4).
  101. stdout << ((char asInteger) bitAt: 3).
  102. stdout << ((char asInteger) bitAt: 2).
  103. stdout << ((char asInteger) bitAt: 1).
  104. ^ 9.
  105.  
  106. ]
  107. ]
  108.  
  109. Leaf subclass: Tree [
  110. |left right|
  111.  
  112. Tree class >> new: aChar count: aCount [
  113. self shouldNotImplement
  114. ]
  115.  
  116. Tree class >> new: aChar count: aCount left: aLeft right: aRight [
  117. |result|
  118. result := super new: aChar count: aCount.
  119. result setLeft: aLeft andRight: aRight.
  120. ^ result
  121. ]
  122.  
  123. setLeft: aLeft andRight: aRight [
  124. left := aLeft.
  125. right := aRight.
  126. ]
  127.  
  128. printOn: aStream [
  129. (self printBase: aStream) << ',' << left << ',' << right << ')'.
  130. ]
  131.  
  132. inorder: visitor prefix: string [
  133. left inorder: visitor prefix: string, '0'.
  134. right inorder: visitor prefix: string, '1'.
  135. ]
  136.  
  137. treeAsBit: visitor sum: qlength queue: stackq[
  138. |sum1 sum2 sum |
  139. sum1 := left treeAsBit: visitor sum: qlength queue: stackq.
  140. sum2 := right treeAsBit: visitor sum: qlength queue: stackq.
  141. sum := sum1 + sum2.
  142. sum := sum + 1.
  143. stackq nextPut: 0.
  144. stdout << '0'.
  145. ^ sum
  146. ]
  147.  
  148. ]
  149.  
  150.  
  151. "----------------------------------------------------------------------"
  152.  
  153. Character extend [
  154. isPrint [
  155. ^ (Character space <= self) & (self <= $~)
  156. ]
  157. visible [
  158. self isPrint ifTrue: [^ '$', self asString]
  159. ifFalse: [^ self asInteger printStringRadix: 8]
  160. ]
  161. ]
  162.  
  163. Object subclass: ZeroArray [
  164. |theArray|
  165.  
  166. ZeroArray class >> new [
  167. self shouldNotImplement.
  168. ]
  169.  
  170. ZeroArray class >> new: size [
  171. |result|
  172. result := super new.
  173. result init: size.
  174. ^ result
  175. ]
  176.  
  177. init: size [
  178. theArray := Array new: size.
  179. ]
  180.  
  181. size [
  182. ^ theArray size.
  183. ]
  184.  
  185. at: index [
  186. ^ theArray at: index + 1.
  187. ]
  188.  
  189. at: index put: value [
  190. ^ theArray at: index + 1 put: value.
  191. ]
  192.  
  193. incr: index [
  194. (self at: index) isNil
  195. ifTrue: [ self at: index put: 0].
  196. self at: index put: (self at: index) + 1.
  197. ]
  198.  
  199. keysAndValuesDo: aBlock [
  200. (0 to: (self size) - 1) do: [:index |
  201. aBlock value: index value: (self at: index).
  202. ]
  203. ]
  204.  
  205.  
  206.  
  207. ]
  208. "-------------------------------------------------------------------"
  209.  
  210.  
  211.  
  212.  
  213.  
  214. freqtable := ZeroArray new: 256.
  215.  
  216. infile := FileStream open: (Smalltalk arguments at: 2)
  217. mode: FileStream read.
  218.  
  219.  
  220. [infile atEnd not] whileTrue: [
  221. |ordChar|
  222. ordChar := infile next asInteger.
  223. freqtable incr: ordChar.
  224. ].
  225.  
  226. infile close.
  227.  
  228. d := 0.
  229. t := 0.
  230. c := 0.
  231. u := 0.
  232.  
  233. stdout << 'file: ' <<(Smalltalk arguments at: 2) << nl.
  234.  
  235. Smalltalk arguments: '-d -t -c -u'
  236. do: [:opt :arg |
  237. opt = $d ifTrue: [stdout << 'option d' <<nl.
  238. d :=1. ].
  239. opt = $t ifTrue: [stdout << 'option t' <<nl.
  240. t :=1. ].
  241. opt = $c ifTrue: [stdout << 'option c' <<nl.
  242. c :=1. ].
  243. opt = $u ifTrue: [stdout << 'option u' <<nl.
  244. u :=1. ].
  245. ]
  246. ifError: [
  247. 'Error: invalid option' displayNl.
  248. ].
  249.  
  250.  
  251.  
  252.  
  253. sortcol := SortedCollection new.
  254. q := SharedQueue new.
  255. chars_in_file := SortedCollection new.
  256.  
  257. freqtable keysAndValuesDo: [:key :value |
  258. (value notNil and: [value > 0]) ifTrue: [
  259. val := Leaf new: key asCharacter count: value.
  260. sortcol add: val.
  261. chars_in_file add: key
  262. ]
  263. ].
  264.  
  265. encode_table := ZeroArray new: 256.
  266.  
  267.  
  268. [sortcol size > 1] whileTrue: [
  269. |first second t|
  270. first := sortcol removeFirst.
  271. second := sortcol removeFirst.
  272.  
  273. t := Tree new: (second char) count: (first count + second count) left: first right: second.
  274. sortcol add: t.
  275. ].
  276.  
  277.  
  278. t := sortcol removeFirst.
  279.  
  280. t inorder: [:char :string |
  281. encode_table at: char asInteger put: string.
  282. ] prefix: ''.
  283.  
  284. formatOutput := [:let :freq :bits|
  285. let asString size to: 3 do: [:skip| stdout << ' '].
  286. stdout << let.
  287. freq asString size to: 5 do: [:skip| stdout << ' '].
  288. stdout << freq << ' ' << bits << nl.
  289. ].
  290.  
  291.  
  292. arraylen := 0.
  293.  
  294. encode_table keysAndValuesDo: [:key :value |
  295. (value notNil and: [value > 0]) ifTrue: [
  296. formatOutput value: (key asCharacter visible) value: (freqtable at: key) value: value.
  297. arraylen := arraylen + ((freqtable at: key)*(value size)).
  298. ]
  299. ].
  300.  
  301.  
  302.  
  303.  
  304.  
  305. stackq := SharedQueue new.
  306. qlength := 0.
  307. stdout<<nl.
  308. qlength := t treeAsBit: [:char :string ] sum: qlength queue: stackq.
  309. stdout<<0.
  310. qlength := qlength + 1.
  311. stackq nextPut: 0.
  312. stdout<<nl.
  313.  
  314.  
  315. stdout << qlength<< nl.
  316.  
  317.  
  318. extra:= 0.
  319. stdout << ((arraylen+qlength) \\ 8) << nl << nl << nl.
  320. ((arraylen+qlength) // 8 = 0) ifFalse: [
  321. extra := 8 - (arraylen+qlength) \\ 8 ].
  322.  
  323. bitArray := Array new: (arraylen+qlength)+extra.
  324. stdout << bitArray size << nl << nl.
  325.  
  326. index := 1.
  327.  
  328.  
  329. [stackq isEmpty not] whileTrue: [
  330. |bit|
  331. bit := stackq next.
  332. bitArray at: index put: bit.
  333. index := index + 1.
  334. ].
  335.  
  336. stdout << nl << nl.
  337.  
  338.  
  339.  
  340. infile := FileStream open: (Smalltalk arguments at: 2)
  341. mode: FileStream read.
  342.  
  343.  
  344.  
  345. stdout << 'second part of bitarray' <<nl.
  346.  
  347. [infile atEnd not] whileTrue: [
  348. |ordChar|
  349. ordChar := infile next asInteger.
  350.  
  351. index to: (index+((encode_table at: ordChar) size))-1 do:
  352. [ :x |
  353. bit := 0.
  354. (((encode_table at: ordChar) at: ((x-index)+1)) = $1) ifTrue: [bit:= 1.].
  355. bitArray at: x put: bit.
  356. stdout << (bitArray at: x put: bit).
  357. ].
  358.  
  359. index := (index+((encode_table at: ordChar) size)).
  360. ].
  361.  
  362. index to: ((arraylen+qlength)+extra) do: [
  363. :x |
  364. (bitArray at: x put: 0).
  365. ].
  366.  
  367. stdout << nl << nl << nl.
  368. bitArray do: [:bit | stdout << bit. ].
  369. stdout << nl.
  370.  
  371. infile close.
  372.  
  373.  
  374. writeBitArray := [:outfile|
  375. |outBytes|
  376. outBytes := OutBits new: outfile.
  377. stdout << 'writing to outfile:'<<nl.
  378. bitArray do: [:bit| outBytes writeBit: bit.].
  379. stdout << nl << 'done writing to outfile'<<nl.
  380. outBytes flushByte.
  381. stdout<<nl.
  382. ].
  383.  
  384. Smalltalk arguments size = 2
  385. ifTrue: [writeBitArray value: stdout]
  386. ifFalse: [
  387. |outfilename|
  388. outfilename := Smalltalk arguments at: 3.
  389. [ |outfile|
  390. outfile := FileStream open: outfilename mode: FileStream write.
  391. writeBitArray value: outfile.
  392. outfile close.
  393. ] on: SystemExceptions.FileError do: [:signal |
  394. |errno|
  395. errno := File errno.
  396. stdout flush.
  397. stderr << execname << ': ' << filename << ': '
  398. << signal messageText << nl.
  399. stderr flush.
  400. ]
  401. ].
  402.  
  403. bitArray do: [:bit | stdout << bit. ].
  404. stdout << nl.
  405. stdout << nl.
  406.  
  407. "
  408. infile := FileStream open: (Smalltalk arguments at: 2)
  409. mode: FileStream read.
  410.  
  411. Smalltalk arguments size = 2
  412. ifTrue: [
  413. [infile atEnd not] whileTrue: [
  414. |ordChar|
  415. ordChar := infile next asInteger.
  416. stdout << (encode_table at: ordChar)
  417. ].
  418. ]
  419. ifFalse: [stdout << 'hi' << nl.].
  420.  
  421. infile close."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement