ååâ
terazzo.hatenadiary.org
ã¾ãããããã¯ã»ã¼è足ã ãã©ãååã®ãã®ãIteratorã«æéã®ãã®ãæ··ãã£ã¦ãã¦ã大ä¸å¤«ãªããã«ä¿®æ£ããã
初回ã®æã®å³ãæãåºããªãããããã®çæ¹ãæéã®å ´åã§èãã¦ã¿ãã
âââââ
å¢ããaã«å¯¾ãã¦éå»ã®bã®å ¨å¤ãæãåããã¦ããã°ããã§ããã
ããã°ã©ã ãè¦ç´ãã¨ãã¾ãIteratorãåæã«åãã¦ããã¨ãããä¿®æ£ããã©ããä¸ã¤ã§ã次ã®å¤ãããå ´åã«ã«ã¼ããç¶ããããã«ãããã®ã¨ãã次ã®å¤ããªãå ´åã¯åºåæ¸ã¿ã®å¤ãåºãç¶ãããããã«ç´ãã°è¯ããã
function gradual_iterator(iterable $iter): \Iterator { $cached = []; foreach ($iter as $last) { yield [$cached, [$last], true]; $cached[] = $last; } // å¤ãå°½ãã¦ãåºãç¶ãã(validãã©ããã¯æ»ãå¤ã§æ¸¡ã) while (true) { yield [$cached, [], false]; } } function gradual_iterator_zip(iterable ...$iters): \Iterator { $gradual_iterators = array_map(gradual_iterator(...), $iters); while (true) { $any_valid = false; $values = []; foreach ($gradual_iterators as $iter) { [$cached, $last, $valid] = $iter->current(); $iter->next(); $any_valid = $any_valid || $valid; $values[] = [$cached, $last]; } if (!$any_valid) { return; } yield $values; } }
ã¤ãã§ã«é
åã渡ããããã«å¼æ°ãiterableã«ãã¦ããã
æ°è¦ã®å¤ãããããvalid()ãããªãã¦å¤ã§å¤å®ããã®ã¯Iteratorã¨ãã¦ã¯è¡åãæªãæ°ã¯ãã¾ãããå¤å®ã¨éå»ã®å¤ãä¿æããã®ãåæã«ããããã®ã§ãããªã£ã¦ãã
function iterator_cartesian_product(iterable ...$iterators): \Iterator
{
foreach (gradual_iterator_zip(...$iterators) as $dimensions) {
$configurations = array_cartesian_product(...$dimensions);
array_shift($configurations); // åºåæ¸ã¿ã®å¤ãã¹ããã
foreach ($configurations as $configuration) {
yield from array_cartesian_product(...$configuration);
}
}
}
ãã¹ã
public function test_iterator_product2_finite()
{
$iterator = iterator_cartesian_product(sequence(0), sequence(0, 2));
$result = iterator_take($iterator, 15);
$expected = [[0, 0], [1, 0], [0, 1], [1, 1], [2, 0], [2, 1], [0, 2], [1, 2], [2, 2], [3, 0], [3, 1], [3, 2], [4, 0], [4, 1], [4, 2]];
$this->assertEquals($expected, $result);
}
æè§Generatorã使ã£ãé 延è©ä¾¡ãªã®ã«array_cartesian_productãã°ãã§ããé åãä½ãã®ã§ã¡ã¢ãªã足ããªããªãã®ãã¤ãã¤ãã§ããã