我怎样才能完成笛卡尔积函数的 Objective-C 实现?

发布于 2024-12-18 05:32:56 字数 1608 浏览 3 评论 0原文

作为我的问题的后续此处,我正在尝试在 Objective-C 中实现以下 PHP 函数,它将生成一个笛卡尔积:

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;
}

这是我到目前为止所拥有的:

-(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;

}

当尝试编写与 PHP 等效的代码时,我迷失了方向nextcurrentreset 函数,因为我不知道如何引用数组的内部指针。

如何实现最后一段代码并获得等效的功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箜明 2024-12-25 05:32:57
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;
}
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;
}
娇妻 2024-12-25 05:32:57

NSArray NSMutableArray 没有下一个当前重置功能。
我认为你可以写一个类来实现这样的功能,

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

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

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文