Created
          November 30, 2010 18:13 
        
      - 
      
 - 
        
Save pheuter/722104 to your computer and use it in GitHub Desktop.  
    Objective C implementation of merge assignment
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // ~ 27 loc | |
| #import <Foundation/Foundation.h> | |
| @interface Main : NSObject | |
| { | |
| NSMutableArray *a1, *a2, *merged; | |
| } | |
| @property(nonatomic, assign) NSMutableArray *a1, *a2, *merged; | |
| - (NSMutableArray *)merge; | |
| @end | |
| @implementation Main | |
| @synthesize a1, a2, merged; | |
| -(NSMutableArray*)merge { | |
| merged = [NSMutableArray arrayWithCapacity:a1.count+a2.count]; | |
| while (a1.count != 0 && a2.count != 0) { | |
| if ([[a1 objectAtIndex:0] intValue] < [[a2 objectAtIndex:0] intValue]) { | |
| [merged addObject:[a1 objectAtIndex:0]]; | |
| [a1 removeObjectAtIndex:0]; | |
| } else { | |
| [merged addObject:[a2 objectAtIndex:0]]; | |
| [a2 removeObjectAtIndex:0]; | |
| } | |
| } | |
| [merged addObjectsFromArray:(a1.count == 0 ? a2 : a1)]; | |
| return merged; | |
| } | |
| @end | |
| int main (int argc, const char * argv[]) { | |
| NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
| Main* m = [[Main alloc] init]; | |
| m.a1 = [NSMutableArray arrayWithObjects: | |
| [NSNumber numberWithInt:1], | |
| [NSNumber numberWithInt:3], | |
| [NSNumber numberWithInt:5], | |
| [NSNumber numberWithInt:7], | |
| [NSNumber numberWithInt:9], | |
| [NSNumber numberWithInt:11],nil]; | |
| m.a2 = [NSMutableArray arrayWithObjects: | |
| [NSNumber numberWithInt:2], | |
| [NSNumber numberWithInt:4], | |
| [NSNumber numberWithInt:6], | |
| [NSNumber numberWithInt:8], | |
| [NSNumber numberWithInt:10], | |
| [NSNumber numberWithInt:12],nil]; | |
| [m merge]; | |
| for(NSNumber* n in m.merged) { | |
| NSLog(@"%d",[n intValue]); | |
| } | |
| [m release]; | |
| [pool drain]; | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment