Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.43 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. bitIndex := bitIndex - 1.
  38. bitIndex = 0 ifTrue: [self flushByte].
  39. ]
  40. ]
  41.  
  42. "----------------------------------------------------------"
  43. Object subclass: Leaf [
  44. |char count|
  45. char [ ^ char ]
  46. count [ ^ count ]
  47.  
  48. Leaf class >> new [
  49. self shouldNotImplement
  50. ]
  51.  
  52. Leaf class >> new: aChar count: aCount [
  53. |result|
  54. result := super new.
  55. result setChar: aChar andCount: aCount.
  56. ^result
  57. ]
  58.  
  59. setChar: aChar andCount: aCount [
  60. char := aChar.
  61. count := aCount.
  62. ]
  63.  
  64. <= other [
  65. ^ (count < other count)
  66. | ((count = other count) & (char <= other char))
  67. ]
  68.  
  69. printBase: aStream [
  70. ^ aStream << self class << '(' << char << ',' << count
  71. ]
  72.  
  73. printOn: aStream [
  74. (self printBase: aStream) << ')'.
  75. ]
  76.  
  77. inorder: visitor prefix: string [
  78. visitor value: char value: string.
  79. ]
  80.  
  81.  
  82. treeAsBit: visitor sum: qlength queue: stackq [
  83. stdout<< '1'.
  84. stackq nextPut: 1.
  85.  
  86. stackq nextPut: ((char asInteger) bitAt: 8).
  87. stackq nextPut: ((char asInteger) bitAt: 7).
  88. stackq nextPut: ((char asInteger) bitAt: 6).
  89. stackq nextPut: ((char asInteger) bitAt: 5).
  90. stackq nextPut: ((char asInteger) bitAt: 4).
  91. stackq nextPut: ((char asInteger) bitAt: 3).
  92. stackq nextPut: ((char asInteger) bitAt: 2).
  93. stackq nextPut: ((char asInteger) bitAt: 1).
  94.  
  95. stdout << ((char asInteger) bitAt: 8).
  96. stdout << ((char asInteger) bitAt: 7).
  97. stdout << ((char asInteger) bitAt: 6).
  98. stdout << ((char asInteger) bitAt: 5).
  99. stdout << ((char asInteger) bitAt: 4).
  100. stdout << ((char asInteger) bitAt: 3).
  101. stdout << ((char asInteger) bitAt: 2).
  102. stdout << ((char asInteger) bitAt: 1).
  103. ^ 9.
  104.  
  105. ]
  106.  
  107. isLeaf [
  108. ^1
  109. ]
  110. ]
  111.  
  112. Leaf subclass: Tree [
  113. |left right|
  114.  
  115. Tree class >> new: aChar count: aCount [
  116. self shouldNotImplement
  117. ]
  118.  
  119. Tree class >> new: aChar count: aCount left: aLeft right: aRight [
  120. |result|
  121. result := super new: aChar count: aCount.
  122. result setLeft: aLeft andRight: aRight.
  123. ^ result
  124. ]
  125.  
  126. setLeft: aLeft andRight: aRight [
  127. left := aLeft.
  128. right := aRight.
  129. ]
  130.  
  131. left [
  132. ^ left
  133. ]
  134.  
  135. right [
  136. ^ right
  137. ]
  138.  
  139. printOn: aStream [
  140. (self printBase: aStream) << ',' << left << ',' << right << ')'.
  141. ]
  142.  
  143. inorder: visitor prefix: string [
  144. left inorder: visitor prefix: string, '0'.
  145. right inorder: visitor prefix: string, '1'.
  146. ]
  147.  
  148. treeAsBit: visitor sum: qlength queue: stackq[
  149. |sum1 sum2 sum |
  150. sum1 := left treeAsBit: visitor sum: qlength queue: stackq.
  151. sum2 := right treeAsBit: visitor sum: qlength queue: stackq.
  152. sum := sum1 + sum2.
  153. sum := sum + 1.
  154. stackq nextPut: 0.
  155. stdout << '0'.
  156. ^ sum
  157. ]
  158.  
  159.  
  160. isLeaf [
  161. ^0
  162. ]
  163.  
  164. ]
  165.  
  166.  
  167. "----------------------------------------------------------------------"
  168.  
  169. Character extend [
  170. isPrint [
  171. ^ (Character space <= self) & (self <= $~)
  172. ]
  173. visible [
  174. self isPrint ifTrue: [^ '$', self asString]
  175. ifFalse: [^ self asInteger printStringRadix: 8]
  176. ]
  177. ]
  178.  
  179. Object subclass: ZeroArray [
  180. |theArray|
  181.  
  182. ZeroArray class >> new [
  183. self shouldNotImplement.
  184. ]
  185.  
  186. ZeroArray class >> new: size [
  187. |result|
  188. result := super new.
  189. result init: size.
  190. ^ result
  191. ]
  192.  
  193. init: size [
  194. theArray := Array new: size.
  195. ]
  196.  
  197. size [
  198. ^ theArray size.
  199. ]
  200.  
  201. at: index [
  202. ^ theArray at: index + 1.
  203. ]
  204.  
  205. at: index put: value [
  206. ^ theArray at: index + 1 put: value.
  207. ]
  208.  
  209. incr: index [
  210. (self at: index) isNil
  211. ifTrue: [ self at: index put: 0].
  212. self at: index put: (self at: index) + 1.
  213. ]
  214.  
  215. keysAndValuesDo: aBlock [
  216. (0 to: (self size) - 1) do: [:index |
  217. aBlock value: index value: (self at: index).
  218. ]
  219. ]
  220.  
  221.  
  222.  
  223. ]
  224. "-------------------------------------------------------------------"
  225.  
  226.  
  227.  
  228.  
  229.  
  230. freqtable := ZeroArray new: 256.
  231.  
  232. infile := FileStream open: (Smalltalk arguments at: 2)
  233. mode: FileStream read.
  234.  
  235.  
  236. [infile atEnd not] whileTrue: [
  237. |ordChar|
  238. ordChar := infile next asInteger.
  239. freqtable incr: ordChar.
  240. ].
  241.  
  242. infile close.
  243.  
  244. d := 0.
  245. t := 0.
  246. c := 0.
  247. u := 0.
  248.  
  249. stdout << 'file: ' <<(Smalltalk arguments at: 2) << nl.
  250.  
  251. Smalltalk arguments: '-d -t -c -u'
  252. do: [:opt :arg |
  253. opt = $d ifTrue: [stdout << 'option d' <<nl.
  254. d :=1. ].
  255. opt = $t ifTrue: [stdout << 'option t' <<nl.
  256. t :=1. ].
  257. opt = $c ifTrue: [stdout << 'option c' <<nl.
  258. c :=1. ].
  259. opt = $u ifTrue: [stdout << 'option u' <<nl.
  260. u :=1. ].
  261. ]
  262. ifError: [
  263. 'Error: invalid option' displayNl.
  264. ].
  265.  
  266.  
  267. c = 1 ifTrue: [
  268. sortcol := SortedCollection new.
  269. q := SharedQueue new.
  270. chars_in_file := SortedCollection new.
  271.  
  272. freqtable keysAndValuesDo: [:key :value |
  273. (value notNil and: [value > 0]) ifTrue: [
  274. val := Leaf new: key asCharacter count: value.
  275. sortcol add: val.
  276. chars_in_file add: key
  277. ]
  278. ].
  279.  
  280. encode_table := ZeroArray new: 256.
  281.  
  282.  
  283. [sortcol size > 1] whileTrue: [
  284. |first second t|
  285. first := sortcol removeFirst.
  286. second := sortcol removeFirst.
  287.  
  288. t := Tree new: (second char) count: (first count + second count) left: first right: second.
  289. sortcol add: t.
  290. ].
  291.  
  292.  
  293. t := sortcol removeFirst.
  294. stdout << 'hi this is our encrypted tree: \n'.
  295. t inspect.
  296.  
  297. t inorder: [:char :string |
  298. encode_table at: char asInteger put: string.
  299. ] prefix: ''.
  300.  
  301. formatOutput := [:let :freq :bits|
  302. let asString size to: 3 do: [:skip| stdout << ' '].
  303. stdout << let.
  304. freq asString size to: 5 do: [:skip| stdout << ' '].
  305. stdout << freq << ' ' << bits << nl.
  306. ].
  307.  
  308.  
  309. arraylen := 0.
  310.  
  311. encode_table keysAndValuesDo: [:key :value |
  312. (value notNil and: [value > 0]) ifTrue: [
  313. formatOutput value: (key asCharacter visible) value: (freqtable at: key) value: value.
  314. arraylen := arraylen + ((freqtable at: key)*(value size)).
  315. ]
  316. ].
  317.  
  318.  
  319.  
  320.  
  321.  
  322. stackq := SharedQueue new.
  323. qlength := 0.
  324. stdout<<nl.
  325. qlength := t treeAsBit: [:char :string ] sum: qlength queue: stackq.
  326. stdout<<0.
  327. qlength := qlength + 1.
  328. stackq nextPut: 0.
  329. stdout<<nl.
  330.  
  331.  
  332. stdout << qlength<< nl.
  333.  
  334.  
  335. extra:= 0.
  336. stdout << ((arraylen+qlength) \\ 8) << nl << nl << nl.
  337. ((arraylen+qlength) // 8 = 0) ifFalse: [
  338. extra := 8 - (arraylen+qlength) \\ 8 ].
  339.  
  340. bitArray := Array new: (arraylen+qlength)+extra.
  341. stdout << bitArray size << nl << nl.
  342.  
  343. index := 1.
  344.  
  345.  
  346. [stackq isEmpty not] whileTrue: [
  347. |bit|
  348. bit := stackq next.
  349. bitArray at: index put: bit.
  350. index := index + 1.
  351. ].
  352.  
  353. stdout << nl << nl.
  354.  
  355.  
  356.  
  357. infile := FileStream open: (Smalltalk arguments at: 2)
  358. mode: FileStream read.
  359.  
  360.  
  361.  
  362. stdout << 'second part of bitarray' <<nl.
  363.  
  364. [infile atEnd not] whileTrue: [
  365. |ordChar|
  366. ordChar := infile next asInteger.
  367.  
  368. index to: (index+((encode_table at: ordChar) size))-1 do:
  369. [ :x |
  370. bit := 0.
  371. (((encode_table at: ordChar) at: ((x-index)+1)) = $1) ifTrue: [bit:= 1.].
  372. bitArray at: x put: bit.
  373. stdout << (bitArray at: x put: bit).
  374. ].
  375.  
  376. index := (index+((encode_table at: ordChar) size)).
  377. ].
  378.  
  379. index to: ((arraylen+qlength)+extra) do: [
  380. :x |
  381. (bitArray at: x put: 0).
  382. ].
  383.  
  384. stdout << nl << nl << nl.
  385. bitArray do: [:bit | stdout << bit. ].
  386. stdout << nl.
  387.  
  388. infile close.
  389.  
  390.  
  391. writeBitArray := [:outfile|
  392. |outBytes|
  393. outBytes := OutBits new: outfile.
  394. bitArray do: [:bit| outBytes writeBit: bit.].
  395. outBytes flushByte.
  396. stdout<<nl.
  397. ].
  398.  
  399. Smalltalk arguments size = 2
  400. ifTrue: [writeBitArray value: stdout]
  401. ifFalse: [
  402. |outfilename|
  403. outfilename := Smalltalk arguments at: 3.
  404. [ |outfile|
  405. outfile := FileStream open: outfilename mode: FileStream write.
  406. writeBitArray value: outfile.
  407. outfile close.
  408. ] on: SystemExceptions.FileError do: [:signal |
  409. |errno|
  410. errno := File errno.
  411. stdout flush.
  412. stderr << execname << ': ' << filename << ': '
  413. << signal messageText << nl.
  414. stderr flush.
  415. ]
  416. ].
  417.  
  418. bitArray do: [:bit | stdout << bit. ].
  419. stdout << nl.
  420. stdout << nl.
  421. ].
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428. "-----------------------------------------------------------------"
  429. "decompression----------------------------------------------------"
  430.  
  431. FileStream extend [
  432. |bitIndex currentChar|
  433.  
  434. nextBit [
  435. |bit|
  436. bitIndex isNil ifTrue: [bitIndex := 0].
  437. bitIndex = 0 ifTrue: [
  438. bitIndex := 8.
  439. currentChar := self next.
  440. ].
  441. bit := currentChar value bitAt: bitIndex.
  442. bitIndex := bitIndex - 1.
  443. ^ bit
  444. ]
  445.  
  446. atBitEnd [
  447. ^ (bitIndex isNil | (bitIndex = 0)) & self atEnd
  448. ]
  449.  
  450. ]
  451.  
  452.  
  453. treeq := SortedCollection new.
  454.  
  455.  
  456.  
  457. infile := FileStream open: (Smalltalk arguments at: 2)
  458. mode: FileStream read.
  459.  
  460.  
  461. stdout <<'contents of file:'<<nl.
  462. [infile atBitEnd not] whileTrue: [
  463. | bit |
  464. bit := infile nextBit.
  465. stdout << bit.
  466. ].
  467. stdout << nl.
  468. infile close.
  469.  
  470.  
  471. infile := FileStream open: (Smalltalk arguments at: 2)
  472. mode: FileStream read.
  473.  
  474. treeq := OrderedCollection new.
  475. priority := 1000.
  476. endOfTree := 1.
  477.  
  478. [endOfTree = 1] whileTrue: [
  479. | bit |
  480. bit := infile nextBit.
  481.  
  482. bit = 1 ifTrue: [
  483. | char count |
  484. char := 255.
  485. count := 8.
  486. [count > 0 ] whileTrue: [
  487. count := count - 1.
  488. infile nextBit = 0 ifTrue: [
  489. num := 2 raisedTo: count.
  490. char := char - num.
  491. ]
  492. ].
  493. val := Leaf new: char asCharacter count: priority. "we do this because don't know freq"
  494. stdout <<'is this a leaf---------------------------------'<< (val isLeaf) << nl.
  495. priority := priority - 10.
  496. treeq addFirst: val.
  497. stdout << val <<nl.
  498. ]
  499. ifFalse: [ "for when we see a zero"
  500. treeq size = 1
  501. ifTrue: [
  502. endOfTree := 0. ]
  503. ifFalse: [
  504. |first second t|
  505. first := treeq removeFirst.
  506. second := treeq removeFirst.
  507.  
  508. t := Tree new: (first char) count: priority left: second right: first.
  509. priority := priority - 10.
  510. treeq addFirst: t.
  511. ].
  512.  
  513. ].
  514.  
  515. ].
  516.  
  517. t := treeq removeFirst.
  518. encode_table := ZeroArray new: 256.
  519.  
  520.  
  521. "creating an encoding table based of tree we created above ^"
  522. t inorder: [:char :string |
  523. encode_table at: char asInteger put: string.
  524. stdout << char << ' ' << string <<nl.
  525. ] prefix: ''.
  526.  
  527. tR := t right.
  528. stdout << tR isLeaf << nl.
  529. stdout << tR char <<nl.
  530.  
  531.  
  532. t_current := t.
  533. result := OrderedCollection new.
  534.  
  535. [infile atBitEnd not] whileTrue: [
  536. | bit |
  537.  
  538. t_current isLeaf = 1
  539. ifTrue: [
  540. result addLast: t_current char.
  541. stdout << t_current char.
  542. t_current := t.
  543. ]
  544. ifFalse: [
  545. bit := infile nextBit.
  546. bit = 1 ifTrue: [
  547. t_current := t_current right.
  548. ]
  549. ifFalse: [
  550. t_current := t_current left.
  551. ].
  552. ].
  553.  
  554. ].
  555.  
  556. infile close.
  557.  
  558. writeOutFile := [:outfile|
  559. [(result size = 1) not] whileTrue: [
  560. |letter|
  561. letter := result removeFirst.
  562. outfile nextPut: letter.
  563. ].
  564. ].
  565.  
  566. Smalltalk arguments size = 2
  567. ifTrue: [writeOutFile value: stdout]
  568. ifFalse: [
  569. |outfilename|
  570. outfilename := Smalltalk arguments at: 3.
  571. [ |outfile|
  572. outfile := FileStream open: outfilename
  573. mode: FileStream write.
  574. writeOutFile value: outfile.
  575. outfile close.
  576. ] on: SystemExceptions.FileError do: [:signal |
  577. |errno|
  578. errno := File errno.
  579. stdout flush.
  580. stderr << execname << ': ' << filename << ': '
  581. << signal messageText << nl.
  582. stderr flush.
  583. ]
  584. ].
  585.  
  586.  
  587.  
  588.  
  589.  
  590. "
  591.  
  592. infile := FileStream open: (Smalltalk arguments at: 2)
  593. mode: FileStream read.
  594.  
  595. Smalltalk arguments size = 2
  596. ifTrue: [
  597. [infile atEnd not] whileTrue: [
  598. |ordChar|
  599. ordChar := infile next asInteger.
  600. stdout << (encode_table at: ordChar)
  601. ].
  602. ]
  603. ifFalse: [stdout << 'hi' << nl.].
  604.  
  605. infile close.
  606. "
  607.  
  608.  
  609. " createLeaf := [:infile|
  610. | char count |
  611. char = 255.
  612. count = 8.
  613. [count > 0 ] whileTrue: [
  614. count := count - 1.
  615. nextbit = 0 ifTrue: [
  616. char := char - (2 raisedTo count).
  617. ].
  618. val := Leaf new: char asCharacter count: -1. we do this because don't know freq
  619. treeq nextPut val.
  620. ].
  621. b0 := infile nextBit.
  622.  
  623.  
  624. ]."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement