Run-length encoding/Objective-C: Difference between revisions

m
modernize
(moved from Run-length encoding)
 
m (modernize)
 
Line 12:
NSMutableArray *counters;
}
-+ (idinstancetype)initWithDataencoderWithData: (NSData *)data;
+ (id)alloc;
+- (idinstancetype)encoderWithDatainitWithData: (NSData *)data;
- (id)init;
- (id)initWithData: (NSData *)data;
- (void)dealloc;
- (void)addByte: (char)aByte;
- (void)addByte: (char)aByte repeated: (int)repetitionCount;
Line 27 ⟶ 24:
 
@implementation RCRunLengthEncoder
+ (idinstancetype)encoderWithData: (NSData *)data
{
return [[[self alloc] initWithData: data] autorelease];
}
 
- (idinstancetype)initWithData: (NSData *)data
{
if ((self = [self init]) != nil) {
Line 40 ⟶ 37:
}
 
- (idinstancetype)init
{
if ((self = [super init]) != nil) {
Line 47 ⟶ 44:
}
return self;
}
 
- (void)dealloc
{
[counters release];
[contents release];
[super dealloc];
}
 
Line 64 ⟶ 54:
{
if ( ([contents count] == 0) || ([[contents lastObject] charValue] != aByte) ) {
[contents addObject: [NSNumber numberWithChar: @(aByte])];
[counters addObject: [NSNumber numberWithInt: @(repetitionCount])];
} else {
NSNumberint *a = [[counters lastObject] intValue];
[counters removeLastObject];
[counters addObject: [NSNumber@(a numberWithInt:+ repetitionCount)];
([a intValue] + repetitionCount) ]];
}
}
Line 98 ⟶ 87:
 
for(i=0; i < [contents count]; i++) {
char c = [[contents objectAtIndex: [i] charValue];
int n = [[counters objectAtIndex: [i] intValue];
memset(buf, c, 256);
while ( n > 0 ) {
Line 106 ⟶ 95:
}
}
return [d autorelease];
}
@end</lang>
Line 147 ⟶ 136:
 
for(i=0; i < [counters count]; i++) {
char c = [[contents objectAtIndex: [i] charValue];
int howMany = [[counters objectAtIndex: [i] intValue];
if ( literalCount == 128 ) {
buf[0] = 0;
Line 175 ⟶ 164:
}
}
return [r autorelease];
}
@end</lang>
Line 185 ⟶ 174:
int main()
{
@autoreleasepool {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
codecRLE *enc = [[codecRLE alloc]
initWithData: [NSData dataWithBytes: s
length: strlen(s)] ];
 
NSData *repr = [enc encode];
fwrite([repr bytes], 1, [repr length], stdout);
[enc release];
 
enc = [[codecRLE alloc] init];
[enc decode: repr];
NSData *d = [enc data];
fwrite([d bytes], 1, [d length], stdout);
[enc release];
 
}
[pool release];
return EXIT_SUCCESS;
}</lang>
Anonymous user