langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
97
Task/LZW-compression/Objective-C/lzw-compression-1.m
Normal file
97
Task/LZW-compression/Objective-C/lzw-compression-1.m
Normal 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
|
||||
26
Task/LZW-compression/Objective-C/lzw-compression-2.m
Normal file
26
Task/LZW-compression/Objective-C/lzw-compression-2.m
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue