LZW compression
Übersicht BlitzMax, BlitzMax NG Codearchiv & Module
RoneBetreff: LZW compression |
Do, Feb 09, 2017 16:19 Antworten mit Zitat |
|
---|---|---|
https://rosettacode.org/wiki/LZW_compression
Code: [AUSKLAPPEN] Function lzw_compress:Int[](str:String)
'' Build the dictionary. Local dictSize = 256; Local dict:TMap = CreateMap() For Local i = 0 Until 256 dict.insert(String(i) ,Box.Create(i)) Next Local w:String = "" Local result:Int[] = New Int[0] For Local i = 0 Until str.length Local c:String = str[i] Local wc:String = w + c; If MapContains(dict,wc) w = wc; Else result = result[..result.length+1] result[result.length-1] = Box(dict.ValueForKey(w)).Value '' Add wc To the dictionary. dict.Insert(wc, Box.Create(dictSize)) dictSize:+1 w = "" + c; EndIf Next '' Output the code For w. If (w <> "") result = result[..result.length+1] result[result.length-1] = Box(dict.ValueForKey(w)).Value EndIf Print "dictSize : " + dictSize Return result; EndFunction Function lzw_decompress:String(compressed:Int[]) '' Build the dictionary. Local dictSize =256; Local dict:TMap = CreateMap() For Local i =08 Until 256 dict.insert(String(i), ""+ Chr(i) ) Next Local w: String = "" + Chr(compressed[0]) Local result:String = "" + w For Local i = 1 Until compressed.length Local k = compressed[i] Local entry:String If ( MapContains(dict,String(k)) ) entry = String(dict.ValueForKey(String(k))); Else If (k = dictSize) entry = w + Chr(w[0]); Else Throw ("Bad compressed k: " + k); EndIf result:+ entry dict.Insert(String(dictSize), w + Chr(entry[0])); dictSize:+1 w = entry; Next Return result; EndFunction Type Box Field Value:Int Function Create:Box(i) Local b:Box = New Box b.Value = i Return b EndFunction EndType |
||
DAK |
Fr, Feb 10, 2017 12:42 Antworten mit Zitat |
|
---|---|---|
Respekt, was mit so wenig Code geht!
Allerdings hast du einen etwas eigenwilligen Programmierstil, wenn du mir die Bemerkung erlaubst. Deine Einrückungen sind nicht wirklich sauber und es scheint, als hättest du Strichpunkte am Ende jeder Zeile aus C übernommen. Auch zählt es unter BMax als sauber, wenn Ifs mit einem Then beendet werden. Gute Arbeit ansonsten! |
||
Gewinner der 6. und der 68. BlitzCodeCompo |
Übersicht BlitzMax, BlitzMax NG Codearchiv & Module
Powered by phpBB © 2001 - 2006, phpBB Group