How to resolve the algorithm LZW compression step by step in the Objective-C programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm LZW compression step by step in the Objective-C programming language

Table of Contents

Problem Statement

The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm LZW compression step by step in the Objective-C programming language

Source code in the objective-c programming language

#import 
#import 

@interface LZWCompressor : NSObject
{
  @private
    NSMutableArray *iostream;
    NSMutableDictionary *dict;
    NSUInteger codemark;
}

-(instancetype) init;
-(instancetype) initWithArray: (NSMutableArray *) stream;
-(BOOL) compressData: (NSData *) string;
-(void) setArray: (NSMutableArray *) stream;
-(NSArray *) getArray;
@end

@implementation LZWCompressor : NSObject

-(instancetype) init
{
   self = [super init];
   if ( self )
   {
      iostream = nil;
      codemark = 256;
      dict = [[NSMutableDictionary alloc] initWithCapacity: 512];
   }
   return self;
}

-(instancetype) initWithArray: (NSMutableArray *) stream
{
   self = [self init];
   if ( self )
   {
      [self setArray: stream];
   }
   return self;
}

-(void) setArray: (NSMutableArray *) stream
{
   iostream = stream;
}

-(BOOL) compressData: (NSData *) string;
{
    // prepare dict
    for(NSUInteger i=0; i < 256; i++)
    {
       unsigned char j = i;
       NSData *s = [NSData dataWithBytes: &j length: 1];
       dict[s] = @(i);
    }
    
    NSData *w = [NSData data];
    
    for(NSUInteger i=0; i < [string length]; i++)
    {
       NSMutableData *wc = [NSMutableData dataWithData: w];
       [wc appendData: [string subdataWithRange: NSMakeRange(i, 1)]];
       if ( dict[wc] != nil )
       {
          w = wc;
       } else {
          [iostream addObject: dict[w]];
          dict[wc] = @(codemark);
          codemark++;
          w = [string subdataWithRange: NSMakeRange(i, 1)];
       }
    }
    if ( [w length] != 0 )
    {
       [iostream addObject: dict[w]];
    }
    return YES;
}

-(NSArray *) getArray
{
  return iostream;
}

@end


NSString *text = @"TOBEORNOTTOBEORTOBEORNOT";

int main()
{
  @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]);
       }
    }
  
  }
  return EXIT_SUCCESS;
}


  

You may also check:How to resolve the algorithm Tau function step by step in the Ring programming language
You may also check:How to resolve the algorithm Vector products step by step in the PL/I programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the MapBasic programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm List comprehensions step by step in the Oz programming language