March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,31 +1,31 @@
|
|||
import std.stdio, std.array;
|
||||
|
||||
auto compress(string original) {
|
||||
auto compress(in string original) pure nothrow {
|
||||
int[string] dict;
|
||||
foreach (b; 0 .. 256)
|
||||
dict[[cast(immutable char)b]] = b;
|
||||
foreach (immutable char c; char.min .. char.max + 1)
|
||||
dict[[c]] = c;
|
||||
|
||||
string w;
|
||||
int[] result;
|
||||
foreach (ch; original)
|
||||
foreach (immutable ch; original)
|
||||
if (w ~ ch in dict)
|
||||
w = w ~ ch;
|
||||
else {
|
||||
result ~= dict[w];
|
||||
dict[w ~ ch] = dict.length - 1;
|
||||
dict[w ~ ch] = dict.length;
|
||||
w = [ch];
|
||||
}
|
||||
return w.empty ? result : (result ~ dict[w]);
|
||||
}
|
||||
|
||||
auto decompress(int[] compressed) {
|
||||
auto dict = new string[256];
|
||||
foreach (b; 0 .. 256)
|
||||
dict[b] = [cast(char)b];
|
||||
auto decompress(in int[] compressed) pure nothrow {
|
||||
auto dict = new string[char.max - char.min + 1];
|
||||
foreach (immutable char c; char.min .. char.max + 1)
|
||||
dict[c] = [c];
|
||||
|
||||
auto w = dict[compressed[0]];
|
||||
auto result = w;
|
||||
foreach (k; compressed[1 .. $]) {
|
||||
foreach (immutable k; compressed[1 .. $]) {
|
||||
auto entry = (k < dict.length) ? dict[k] : w ~ w[0];
|
||||
result ~= entry;
|
||||
dict ~= w ~ entry[0];
|
||||
|
|
@ -35,6 +35,6 @@ auto decompress(int[] compressed) {
|
|||
}
|
||||
|
||||
void main() {
|
||||
auto comp = compress("TOBEORNOTTOBEORTOBEORNOT");
|
||||
writeln(comp, "\n", decompress(comp));
|
||||
auto comp = "TOBEORNOTTOBEORTOBEORNOT".compress;
|
||||
writeln(comp, "\n", comp.decompress);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ struct LZW {
|
|||
enum int initDictSize = 256;
|
||||
static immutable ubyte[initDictSize] bytes;
|
||||
static this() {
|
||||
foreach (immutable i; 0 .. initDictSize)
|
||||
bytes[i] = cast(T)i; //*
|
||||
foreach (immutable T i; 0 .. initDictSize)
|
||||
bytes[i] = i;
|
||||
}
|
||||
|
||||
static Tcomp[] compress(immutable scope T[] original) pure nothrow
|
||||
|
|
|
|||
33
Task/LZW-compression/Groovy/lzw-compression-1.groovy
Normal file
33
Task/LZW-compression/Groovy/lzw-compression-1.groovy
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
def compress = { text ->
|
||||
def dictionary = (1..255).inject([:]) { map, ch -> map."${(char)ch}" = ch; map }
|
||||
def w = '', compressed = []
|
||||
text.each { ch ->
|
||||
def wc = "$w$ch"
|
||||
if (dictionary[wc]) {
|
||||
w = wc
|
||||
} else {
|
||||
compressed << dictionary[w]
|
||||
dictionary[wc] = dictionary.size() + 1
|
||||
w = "$ch"
|
||||
}
|
||||
}
|
||||
if (w) { compressed << dictionary[w] }
|
||||
compressed
|
||||
}
|
||||
|
||||
def decompress = { compressed ->
|
||||
def dictionary = (1..255).inject([:]) { map, ch -> map[ch] = "${(char)ch}"; map }
|
||||
String w = "${(char)compressed.remove(0)}"
|
||||
StringBuffer result = new StringBuffer(w)
|
||||
compressed.each { k ->
|
||||
String entry = dictionary[k]
|
||||
if (!entry) {
|
||||
if (k != dictionary.size()) throw new IllegalArgumentException("Bad compressed k $k")
|
||||
entry = "$w${w[0]}"
|
||||
}
|
||||
result << entry
|
||||
dictionary[dictionary.size() + 1] = "$w${entry[0]}"
|
||||
w = entry
|
||||
}
|
||||
result.toString()
|
||||
}
|
||||
8
Task/LZW-compression/Groovy/lzw-compression-2.groovy
Normal file
8
Task/LZW-compression/Groovy/lzw-compression-2.groovy
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def plaintext = 'TOBEORNOTTOBEORTOBEORNOT'
|
||||
def compressed = compress(plaintext)
|
||||
def result = decompress(compressed)
|
||||
|
||||
println """\
|
||||
Plaintext: '$plaintext'
|
||||
Compressed: $compressed
|
||||
Uncompressed: '$result'""".stripIndent()
|
||||
|
|
@ -9,8 +9,8 @@
|
|||
NSUInteger codemark;
|
||||
}
|
||||
|
||||
-(LZWCompressor *) init;
|
||||
-(LZWCompressor *) initWithArray: (NSMutableArray *) stream;
|
||||
-(instancetype) init;
|
||||
-(instancetype) initWithArray: (NSMutableArray *) stream;
|
||||
-(BOOL) compressData: (NSData *) string;
|
||||
-(void) setArray: (NSMutableArray *) stream;
|
||||
-(NSArray *) getArray;
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
@implementation LZWCompressor : NSObject
|
||||
|
||||
-(LZWCompressor *) init
|
||||
-(instancetype) init
|
||||
{
|
||||
self = [super init];
|
||||
if ( self )
|
||||
|
|
@ -30,7 +30,7 @@
|
|||
return self;
|
||||
}
|
||||
|
||||
-(LZWCompressor *) initWithArray: (NSMutableArray *) stream
|
||||
-(instancetype) initWithArray: (NSMutableArray *) stream
|
||||
{
|
||||
self = [self init];
|
||||
if ( self )
|
||||
|
|
@ -40,51 +40,40 @@
|
|||
return self;
|
||||
}
|
||||
|
||||
-(void) dealloc
|
||||
{
|
||||
[dict release];
|
||||
[iostream release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
-(void) setArray: (NSMutableArray *) stream
|
||||
{
|
||||
iostream = [stream retain];
|
||||
iostream = stream;
|
||||
}
|
||||
|
||||
-(BOOL) compressData: (NSData *) string;
|
||||
{
|
||||
NSUInteger i;
|
||||
unsigned char j;
|
||||
|
||||
// prepare dict
|
||||
for(i=0; i < 256; i++)
|
||||
for(NSUInteger i=0; i < 256; i++)
|
||||
{
|
||||
j = i;
|
||||
unsigned char j = i;
|
||||
NSData *s = [NSData dataWithBytes: &j length: 1];
|
||||
[dict setObject: [NSNumber numberWithUnsignedInt: i] forKey: s];
|
||||
dict[s] = @(i);
|
||||
}
|
||||
|
||||
NSMutableData *w = [NSMutableData data];
|
||||
NSMutableData *wc = [NSMutableData data];
|
||||
NSData *w = [NSData data];
|
||||
|
||||
for(i=0; i < [string length]; i++)
|
||||
for(NSUInteger i=0; i < [string length]; i++)
|
||||
{
|
||||
[wc setData: w];
|
||||
NSMutableData *wc = [NSMutableData dataWithData: w];
|
||||
[wc appendData: [string subdataWithRange: NSMakeRange(i, 1)]];
|
||||
if ( [dict objectForKey: wc] != nil )
|
||||
if ( dict[wc] != nil )
|
||||
{
|
||||
[w setData: wc];
|
||||
w = wc;
|
||||
} else {
|
||||
[iostream addObject: [dict objectForKey: w]];
|
||||
[dict setObject: [NSNumber numberWithUnsignedInt: codemark] forKey: wc];
|
||||
[iostream addObject: dict[w]];
|
||||
dict[wc] = @(codemark);
|
||||
codemark++;
|
||||
[w setData: [string subdataWithRange: NSMakeRange(i, 1)]];
|
||||
w = [string subdataWithRange: NSMakeRange(i, 1)];
|
||||
}
|
||||
}
|
||||
if ( [w length] != 0 )
|
||||
{
|
||||
[iostream addObject: [dict objectForKey: w]];
|
||||
[iostream addObject: dict[w]];
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,21 @@
|
|||
const char *text = "TOBEORNOTTOBEORTOBEORNOT";
|
||||
NSString *text = @"TOBEORNOTTOBEORTOBEORNOT";
|
||||
|
||||
int main()
|
||||
{
|
||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
||||
@autoreleasepool {
|
||||
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
LZWCompressor *lzw = [[LZWCompressor alloc]
|
||||
initWithArray: array ];
|
||||
if ( lzw )
|
||||
{
|
||||
[lzw compressData: [text dataUsingEncoding: NSUTF8StringEncoding]];
|
||||
for ( id obj in array )
|
||||
{
|
||||
printf("%u\n", [obj unsignedIntValue]);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
64
Task/LZW-compression/PHP/lzw-compression.php
Normal file
64
Task/LZW-compression/PHP/lzw-compression.php
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
class LZW
|
||||
{
|
||||
function compress($unc) {
|
||||
$i;$c;$wc;
|
||||
$w = "";
|
||||
$dictionary = array();
|
||||
$result = array();
|
||||
$dictSize = 256;
|
||||
for ($i = 0; $i < 256; $i += 1) {
|
||||
$dictionary[chr($i)] = $i;
|
||||
}
|
||||
for ($i = 0; $i < strlen($unc); $i++) {
|
||||
$c = $unc[$i];
|
||||
if (property_exists($dictionary, $w.$c)) {
|
||||
$w = $w.$c;
|
||||
} else {
|
||||
array_push($result,$dictionary[$w]);
|
||||
$dictionary[$wc] = $dictSize++;
|
||||
$w = (string)$c;
|
||||
}
|
||||
}
|
||||
if ($w !== "") {
|
||||
array_push($result,$dictionary[$w]);
|
||||
}
|
||||
array_shift($result);
|
||||
return implode(",",$result);
|
||||
}
|
||||
|
||||
function decompress($com) {
|
||||
$com = explode(",",$com);
|
||||
$i;$w;$k;$result;
|
||||
$dictionary = array();
|
||||
$entry = "";
|
||||
$dictSize = 256;
|
||||
for ($i = 0; $i < 256; $i++) {
|
||||
$dictionary[$i] = chr($i);
|
||||
}
|
||||
$w = chr($com[0]);
|
||||
$result = $w;
|
||||
for ($i = 1; $i < count($com);$i++) {
|
||||
$k = $com[$i];
|
||||
if ($dictionary[$k]) {
|
||||
$entry = $dictionary[$k];
|
||||
} else {
|
||||
if ($k === $dictSize) {
|
||||
$entry = $w.$w[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
$result .= $entry;
|
||||
$dictionary[$dictSize++] = $w + $entry[0];
|
||||
$w = $entry;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
//How to use
|
||||
$str = 'TOBEORNOTTOBEORTOBEORNOT';
|
||||
$lzw = new LZW();
|
||||
$com = $lzw->compress($str);
|
||||
$dec = $lzw->decompress($com);
|
||||
echo $com . "<br>" . $dec;
|
||||
Loading…
Add table
Add a link
Reference in a new issue