Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

Commit 0870ae9

Browse files
committed
change CartesianProduct to be associative
1 parent 9f7f77b commit 0870ae9

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

Combinatorics/Combination/CartesianProduct.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* New BSD License
1010
*
1111
* Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
12+
* Copyright © 2007-2015, Karoly Negyesi. All rights reserved.
1213
*
1314
* Redistribution and use in source and binary forms, with or without
1415
* modification, are permitted provided that the following conditions are met:
@@ -70,6 +71,13 @@ class CartesianProduct implements Iterator {
7071
*/
7172
protected $_max = 0;
7273

74+
/**
75+
* array_keys() of the input array.
76+
*
77+
* @var array
78+
*/
79+
protected $_keys = [];
80+
7381
/**
7482
* Key.
7583
*
@@ -97,13 +105,13 @@ class CartesianProduct implements Iterator {
97105
* Constructor.
98106
*
99107
* @access public
100-
* @param \Traversable $set Set.
101-
* @param … … …
108+
* @param \Traversable $sets Sets.
102109
* @return void
103110
*/
104-
public function __construct ( $set ) {
111+
public function __construct ( $sets ) {
105112

106-
foreach(func_get_args() as $s) {
113+
$this->_keys = array_keys($sets);
114+
foreach($sets as $s) {
107115

108116
if(is_array($s))
109117
$s = new Iterator\Map($s);
@@ -140,8 +148,8 @@ protected function _current ( ) {
140148

141149
$this->_current = [];
142150

143-
foreach($this->_sets as $set)
144-
$this->_current[] = $set->current();
151+
foreach($this->_sets as $i => $set)
152+
$this->_current[$this->_keys[$i]] = $set->current();
145153

146154
return;
147155
}

Test/Unit/Combinatorics/Combination/CartesianProduct.php

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function case_empty ( ) {
6464
public function case_X ( ) {
6565

6666
$this
67-
->given($iterator = new CUT([1, 2, 3]))
67+
->given($iterator = new CUT([[1], [2], [3]]))
6868
->when($result = iterator_to_array($iterator))
6969
->then
7070
->array($result)
@@ -78,62 +78,59 @@ public function case_X ( ) {
7878
public function case_X_Y ( ) {
7979

8080
$this
81-
->given($iterator = new CUT([1, 2, 3], [4, 5, 6]))
81+
->given($iterator = new CUT(['X' => [1, 2, 3], 'Y' => [4, 5, 6]]))
8282
->when($result = iterator_to_array($iterator))
8383
->then
8484
->array($result)
8585
->isEqualTo([
86-
[1, 4],
87-
[2, 4],
88-
[3, 4],
89-
90-
[1, 5],
91-
[2, 5],
92-
[3, 5],
93-
94-
[1, 6],
95-
[2, 6],
96-
[3, 6]
86+
['X' => 1, 'Y' => 4],
87+
['X' => 2, 'Y' => 4],
88+
['X' => 3, 'Y' => 4],
89+
['X' => 1, 'Y' => 5],
90+
['X' => 2, 'Y' => 5],
91+
['X' => 3, 'Y' => 5],
92+
['X' => 1, 'Y' => 6],
93+
['X' => 2, 'Y' => 6],
94+
['X' => 3, 'Y' => 6]
9795
]);
9896
}
9997

100-
public function case_X_Y_Z ( ) {
10198

99+
public function case_X_Y_Z ( ) {
102100
$this
103-
->given($iterator = new CUT([1, 2, 3], [4, 5, 6], [7, 8, 9]))
101+
->given($iterator = new CUT(['X' => [1, 2, 3], 'Y' => [4, 5, 6], 'Z' => [7, 8, 9]]))
104102
->when($result = iterator_to_array($iterator))
105103
->then
106104
->array($result)
107105
->isEqualTo([
108-
[1, 4, 7],
109-
[2, 4, 7],
110-
[3, 4, 7],
111-
[1, 5, 7],
112-
[2, 5, 7],
113-
[3, 5, 7],
114-
[1, 6, 7],
115-
[2, 6, 7],
116-
[3, 6, 7],
117-
118-
[1, 4, 8],
119-
[2, 4, 8],
120-
[3, 4, 8],
121-
[1, 5, 8],
122-
[2, 5, 8],
123-
[3, 5, 8],
124-
[1, 6, 8],
125-
[2, 6, 8],
126-
[3, 6, 8],
127-
128-
[1, 4, 9],
129-
[2, 4, 9],
130-
[3, 4, 9],
131-
[1, 5, 9],
132-
[2, 5, 9],
133-
[3, 5, 9],
134-
[1, 6, 9],
135-
[2, 6, 9],
136-
[3, 6, 9]
106+
['X' => 1, 'Y' => 4, 'Z' => 7],
107+
['X' => 2, 'Y' => 4, 'Z' => 7],
108+
['X' => 3, 'Y' => 4, 'Z' => 7],
109+
['X' => 1, 'Y' => 5, 'Z' => 7],
110+
['X' => 2, 'Y' => 5, 'Z' => 7],
111+
['X' => 3, 'Y' => 5, 'Z' => 7],
112+
['X' => 1, 'Y' => 6, 'Z' => 7],
113+
['X' => 2, 'Y' => 6, 'Z' => 7],
114+
['X' => 3, 'Y' => 6, 'Z' => 7],
115+
['X' => 1, 'Y' => 4, 'Z' => 8],
116+
['X' => 2, 'Y' => 4, 'Z' => 8],
117+
['X' => 3, 'Y' => 4, 'Z' => 8],
118+
['X' => 1, 'Y' => 5, 'Z' => 8],
119+
['X' => 2, 'Y' => 5, 'Z' => 8],
120+
['X' => 3, 'Y' => 5, 'Z' => 8],
121+
['X' => 1, 'Y' => 6, 'Z' => 8],
122+
['X' => 2, 'Y' => 6, 'Z' => 8],
123+
['X' => 3, 'Y' => 6, 'Z' => 8],
124+
['X' => 1, 'Y' => 4, 'Z' => 9],
125+
['X' => 2, 'Y' => 4, 'Z' => 9],
126+
['X' => 3, 'Y' => 4, 'Z' => 9],
127+
['X' => 1, 'Y' => 5, 'Z' => 9],
128+
['X' => 2, 'Y' => 5, 'Z' => 9],
129+
['X' => 3, 'Y' => 5, 'Z' => 9],
130+
['X' => 1, 'Y' => 6, 'Z' => 9],
131+
['X' => 2, 'Y' => 6, 'Z' => 9],
132+
['X' => 3, 'Y' => 6, 'Z' => 9]
137133
]);
138134
}
135+
139136
}

0 commit comments

Comments
 (0)