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,50 @@
type 'a huffman_tree =
| Leaf of 'a
| Node of 'a huffman_tree * 'a huffman_tree
module HSet = Set.Make
(struct
type t = int * char huffman_tree (* pair of frequency and the tree *)
let compare = compare
(* We can use the built-in compare function to order this: it will order
first by the first element (frequency) and then by the second (the tree),
the latter of which we don't care about but which helps prevent elements
from being equal, since Set does not allow duplicate elements *)
end);;
let build_tree charFreqs =
let leaves = List.fold_left (fun z (c,f) -> HSet.add (f, Leaf c) z) HSet.empty charFreqs in
let rec aux trees =
let f1, a = HSet.min_elt trees in
let trees' = HSet.remove (f1,a) trees in
if HSet.is_empty trees' then
a
else
let f2, b = HSet.min_elt trees' in
let trees'' = HSet.remove (f2,b) trees' in
let trees''' = HSet.add (f1 + f2, Node (a, b)) trees'' in
aux trees'''
in
aux leaves
let rec print_tree code = function
| Leaf c ->
Printf.printf "%c\t%s\n" c (String.concat "" (List.rev code));
| Node (l, r) ->
print_tree ("0"::code) l;
print_tree ("1"::code) r
let () =
let str = "this is an example for huffman encoding" in
let charFreqs = Hashtbl.create 42 in
String.iter (fun c ->
let old =
try Hashtbl.find charFreqs c
with Not_found -> 0 in
Hashtbl.replace charFreqs c (old+1)
) str;
let charFreqs = Hashtbl.fold (fun c f acc -> (c,f)::acc) charFreqs [] in
let tree = build_tree charFreqs in
print_string "Symbol\tHuffman code\n";
print_tree [] tree

View file

@ -0,0 +1,156 @@
#import <Foundation/Foundation.h>
@interface HuffmanTree : NSObject {
int freq;
}
-(id)initWithFreq:(int)f;
@property (readonly) int freq;
@end
@implementation HuffmanTree
@synthesize freq; // the frequency of this tree
-(id)initWithFreq:(int)f {
if (self = [super init]) {
freq = f;
}
return self;
}
@end
const void *HuffmanRetain(CFAllocatorRef allocator, const void *ptr) {
return [(id)ptr retain];
}
void HuffmanRelease(CFAllocatorRef allocator, const void *ptr) {
[(id)ptr release];
}
CFComparisonResult HuffmanCompare(const void *ptr1, const void *ptr2, void *unused) {
int f1 = ((HuffmanTree *)ptr1).freq;
int f2 = ((HuffmanTree *)ptr2).freq;
if (f1 == f2)
return kCFCompareEqualTo;
else if (f1 > f2)
return kCFCompareGreaterThan;
else
return kCFCompareLessThan;
}
@interface HuffmanLeaf : HuffmanTree {
char value; // the character this leaf represents
}
@property (readonly) char value;
-(id)initWithFreq:(int)f character:(char)c;
@end
@implementation HuffmanLeaf
@synthesize value;
-(id)initWithFreq:(int)f character:(char)c {
if (self = [super initWithFreq:f]) {
value = c;
}
return self;
}
@end
@interface HuffmanNode : HuffmanTree {
HuffmanTree *left, *right; // subtrees
}
@property (readonly) HuffmanTree *left, *right;
-(id)initWithLeft:(HuffmanTree *)l right:(HuffmanTree *)r;
@end
@implementation HuffmanNode
@synthesize left, right;
-(id)initWithLeft:(HuffmanTree *)l right:(HuffmanTree *)r {
if (self = [super initWithFreq:l.freq+r.freq]) {
left = [l retain];
right = [r retain];
}
return self;
}
-(void)dealloc {
[left release];
[right release];
[super dealloc];
}
@end
HuffmanTree *buildTree(NSCountedSet *chars) {
CFBinaryHeapCallBacks callBacks = {0, HuffmanRetain, HuffmanRelease, NULL, HuffmanCompare};
CFBinaryHeapRef trees = CFBinaryHeapCreate(NULL, 0, &callBacks, NULL);
// initially, we have a forest of leaves
// one for each non-empty character
for (NSNumber *ch in chars) {
int freq = [chars countForObject:ch];
if (freq > 0)
CFBinaryHeapAddValue(trees, [[[HuffmanLeaf alloc] initWithFreq:freq character:(char)[ch intValue]] autorelease]);
}
NSCAssert(CFBinaryHeapGetCount(trees) > 0, @"String must have at least one character");
// loop until there is only one tree left
while (CFBinaryHeapGetCount(trees) > 1) {
// two trees with least frequency
HuffmanTree *a = (HuffmanTree *)CFBinaryHeapGetMinimum(trees);
CFBinaryHeapRemoveMinimumValue(trees);
HuffmanTree *b = (HuffmanTree *)CFBinaryHeapGetMinimum(trees);
CFBinaryHeapRemoveMinimumValue(trees);
// put into new node and re-insert into queue
CFBinaryHeapAddValue(trees, [[[HuffmanNode alloc] initWithLeft:a right:b] autorelease]);
}
HuffmanTree *result = [(HuffmanTree *)CFBinaryHeapGetMinimum(trees) retain];
CFRelease(trees);
return [result autorelease];
}
void printCodes(HuffmanTree *tree, NSMutableString *prefix) {
NSCAssert(tree != nil, @"tree must not be nil");
if ([tree isKindOfClass:[HuffmanLeaf class]]) {
HuffmanLeaf *leaf = (HuffmanLeaf *)tree;
// print out character, frequency, and code for this leaf (which is just the prefix)
NSLog(@"%c\t%d\t%@", leaf.value, leaf.freq, prefix);
} else if ([tree isKindOfClass:[HuffmanNode class]]) {
HuffmanNode *node = (HuffmanNode *)tree;
// traverse left
[prefix appendString:@"0"];
printCodes(node.left, prefix);
[prefix deleteCharactersInRange:NSMakeRange([prefix length]-1, 1)];
// traverse right
[prefix appendString:@"1"];
printCodes(node.right, prefix);
[prefix deleteCharactersInRange:NSMakeRange([prefix length]-1, 1)];
}
}
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *test = @"this is an example for huffman encoding";
// read each character and record the frequencies
NSCountedSet *chars = [[NSCountedSet alloc] init];
int n = [test length];
for (int i = 0; i < n; i++)
[chars addObject:[NSNumber numberWithInt:[test characterAtIndex:i]]];
// build tree
HuffmanTree *tree = buildTree(chars);
[chars release];
// print out results
NSLog(@"SYMBOL\tWEIGHT\tHUFFMAN CODE");
printCodes(tree, [NSMutableString string]);
[pool drain];
return 0;
}

View file

@ -0,0 +1,17 @@
sub huffman ($s) {
my $de = $s.chars;
my @q = $s.comb.classify({$_}).map({[+.value / $de, .key]}).sort;
while @q > 1 {
my ($a,$b) = @q.splice(0,2);
@q = sort [$a[0] + $b[0], [$a[1], $b[1]]], @q;
}
sort *.value, gather walk @q[0][1], '';
}
multi walk (@node, $prefix) {
walk @node[0], $prefix ~ 1;
walk @node[1], $prefix ~ 0;
}
multi walk ($node, $prefix) { take $node => $prefix }
say .perl for huffman('this is an example for huffman encoding');

View file

@ -0,0 +1,9 @@
my $str = 'this is an example for huffman encoding';
my %enc = huffman $str;
my %dec = %enc.invert;
say $str;
my $huf = %enc{$str.comb}.join;
say $huf;
my $rx = join('|', map { "'" ~ .key ~ "'" }, %dec);
$rx = eval '/' ~ $rx ~ '/';
say $huf.subst(/<$rx>/, -> $/ {%dec{~$/}}, :g);

View file

@ -0,0 +1,83 @@
OpenConsole()
SampleString.s="this is an example for huffman encoding"
datalen=Len(SampleString)
Structure ztree
linked.c
ischar.c
char.c
number.l
left.l
right.l
EndStructure
Dim memc.c(0)
memc()=@SampleString
Dim tree.ztree(255)
For i=0 To datalen-1
tree(memc(i))\char=memc(i)
tree(memc(i))\number+1
tree(memc(i))\ischar=1
Next
SortStructuredArray(tree(),#PB_Sort_Descending,OffsetOf(ztree\number),#PB_Sort_Character)
For i=0 To 255
If tree(i)\number=0
ReDim tree(i-1)
Break
EndIf
Next
dimsize=ArraySize(tree())
Repeat
min1.l=0
min2.l=0
For i=0 To dimsize
If tree(i)\linked=0
If tree(i)\number<min1 Or min1=0
min1=tree(i)\number
hmin1=i
ElseIf tree(i)\number<min2 Or min2=0
min2=tree(i)\number
hmin2=i
EndIf
EndIf
Next
If min1=0 Or min2=0
Break
EndIf
dimsize+1
ReDim tree(dimsize)
tree(dimsize)\number=tree(hmin1)\number+tree(hmin2)\number
tree(hmin1)\left=dimsize
tree(hmin2)\right=dimsize
tree(hmin1)\linked=1
tree(hmin2)\linked=1
ForEver
i=0
While tree(i)\ischar=1
str.s=""
k=i
ZNEXT:
If tree(k)\left<>0
str="0"+str
k=tree(k)\left
Goto ZNEXT
ElseIf tree(k)\right<>0
str="1"+str
k=tree(k)\right
Goto ZNEXT
EndIf
PrintN(Chr(tree(i)\char)+" "+str)
i+1
Wend
Input()
CloseConsole()

View file

@ -0,0 +1,38 @@
var forest := {}, encTab := {};
plaintext := 'this is an example for huffman encoding';
ft := {};
(for c in plaintext)
ft(c) +:= 1;
end;
forest := {[f, c]: [c, f] in ft};
(while 1 < #forest)
[f1, n1] := getLFN();
[f2, n2] := getLFN();
forest with:= [f1+f2, [n1,n2]];
end;
addToTable('', arb range forest);
(for e = encTab(c))
print(c, ft(c), e);
end;
print(+/ [encTab(c): c in plaintext]);
proc addToTable(prefix, node);
if is_tuple node then
addToTable(prefix + '0', node(1));
addToTable(prefix + '1', node(2));
else
encTab(node) := prefix;
end;
end proc;
proc getLFN();
f := min/ domain forest;
n := arb forest{f};
forest less:= [f, n];
return [f, n];
end proc;

View file

@ -0,0 +1,57 @@
datatype 'a huffman_tree =
Leaf of 'a
| Node of 'a huffman_tree * 'a huffman_tree
structure HuffmanPriority = struct
type priority = int
(* reverse comparison to achieve min-heap *)
fun compare (a, b) = Int.compare (b, a)
type item = int * char huffman_tree
val priority : item -> int = #1
end
structure HPQueue = LeftPriorityQFn (HuffmanPriority)
fun buildTree charFreqs = let
fun aux trees = let
val ((f1,a), trees) = HPQueue.remove trees
in
if HPQueue.isEmpty trees then
a
else let
val ((f2,b), trees) = HPQueue.remove trees
val trees = HPQueue.insert ((f1 + f2, Node (a, b)),
trees)
in
aux trees
end
end
val trees = HPQueue.fromList (map (fn (c,f) => (f, Leaf c)) charFreqs)
in
aux trees
end
fun printCodes (revPrefix, Leaf c) =
print (String.str c ^ "\t" ^
implode (rev revPrefix) ^ "\n")
| printCodes (revPrefix, Node (l, r)) = (
printCodes (#"0"::revPrefix, l);
printCodes (#"1"::revPrefix, r)
);
let
val test = "this is an example for huffman encoding"
val charFreqs = HashTable.mkTable
(HashString.hashString o String.str, op=)
(42, Empty)
val () =
app (fn c =>
let val old = getOpt (HashTable.find charFreqs c, 0)
in HashTable.insert charFreqs (c, old+1)
end)
(explode test)
val tree = buildTree (HashTable.listItemsi charFreqs)
in
print "SYMBOL\tHUFFMAN CODE\n";
printCodes ([], tree)
end

View file

@ -0,0 +1,14 @@
#import std
#import nat
#import flo
code_table = # takes a training dataset to a table <char: code...>
-+
*^ ~&v?\~&iNC @v ~&t?\~&h ~&plrDSLrnPlrmPCAS/'01',
~&itB->h fleq-<&d; ^C\~&tt @hthPX ^V\~&lrNCC plus@bd,
^V(div@rrPlX,~&rlNVNC)^*D(plus:-0.@rS,~&)+ *K2 ^/~&h float+ length+-
#cast %csAL
table = code_table 'this is an example for huffman encoding'