Passed
Pull Request — main (#3)
by Pol
13:23
created

RandomIterableAggregate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A randomize() 0 19 4
A __construct() 0 4 1
A getIterator() 0 5 1
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\iterators;
11
12
use ArrayIterator;
13
use Generator;
14
use Iterator;
15
use IteratorAggregate;
16
use Traversable;
17
18
use const PHP_INT_MAX;
19
use const PHP_INT_MIN;
20
21
/**
22
 * @template TKey
23
 * @template T
24
 *
25
 * @implements IteratorAggregate<TKey, T>
26
 */
27
final class RandomIterableAggregate implements IteratorAggregate
28
{
29
    /**
30
     * @var IteratorAggregate<int, array{0: TKey, 1: T}>
31
     */
32
    private IteratorAggregate $iteratorAggregate;
33
34
    private int $seed;
35
36
    /**
37
     * @param iterable<TKey, T> $iterable
38
     */
39
    public function __construct(iterable $iterable, int $seed = 0)
40
    {
41
        $this->iteratorAggregate = new PackIterableAggregate($iterable);
42
        $this->seed = $seed;
43
    }
44
45
    /**
46
     * @return Traversable<TKey, T>
47
     */
48
    public function getIterator(): Traversable
49
    {
50
        mt_srand($this->seed);
51
52
        yield from $this->randomize($this->iteratorAggregate, $this->seed);
53
    }
54
55
    /**
56
     * @param Traversable<int, array{0: TKey, 1: T}> $traversable
57
     *
58
     * @return Generator<TKey, T>
59
     */
60
    private function randomize(Traversable $traversable, int $seed): Generator
61
    {
62
        $isQueueEmpty = true;
63
        /** @var ArrayIterator<int, array{0: TKey, 1: T}> $queue */
64
        $queue = new ArrayIterator();
65
66
        foreach (new UnpackIterableAggregate($traversable) as $key => $value) {
67
            if (mt_rand(0, $seed) === 0) {
68
                yield $key => $value;
69
70
                continue;
71
            }
72
73
            $queue->append([$key, $value]);
74
            $isQueueEmpty = false;
75
        }
76
77
        if (false === $isQueueEmpty) {
78
            yield from $this->randomize($queue, $seed);
79
        }
80
    }
81
}
82