Object subclass: #LSHuffman instanceVariableNames: 'text forest codes note' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Huffman'! Morph subclass: #LSHuffmanView instanceVariableNames: 'model window' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Huffman'! !LSHuffman methodsFor: 'tutorial'! initialize super initialize. self load: 'ABRACADABRA' ! ! !LSHuffman methodsFor: 'tutorial'! load: aString | counts | text := aString copy. codes := Dictionary new. counts := Dictionary new. text do: [:ch | counts at: ch put: (counts at: ch ifAbsent: [0]) + 1]. forest := SortedCollection sortBlock: [:a :b | a first = b first ifTrue: [a second <= b second] ifFalse: [a first < b first]]. counts keysAndValuesDo: [:ch :count | forest add: {count. ch asString. nil. nil}]. note := 'Count each symbol; merge the two lightest trees.'. forest size = 1 ifTrue: [self assign: forest first prefix: ''] ! ! !LSHuffman methodsFor: 'tutorial'! merge | left right parent | forest size < 2 ifTrue: [^ false]. left := forest removeFirst. right := forest removeFirst. parent := {left first + right first. left second, right second. left. right}. forest add: parent. note := 'Merged ', left second, ' + ', right second, ' = ', parent first printString. forest size = 1 ifTrue: [self assign: forest first prefix: '']. ^ true ! ! !LSHuffman methodsFor: 'tutorial'! finish [self merge] whileTrue. ^ self ! ! !LSHuffman methodsFor: 'tutorial'! assign: node prefix: prefix (node at: 3) isNil ifTrue: [codes at: node second first put: (prefix isEmpty ifTrue: ['0'] ifFalse: [prefix])] ifFalse: [self assign: (node at: 3) prefix: prefix, '0'. self assign: (node at: 4) prefix: prefix, '1'] ! ! !LSHuffman methodsFor: 'tutorial'! encode forest size > 1 ifTrue: [self error: 'Finish the tree first']. ^ String streamContents: [:stream | text do: [:ch | stream nextPutAll: (codes at: ch)]] ! ! !LSHuffman methodsFor: 'tutorial'! decode: bits | root node | forest size > 1 ifTrue: [self error: 'Finish the tree first']. (bits allSatisfy: [:ch | ch = $0 or: [ch = $1]]) ifFalse: [self error: 'Only 0 and 1 are allowed']. forest isEmpty ifTrue: [bits isEmpty ifFalse: [self error: 'No tree']. ^ '']. root := forest first. (root at: 3) isNil ifTrue: [ (bits allSatisfy: [:ch | ch = $0]) ifFalse: [self error: 'Single-symbol code is 0']. ^ String new: bits size withAll: root second first]. node := root. ^ String streamContents: [:stream | bits do: [:bit | node := node at: (bit = $0 ifTrue: [3] ifFalse: [4]). (node at: 3) isNil ifTrue: [stream nextPut: node second first. node := root]]. node == root ifFalse: [self error: 'Incomplete final code']] ! ! !LSHuffman methodsFor: 'tutorial'! text ^ text ! ! !LSHuffman methodsFor: 'tutorial'! forest ^ forest asArray ! ! !LSHuffman methodsFor: 'tutorial'! codes ^ codes copy ! ! !LSHuffman methodsFor: 'tutorial'! note ^ note ! ! !LSHuffmanView class methodsFor: 'tutorial'! open ^ self new open ! ! !LSHuffmanView methodsFor: 'tutorial'! initialize super initialize. model := LSHuffman new. self color: (Color fromString: '#F1F6F4'); extent: 1000@620. self button: 'One merge' action: #merge at: 25@560. self button: 'Finish tree' action: #finish at: 225@560. self button: 'Reset sample' action: #reset at: 425@560. self button: 'Try MISSISSIPPI' action: #alternate at: 625@560 ! ! !LSHuffmanView methodsFor: 'tutorial'! button: label action: selector at: point | b | b := SimpleButtonMorph new target: self; label: label; actionSelector: selector. b extent: 180@35; position: point. self addMorph: b ! ! !LSHuffmanView methodsFor: 'tutorial'! open window := SystemWindow new model: self; setLabel: 'Huffman coding laboratory - LearnSqueak'. window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)). window openInWorld; extent: 1020@670. ^ self ! ! !LSHuffmanView methodsFor: 'tutorial'! model ^ model ! ! !LSHuffmanView methodsFor: 'tutorial'! window ^ window ! ! !LSHuffmanView methodsFor: 'tutorial'! okToClose ^ true ! ! !LSHuffmanView methodsFor: 'tutorial'! release ^ self ! ! !LSHuffmanView methodsFor: 'tutorial'! windowIsClosing window := nil ! ! !LSHuffmanView methodsFor: 'tutorial'! merge model merge. self changed ! ! !LSHuffmanView methodsFor: 'tutorial'! finish model finish. self changed ! ! !LSHuffmanView methodsFor: 'tutorial'! reset model load: 'ABRACADABRA'. self changed ! ! !LSHuffmanView methodsFor: 'tutorial'! alternate model load: 'MISSISSIPPI'. model finish. self changed ! ! !LSHuffmanView methodsFor: 'tutorial'! text: string on: canvas at: point size: size color: shade canvas drawString: string at: self position + point font: (StrikeFont familyName: 'DejaVu Sans' pointSize: size) color: shade ! ! !LSHuffmanView methodsFor: 'tutorial'! drawNode: node on: canvas x: x y: y width: width | rect childX | (node at: 3) ifNotNil: [ childX := x - (width / 4). canvas line: self position + (x@y) to: self position + (childX@(y+65)) width: 2 color: Color gray. self text: '0' on: canvas at: ((x+childX)/2)@(y+17) size: 11 color: Color black. self drawNode: (node at: 3) on: canvas x: childX y: y+65 width: width/2. childX := x + (width / 4). canvas line: self position + (x@y) to: self position + (childX@(y+65)) width: 2 color: Color gray. self text: '1' on: canvas at: ((x+childX)/2)@(y+17) size: 11 color: Color black. self drawNode: (node at: 4) on: canvas x: childX y: y+65 width: width/2]. rect := (self position + ((x-27)@(y-12))) extent: 54@29. canvas fillRectangle: rect color: (Color fromString: '#246C73'). self text: ((node at: 3) isNil ifTrue: [node second, ':', node first printString] ifFalse: [node first printString]) on: canvas at: (x-21)@(y-9) size: 11 color: Color white ! ! !LSHuffmanView methodsFor: 'tutorial'! drawOn: canvas | ink muted roots width x bits row | super drawOn: canvas. ink := Color fromString: '#173E48'. muted := Color fromString: '#58717A'. self text: 'HUFFMAN CODING: SMALL WEIGHTS MERGE FIRST' on: canvas at: 24@16 size: 21 color: ink. self text: 'Source: ', model text on: canvas at: 24@50 size: 17 color: ink. self text: model note on: canvas at: 24@82 size: 14 color: muted. roots := model forest. width := 950 / (roots size max: 1). 1 to: roots size do: [:i | x := 25 + ((i-0.5)*width). self drawNode: (roots at: i) on: canvas x: x y: 135 width: width]. roots size = 1 ifTrue: [ row := 0. model codes keys asSortedCollection do: [:ch | self text: ch asString, ' = ', (model codes at: ch) on: canvas at: (25+(row*180))@410 size: 16 color: ink. row := row+1]. bits := model encode. self text: 'Payload: ', bits on: canvas at: 25@450 size: 13 color: ink. self text: bits size printString, ' payload bits; decoded: ', (model decode: bits) on: canvas at: 25@480 size: 15 color: ink]. self text: 'Tree metadata costs extra. A bit string here is text, not a packed compressed file.' on: canvas at: 25@520 size: 13 color: muted ! !