How can I complete this Objective-C implementation of a cartesian product function?
Asked Answered
A

2

8

As a follow up to my question here, I am trying to implement the following PHP function in Objective-C, which will generate a cartesian product:

function array_cartesian_product($arrays)
{
    $result = array();
    $arrays = array_values($arrays);
    $sizeIn = sizeof($arrays);
    $size = $sizeIn > 0 ? 1 : 0;
    foreach ($arrays as $array)
        $size = $size * sizeof($array);
    for ($i = 0; $i < $size; $i ++)
    {
        $result[$i] = array();
        for ($j = 0; $j < $sizeIn; $j ++)
            array_push($result[$i], current($arrays[$j]));
        for ($j = ($sizeIn -1); $j >= 0; $j --)
        {
            if (next($arrays[$j]))
                break;
            elseif (isset ($arrays[$j]))
                reset($arrays[$j]);
        }
    }
    return $result;
}

Here is what I have so far:

-(NSArray *) array_cartesian_product:(NSArray *)arrays {

    NSMutableArray *result = [[NSMutableArray alloc] init];

    int sizeIn = [arrays count];
    int size = (sizeIn > 0) ? 1 : 0;

    for(id array in arrays)
        size *= [array count];


    for(int i = 0; i < size; i++) {

        for (int j = 0; j < sizeIn; j++) {
            [result insertObject:[arrays objectAtIndex:j] atIndex:i];
        }

        for (int j = (sizeIn - 1); j >= 0; j--) {

            // ?????

        }


    }

    return result;

}

I'm getting lost when trying to code the equivalent of PHP's next, current and reset functions, as I dont know how to reference the internal pointer to the array.

How can I implement the last block of code and get an equivalent function?

Anamorphism answered 25/11, 2011 at 5:16 Comment(2)
did you consider using the enumerator object of the array?Crabb
Too localized? I'm sure someone could use this down the road, and adding a cartesian product function in Objective-C to the knowledgebase wouldn't hurt...Anamorphism
C
11
NSArray *cartesianProductOfArrays(NSArray *arrays)
{
    int arraysCount = arrays.count;
    unsigned long resultSize = 1;
    for (NSArray *array in arrays)
        resultSize *= array.count;
    NSMutableArray *product = [NSMutableArray arrayWithCapacity:resultSize];
    for (unsigned long i = 0; i < resultSize; ++i) {
        NSMutableArray *cross = [NSMutableArray arrayWithCapacity:arraysCount];
        [product addObject:cross];
        unsigned long n = i;
        for (NSArray *array in arrays) {
            [cross addObject:[array objectAtIndex:n % array.count]];
            n /= array.count;
        }
    }
    return product;
}
Cultivar answered 25/11, 2011 at 6:4 Comment(1)
Couple of little syntax issues but I used this and it works perfectly. Much thanks, even over a year later.Ascend
G
-3

NSArray NSMutableArray does not has next current reset functions. I think you can write a class to implement such function

@interface myArray {
    NSMutableArray* array;//the real array
    int index;//hole the index
}

-(id)current;
-(id)next;
-(id)reset;
@end

the 3 function will modify the index,

Gwyngwyneth answered 25/11, 2011 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.