langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,80 @@
#directory "+extlib" (* or maybe "+site-lib/extlib/" *)
#load "extLib.cma"
open ExtString
(** compress a string to a list of output symbols *)
let compress ~uncompressed =
(* build the dictionary *)
let dict_size = 256 in
let dictionary = Hashtbl.create 397 in
for i=0 to 255 do
let str = String.make 1 (char_of_int i) in
Hashtbl.add dictionary str i
done;
let f = (fun (w, dict_size, result) c ->
let c = String.make 1 c in
let wc = w ^ c in
if Hashtbl.mem dictionary wc then
(wc, dict_size, result)
else
begin
(* add wc to the dictionary *)
Hashtbl.add dictionary wc dict_size;
let this = Hashtbl.find dictionary w in
(c, dict_size + 1, this::result)
end
) in
let w, _, result =
String.fold_left f ("", dict_size, []) uncompressed
in
(* output the code for w *)
let result =
if w = ""
then result
else (Hashtbl.find dictionary w) :: result
in
(List.rev result)
;;
exception ValueError of string
(** decompress a list of output symbols to a string *)
let decompress ~compressed =
(* build the dictionary *)
let dict_size = 256 in
let dictionary = Hashtbl.create 397 in
for i=0 to pred dict_size do
let str = String.make 1 (char_of_int i) in
Hashtbl.add dictionary i str
done;
let w, compressed =
match compressed with
| hd::tl -> (String.make 1 (char_of_int hd)), tl
| [] -> failwith "empty input"
in
let result = [w] in
let result, _, _ =
List.fold_left (fun (result, w, dict_size) k ->
let entry =
if Hashtbl.mem dictionary k then
Hashtbl.find dictionary k
else if k = Hashtbl.length dictionary then
w ^ (String.make 1 w.[0])
else
raise(ValueError(Printf.sprintf "Bad compressed k: %d" k))
in
let result = entry :: result in
(* add (w ^ entry.[0]) to the dictionary *)
Hashtbl.add dictionary dict_size (w ^ (String.make 1 entry.[0]));
(result, entry, dict_size + 1)
) (result, w, dict_size) compressed
in
(List.rev result)
;;

View file

@ -0,0 +1,2 @@
val compress : uncompressed:string -> int list
val decompress : compressed:int list -> string list

View file

@ -0,0 +1,39 @@
let greatest = List.fold_left max 0 ;;
(** number of bits needed to encode the integer m *)
let n_bits m =
let m = float m in
let rec aux n =
let max = (2. ** n) -. 1. in
if max >= m then int_of_float n
else aux (n +. 1.0)
in
aux 1.0
;;
let write_compressed ~filename ~compressed =
let nbits = n_bits(greatest compressed) in
let oc = open_out filename in
output_byte oc nbits;
let ob = IO.output_bits(IO.output_channel oc) in
List.iter (IO.write_bits ob nbits) compressed;
IO.flush_bits ob;
close_out oc;
;;
let read_compressed ~filename =
let ic = open_in filename in
let nbits = input_byte ic in
let ib = IO.input_bits(IO.input_channel ic) in
let rec loop acc =
try
let code = IO.read_bits ib nbits in
loop (code::acc)
with _ -> List.rev acc
in
let compressed = loop [] in
let result = decompress ~compressed in
let buf = Buffer.create 2048 in
List.iter (Buffer.add_string buf) result;
(Buffer.contents buf)
;;

View file

@ -0,0 +1,97 @@
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface LZWCompressor : NSObject
{
@private
NSMutableArray *iostream;
NSMutableDictionary *dict;
NSUInteger codemark;
}
-(LZWCompressor *) init;
-(LZWCompressor *) initWithArray: (NSMutableArray *) stream;
-(BOOL) compressData: (NSData *) string;
-(void) setArray: (NSMutableArray *) stream;
-(NSArray *) getArray;
@end
@implementation LZWCompressor : NSObject
-(LZWCompressor *) init
{
self = [super init];
if ( self )
{
iostream = nil;
codemark = 256;
dict = [[NSMutableDictionary alloc] initWithCapacity: 512];
}
return self;
}
-(LZWCompressor *) initWithArray: (NSMutableArray *) stream
{
self = [self init];
if ( self )
{
[self setArray: stream];
}
return self;
}
-(void) dealloc
{
[dict release];
[iostream release];
[super dealloc];
}
-(void) setArray: (NSMutableArray *) stream
{
iostream = [stream retain];
}
-(BOOL) compressData: (NSData *) string;
{
NSUInteger i;
unsigned char j;
// prepare dict
for(i=0; i < 256; i++)
{
j = i;
NSData *s = [NSData dataWithBytes: &j length: 1];
[dict setObject: [NSNumber numberWithUnsignedInt: i] forKey: s];
}
NSMutableData *w = [NSMutableData data];
NSMutableData *wc = [NSMutableData data];
for(i=0; i < [string length]; i++)
{
[wc setData: w];
[wc appendData: [string subdataWithRange: NSMakeRange(i, 1)]];
if ( [dict objectForKey: wc] != nil )
{
[w setData: wc];
} else {
[iostream addObject: [dict objectForKey: w]];
[dict setObject: [NSNumber numberWithUnsignedInt: codemark] forKey: wc];
codemark++;
[w setData: [string subdataWithRange: NSMakeRange(i, 1)]];
}
}
if ( [w length] != 0 )
{
[iostream addObject: [dict objectForKey: w]];
}
return YES;
}
-(NSArray *) getArray
{
return iostream;
}
@end

View file

@ -0,0 +1,26 @@
const char *text = "TOBEORNOTTOBEORTOBEORNOT";
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
LZWCompressor *lzw = [[LZWCompressor alloc]
initWithArray: array ];
if ( lzw )
{
[lzw compressData: [NSData dataWithBytes: text
length: strlen(text)]];
NSEnumerator *en = [array objectEnumerator];
id obj;
while( (obj = [en nextObject]) )
{
printf("%u\n", [obj unsignedIntValue]);
}
[lzw release];
}
[array release];
[pool release];
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,43 @@
sub compress(Str $uncompressed --> List) {
my $dict-size = 256;
my %dictionary = (.chr => .chr for ^$dict-size);
my $w = "";
gather {
for $uncompressed.comb -> $c {
my $wc = $w ~ $c;
if %dictionary{$wc}:exists { $w = $wc }
else {
take %dictionary{$w};
%dictionary{$wc} = +%dictionary;
$w = $c;
}
}
take %dictionary{$w} if $w.chars;
}
}
sub decompress(@compressed --> Str) {
my $dict-size = 256;
my %dictionary = (.chr => .chr for ^$dict-size);
my $w = shift @compressed;
join '', gather {
take $w;
for @compressed -> $k {
my $entry;
if %dictionary{$k}:exists { take $entry = %dictionary{$k} }
elsif $k == $dict-size { take $entry = $w ~ $w.substr(0,1) }
else { die "Bad compressed k: $k" }
%dictionary{$dict-size++} = $w ~ $entry.substr(0,1);
$w = $entry;
}
}
}
my @compressed = compress('TOBEORNOTTOBEORTOBEORNOT');
say @compressed;
my $decompressed = decompress(@compressed);
say $decompressed;

View file

@ -0,0 +1,97 @@
Procedure compress(uncompressed.s, List result.u())
;Compress a string to a list of output symbols
;Build the dictionary.
Protected dict_size = 255, i
newmap dict.u()
For i = 0 To 254
dict(Chr(i + 1)) = i
Next
Protected w.s, wc.s, *c.Character = @uncompressed
w = ""
LastElement(result())
While *c\c <> #Null
wc = w + Chr(*c\c)
If FindMapElement(dict(), wc)
w = wc
Else
AddElement(result())
result() = dict(w)
;Add wc to the dictionary
dict(wc) = dict_size
dict_size + 1 ;no check is performed for overfilling the dictionary.
w = Chr(*c\c)
EndIf
*c + 1
Wend
;Output the code for w
If w
AddElement(result())
result() = dict(w)
EndIf
EndProcedure
Procedure.s decompress(List compressed.u())
;Decompress a list of encoded values to a string
If ListSize(compressed()) = 0: ProcedureReturn "": EndIf
;Build the dictionary.
Protected dict_size = 255, i
Dim dict.s(255)
For i = 1 To 255
dict(i - 1) = Chr(i)
Next
Protected w.s, entry.s, result.s
FirstElement(compressed())
w = dict(compressed())
result = w
i = 0
While NextElement(compressed())
i + 1
If compressed() < dict_size
entry = dict(compressed())
ElseIf i = dict_size
entry = w + Left(w, 1)
Else
MessageRequester("Error","Bad compression at [" + Str(i) + "]")
ProcedureReturn result;abort
EndIf
result + entry
;Add w + Left(entry, 1) to the dictionary
If ArraySize(dict()) <= dict_size
Redim dict(dict_size + 256)
EndIf
dict(dict_size) = w + Left(entry, 1)
dict_size + 1 ;no check is performed for overfilling the dictionary.
w = entry
Wend
ProcedureReturn result
EndProcedure
If OpenConsole()
;How to use:
Define initial.s, decompressed.s
Print("Type something: ")
initial = Input()
NewList compressed.u()
compress(initial, compressed())
ForEach compressed()
Print(Str(compressed()) + " ")
Next
PrintN("")
decompressed = decompress(compressed())
PrintN(decompressed)
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,75 @@
$ include "seed7_05.s7i";
const func string: lzwCompress (in string: uncompressed) is func
result
var string: result is "";
local
var char: ch is ' ';
var hash [string] char: mydict is (hash [string] char).value;
var string: buffer is "";
var string: xstr is "";
begin
for ch range chr(0) to chr(255) do
mydict @:= [str(ch)] ch;
end for;
for ch range uncompressed do
xstr := buffer & str(ch);
if xstr in mydict then
buffer &:= str(ch)
else
result &:= str(mydict[buffer]);
mydict @:= [xstr] chr(length(mydict));
buffer := str(ch);
end if;
end for;
if buffer <> "" then
result &:= str(mydict[buffer]);
end if;
end func;
const func string: lzwDecompress (in string: compressed) is func
result
var string: result is "";
local
var char: ch is ' ';
var hash [char] string: mydict is (hash [char] string).value;
var string: buffer is "";
var string: current is "";
var string: chain is "";
begin
for ch range chr(0) to chr(255) do
mydict @:= [ch] str(ch);
end for;
for ch range compressed do
if buffer = "" then
buffer := mydict[ch];
result &:= buffer;
elsif ch <= chr(255) then
current := mydict[ch];
result &:= current;
chain := buffer & current;
mydict @:= [chr(length(mydict))] chain;
buffer := current;
else
if ch in mydict then
chain := mydict[ch];
else
chain := buffer & str(buffer[1]);
end if;
result &:= chain;
mydict @:= [chr(length(mydict))] buffer & str(chain[1]);
buffer := chain;
end if;
end for;
end func;
const proc: main is func
local
var string: compressed is "";
var string: uncompressed is "";
begin
compressed := lzwCompress("TOBEORNOTTOBEORTOBEORNOT");
writeln(literal(compressed));
uncompressed := lzwDecompress(compressed);
writeln(uncompressed);
end func;