Skip to content

Commit 70e9da4

Browse files
committed
Replace FPGrowth::combinations with a package drupol/phpermutations
1 parent dd7996f commit 70e9da4

File tree

4 files changed

+75
-49
lines changed

4 files changed

+75
-49
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=7.4"
12+
"php": ">=7.4",
13+
"drupol/phpermutations": "^1.4"
1314
},
1415
"autoload": {
1516
"psr-4": {

composer.lock

Lines changed: 66 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FPGrowth.php

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace EnzoMC\PhpFPGrowth;
66

7-
use Generator;
7+
use drupol\phpermutations\Generators\Combinations;
88

99
class FPGrowth
1010
{
@@ -108,7 +108,8 @@ protected function generateAssociationRules(array $patterns): array
108108
$itemset = explode(',', $itemsetStr);
109109
$upper_support = $patterns[$itemsetStr];
110110
for ($i = 1; $i < count($itemset); $i++) {
111-
foreach (self::combinations($itemset, $i) as $antecedent) {
111+
$combinations = new Combinations($itemset, $i);
112+
foreach ($combinations->generator() as $antecedent) {
112113
sort($antecedent);
113114
$antecedentStr = implode(',', $antecedent);
114115
$consequent = array_diff($itemset, $antecedent);
@@ -126,47 +127,4 @@ protected function generateAssociationRules(array $patterns): array
126127
}
127128
return $rules;
128129
}
129-
130-
/**
131-
* @param array $pool
132-
* @param int $r
133-
* @return Generator
134-
* @todo Move to separate class
135-
*/
136-
public static function combinations(array $pool, int $r): Generator
137-
{
138-
$n = count($pool);
139-
140-
if ($r > $n) {
141-
return;
142-
}
143-
144-
$indices = range(0, $r - 1);
145-
yield array_slice($pool, 0, $r);
146-
147-
for (; ;) {
148-
for (; ;) {
149-
for ($i = $r - 1; $i >= 0; $i--) {
150-
if ($indices[$i] != $i + $n - $r) {
151-
break 2;
152-
}
153-
}
154-
155-
return;
156-
}
157-
158-
$indices[$i]++;
159-
160-
for ($j = $i + 1; $j < $r; $j++) {
161-
$indices[$j] = $indices[$j - 1] + 1;
162-
}
163-
164-
$row = [];
165-
foreach ($indices as $i) {
166-
$row[] = $pool[$i];
167-
}
168-
169-
yield $row;
170-
}
171-
}
172130
}

src/FPTree.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace EnzoMC\PhpFPGrowth;
66

7+
use drupol\phpermutations\Generators\Combinations;
8+
79
class FPTree
810
{
911
/** @var array<string,int> */
@@ -210,7 +212,8 @@ protected function generatePatternList(): array
210212
}
211213

212214
for ($i = 1; $i <= count($items); $i++) {
213-
foreach (FPGrowth::combinations($items, $i) as $subset) {
215+
$combinations = new Combinations($items,$i);
216+
foreach ($combinations->generator() as $subset) {
214217
$pattern = $this->root->value !== null ? array_merge($subset, [$this->root->value]) : $subset;
215218
sort($pattern);
216219
$min = PHP_INT_MAX;

0 commit comments

Comments
 (0)