-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartesianProductIterator.php
59 lines (51 loc) · 1.44 KB
/
CartesianProductIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Class CartesianProductIterator implements an Iterator that iterates over two
* Iterators, returning all the values of the second Iterator for every value
* of the first Iterator, aka performing a cartesian product or a CROSS JOIN as
* it would be called in SQL.
*
* Each joined value of the product is returned as an array of the two
* values like this:
*
* <code>
* array(valueFrom1stIterator, valueFrom2ndIterator)
* </code>
*
* The keys of the Iterators are discarded, this class will instead return a
* sequential numeric key.
*
* @author Beat Vontobel
* @since 2011-11-24
* @package MeteoNews\phplib
* @subpackage Iterators
*/
class CartesianProductIterator extends IteratorIterator
implements Iterator, Traversable, OuterIterator {
private $innerIterator;
private $key = 0;
public function __construct(Traversable $iterator, Traversable $innerIterator) {
$this->innerIterator = $innerIterator;
parent::__construct($iterator);
}
public function current() {
return array(parent::current(), $this->innerIterator->current());
}
public function key() {
return $this->key;
}
public function next() {
$this->key++;
$this->innerIterator->next();
if($this->innerIterator->valid())
return;
$this->innerIterator->rewind();
parent::next();
}
public function rewind() {
$this->key = 0;
$this->innerIterator->rewind();
parent::rewind();
}
}
?>