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:
作为我的问题的后续,我正在尝试在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;
}
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.
当我试图编写等价于PHP的下一个、当前和重置函数时,我迷失了方向,因为我不知道如何引用数组的内部指针。
How can I implement the last block of code and get an equivalent function?
如何实现最后一个代码块并获得一个等价的函数?
2 个解决方案
#1
8
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;
}
#2
-2
NSArray NSMutableArray does not has next current reset functions. I think you can write a class to implement such function
NSArray NSMutableArray没有下一个当前重置函数。我认为您可以编写一个类来实现这样的函数
@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,
3函数会修改索引,
#1
8
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;
}
#2
-2
NSArray NSMutableArray does not has next current reset functions. I think you can write a class to implement such function
NSArray NSMutableArray没有下一个当前重置函数。我认为您可以编写一个类来实现这样的函数
@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,
3函数会修改索引,