Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I most easily flatten a three-dimensional array in Cocoa?

Say I have an array such as this:

NSArray *threeDimensionalArray = @[
 @[     
     @[ @"Peter", @"Paul", @"Mary" ], @[ @"Joe", @"Jane" ]
  ],
 @[
     @[ @"Alice", @"Bob" ]
  ]
];

and I want it to become:

@[ @"Peter", @"Paul", @"Mary", @"Joe", @"Jane", @"Alice", @"Bob" ]

How can I most easily create this flattened array?

like image 643
Monolo Avatar asked Jun 14 '13 13:06

Monolo


3 Answers

The key-value coding (KVC) collection operator @unionOfArrays flattens one level of arrays, so applying it twice produces the desired result.

Collection operators (other than @count) need a key path to a collection property, and since our objects are already arrays (and hence collections) in themselves, the key path will have to be self.

We therefore need to apply @unionOfArrays twice with the self key path, yielding the following KVC call to flatten a 3D array:

NSArray *flattenedArray = [threeDimensionalArray valueForKeyPath: @"@[email protected]"];
like image 67
Monolo Avatar answered Nov 04 '22 02:11

Monolo


I realize this thread is a little bit old, but I needed a solution where the number of levels deep doesn't really matter. The following methods could be added to a category on NSArray. I've also included the test for these methods:

// This is the method that would be used from an outside class
- (NSArray *)flatten {
    NSArray *array = self;
    while (![array isFlattened]) {
        array = [array flattenOneLevel];
    }
    return [NSArray arrayWithArray:array];
}

- (NSArray *)flattenOneLevel {
    NSMutableArray *array = [NSMutableArray array];
    for (id object in self) {
        [object isKindOfClass:self.class] ? [array addObjectsFromArray:object] : [array addObject:object];
    }
    return array;
}

- (BOOL)isFlattened {
    BOOL flattened = YES;
    for (id object in self) {
        if ([object isKindOfClass:self.class]) {
            flattened = NO;
            break;
        }
    }
    return flattened;
}

Here's the test for these methods to ensure it works properly:

it(@"should flatten an array", ^{
    NSArray *initialArray = @[@[@23, @354, @1, @[@7], @[@[@3]]], @[@[@890], @2, @[@[@6], @8]]];
    NSArray *expectedArray = @[@23, @354, @1, @7, @3, @890, @2, @6, @8];
    expect([initialArray flatten]).equal(expectedArray);
});
like image 2
Christian Di Lorenzo Avatar answered Nov 04 '22 02:11

Christian Di Lorenzo


An alternative answer using recursion, which will take more memory (on the stack), but for those who like recursion, is simpler to read:

- (NSArray *) flatten;
{
    NSMutableArray *flattedArray = [NSMutableArray new];

    for (id item in self) {
        if ([[item class] isSubclassOfClass:[NSArray class]]) {
            [flattedArray addObjectsFromArray:[item flatten]];
        } else {
            [flattedArray addObject:item];
        }
    }

    return flattedArray;
}

And extending the tests:

+ (void) unitTests;
{
    NSArray *flattenedArray;

    NSArray *initialArray1 = @[@[@23, @354, @1, @[@7], @[@[@3]]], @[@[@890], @2, @[@[@6], @8]]];
    NSArray *expectedArray1 = @[@23, @354, @1, @7, @3, @890, @2, @6, @8];
    flattenedArray = [initialArray1 flatten];
    SPASLogDetail(@"flattenedArray: %@", flattenedArray);
    AssertIf(![flattenedArray isEqualToArray:expectedArray1], @"Arrays are not equal");

    NSArray *initialArray2 = @[@[@23, @354, @1, [@[@7] mutableCopy], @[@[@3]]], @[[@[@890] mutableCopy], @2, @[@[@6], @8]]];
    NSArray *expectedArray2 = expectedArray1;
    flattenedArray = [initialArray2 flatten];
    SPASLogDetail(@"flattenedArray: %@", flattenedArray);
    AssertIf(![flattenedArray isEqualToArray:expectedArray2], @"Arrays are not equal");
}
like image 1
Chris Prince Avatar answered Nov 04 '22 01:11

Chris Prince