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

Commit d18aad9

Browse files
committed
Add associative array cartesian products
1 parent b52764f commit d18aad9

File tree

3 files changed

+206
-1
lines changed

3 files changed

+206
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
4+
/**
5+
* Hoa
6+
*
7+
*
8+
* @license
9+
*
10+
* New BSD License
11+
*
12+
* Copyright © 2014, Karoly Negyesi. All rights reserved.
13+
*
14+
* Redistribution and use in source and binary forms, with or without
15+
* modification, are permitted provided that the following conditions are met:
16+
* * Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* * Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
* * Neither the name of the Hoa nor the names of its contributors may be
22+
* used to endorse or promote products derived from this software without
23+
* specific prior written permission.
24+
*
25+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
29+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*/
37+
38+
namespace Hoa\Math\Combinatorics\Combination;
39+
40+
/**
41+
* Class \Hoa\Math\Combinatorics\Combination\CartesianProduct.
42+
*
43+
* Cartesian n-ary product iterator:
44+
* [
45+
* X => [1, 2],
46+
* Y => [a, b],
47+
* ]
48+
* X × Y = [ [X => 1, Y => a], [X => 1, Y => b], [X => 2, Y => a],
49+
* [X => 2, y => b] ]
50+
*
51+
* @author Karoly Negyesi <[email protected]>
52+
* @copyright Copyright © 2014 Karoly Negyesi.
53+
* @license New BSD License
54+
*/
55+
56+
class CartesianAssociativeProduct extends CartesianProduct {
57+
58+
/**
59+
* array_keys() of the input array.
60+
*
61+
* @var array
62+
*/
63+
protected $_keys = [];
64+
65+
/**
66+
* Constructor.
67+
*
68+
* @access public
69+
* @param array $array Associative array of arrays.
70+
* @return void
71+
*/
72+
public function __construct ( $array ) {
73+
74+
$this->_keys = array_keys($array);
75+
$this->init($array);
76+
}
77+
78+
/**
79+
* Prepare the current value.
80+
*
81+
* @access protected
82+
* @return void
83+
*/
84+
protected function _current ( ) {
85+
86+
$this->_current = [];
87+
88+
foreach($this->_sets as $i => $set)
89+
$this->_current[$this->_keys[$i]] = $set->current();
90+
91+
return;
92+
}
93+
94+
}

Combinatorics/Combination/CartesianProduct.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ class CartesianProduct implements Iterator {
103103
*/
104104
public function __construct ( $set ) {
105105

106-
foreach(func_get_args() as $s) {
106+
$this->init(func_get_args());
107+
}
108+
109+
public function init ( $input ) {
110+
111+
foreach($input as $s) {
107112

108113
if(is_array($s))
109114
$s = new Iterator\Map($s);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Hoa\Math\Test\Unit\Sampler\Combinatorics\Combination;
4+
5+
use Hoa\Math\Combinatorics\Combination\CartesianAssociativeProduct as CAT;
6+
use Hoa\Test;
7+
8+
/**
9+
* Class \Hoa\Math\Test\Unit\Sampler\Combinatorics\Combination\CartesianAssociativeProduct.
10+
*
11+
* Test suite of the cartesian product.
12+
*
13+
* @author Karoly Negyesi <[email protected]>
14+
* @copyright Copyright © 2014 Karoly Negyesi.
15+
* @license New BSD License
16+
*/
17+
18+
class CartesianAssociativeProduct extends Test\Unit\Suite {
19+
20+
public function case_empty ( ) {
21+
22+
$this
23+
->given($iterator = new CAT([]))
24+
->when($result = iterator_to_array($iterator))
25+
->then
26+
->array($result)
27+
->isEqualTo([[null]]);
28+
}
29+
30+
public function case_X ( ) {
31+
32+
$this
33+
->given($iterator = new CAT([[1], [2], [3]]))
34+
->when($result = iterator_to_array($iterator))
35+
->then
36+
->array($result)
37+
->isEqualTo([
38+
[1],
39+
[2],
40+
[3]
41+
]);
42+
}
43+
44+
public function case_X_Y ( ) {
45+
46+
$this
47+
->given($iterator = new CAT(['X' => [1, 2, 3], 'Y' => [4, 5, 6]]))
48+
->when($result = iterator_to_array($iterator))
49+
->then
50+
->array($result)
51+
->isEqualTo([
52+
['X' => 1, 'Y' => 4],
53+
['X' => 2, 'Y' => 4],
54+
['X' => 3, 'Y' => 4],
55+
56+
['X' => 1, 'Y' => 5],
57+
['X' => 2, 'Y' => 5],
58+
['X' => 3, 'Y' => 5],
59+
60+
['X' => 1, 'Y' => 6],
61+
['X' => 2, 'Y' => 6],
62+
['X' => 3, 'Y' => 6]
63+
]);
64+
}
65+
66+
public function case_X_Y_Z ( ) {
67+
68+
$this
69+
->given($iterator = new CAT(['X' => [1, 2, 3], 'Y' => [4, 5, 6], 'Z' => [7, 8, 9]]))
70+
->when($result = iterator_to_array($iterator))
71+
->then
72+
->array($result)
73+
->isEqualTo([
74+
['X' => 1, 'Y' => 4, 'Z' => 7],
75+
['X' => 2, 'Y' => 4, 'Z' => 7],
76+
['X' => 3, 'Y' => 4, 'Z' => 7],
77+
['X' => 1, 'Y' => 5, 'Z' => 7],
78+
['X' => 2, 'Y' => 5, 'Z' => 7],
79+
['X' => 3, 'Y' => 5, 'Z' => 7],
80+
['X' => 1, 'Y' => 6, 'Z' => 7],
81+
['X' => 2, 'Y' => 6, 'Z' => 7],
82+
['X' => 3, 'Y' => 6, 'Z' => 7],
83+
84+
['X' => 1, 'Y' => 4, 'Z' => 8],
85+
['X' => 2, 'Y' => 4, 'Z' => 8],
86+
['X' => 3, 'Y' => 4, 'Z' => 8],
87+
['X' => 1, 'Y' => 5, 'Z' => 8],
88+
['X' => 2, 'Y' => 5, 'Z' => 8],
89+
['X' => 3, 'Y' => 5, 'Z' => 8],
90+
['X' => 1, 'Y' => 6, 'Z' => 8],
91+
['X' => 2, 'Y' => 6, 'Z' => 8],
92+
['X' => 3, 'Y' => 6, 'Z' => 8],
93+
94+
['X' => 1, 'Y' => 4, 'Z' => 9],
95+
['X' => 2, 'Y' => 4, 'Z' => 9],
96+
['X' => 3, 'Y' => 4, 'Z' => 9],
97+
['X' => 1, 'Y' => 5, 'Z' => 9],
98+
['X' => 2, 'Y' => 5, 'Z' => 9],
99+
['X' => 3, 'Y' => 5, 'Z' => 9],
100+
['X' => 1, 'Y' => 6, 'Z' => 9],
101+
['X' => 2, 'Y' => 6, 'Z' => 9],
102+
['X' => 3, 'Y' => 6, 'Z' => 9]
103+
]);
104+
}
105+
106+
}

0 commit comments

Comments
 (0)