1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Barryvanveen\CCA; |
6
|
|
|
|
7
|
|
|
use Barryvanveen\CCA\Exceptions\LoopNotFoundException; |
8
|
|
|
|
9
|
|
|
class Runner |
10
|
|
|
{ |
11
|
|
|
/** @var Config */ |
12
|
|
|
protected $config; |
13
|
|
|
|
14
|
|
|
/** @var CCA */ |
15
|
|
|
protected $cca; |
16
|
|
|
|
17
|
3 |
|
public function __construct(Config $config, CCA $cca) |
18
|
|
|
{ |
19
|
3 |
|
$this->config = $config; |
20
|
|
|
|
21
|
3 |
|
$this->cca = $cca; |
22
|
3 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Run the CCA and return the $numIterations-th state. |
26
|
|
|
* |
27
|
|
|
* @param int $numIterations |
28
|
|
|
* |
29
|
|
|
* @return State |
30
|
|
|
*/ |
31
|
3 |
|
public function getLastState(int $numIterations): State |
32
|
|
|
{ |
33
|
|
|
do { |
34
|
3 |
|
$state = $this->cca->getState(); |
35
|
|
|
|
36
|
3 |
|
$iteration = $this->cca->cycle(); |
37
|
3 |
|
} while ($iteration < $numIterations); |
38
|
|
|
|
39
|
3 |
|
return $state; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Run the CCA and return an array with first $numIterations states. |
44
|
|
|
* |
45
|
|
|
* @param int $numIterations |
46
|
|
|
* |
47
|
|
|
* @return State[] |
48
|
|
|
*/ |
49
|
3 |
|
public function getFirstStates(int $numIterations): array |
50
|
|
|
{ |
51
|
3 |
|
$states = []; |
52
|
|
|
|
53
|
|
|
do { |
54
|
3 |
|
$states[] = $this->cca->getState(); |
55
|
|
|
|
56
|
3 |
|
$iteration = $this->cca->cycle(); |
57
|
3 |
|
} while ($iteration < $numIterations); |
58
|
|
|
|
59
|
3 |
|
return $states; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Run the CCA and return the first looping states it encounters. If no loop is found within $maxIterations, |
64
|
|
|
* a LoopNotFoundException exception will be thrown. |
65
|
|
|
* |
66
|
|
|
* @param int $maxIterations |
67
|
|
|
* |
68
|
|
|
* @throws LoopNotFoundException |
69
|
|
|
* |
70
|
|
|
* @return State[] |
71
|
|
|
*/ |
72
|
9 |
|
public function getFirstLoop(int $maxIterations) |
73
|
|
|
{ |
74
|
9 |
|
$states = []; |
75
|
9 |
|
$hashes = []; |
76
|
|
|
|
77
|
|
|
do { |
78
|
9 |
|
$state = $this->cca->getState(); |
79
|
9 |
|
$hash = $state->toHash(); |
80
|
|
|
|
81
|
9 |
|
$firstOccurence = array_search($hash, $hashes); |
82
|
9 |
|
if ($this->loopIsFound($firstOccurence)) { |
83
|
6 |
|
if ($this->loopIsTooShort($states, $firstOccurence)) { |
84
|
3 |
|
throw new LoopNotFoundException(); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
return array_slice($states, (int) $firstOccurence); |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
$states[] = $state; |
91
|
9 |
|
$hashes[] = $hash; |
92
|
|
|
|
93
|
9 |
|
$iteration = $this->cca->cycle(); |
94
|
9 |
|
} while ($iteration < $maxIterations); |
95
|
|
|
|
96
|
3 |
|
throw new LoopNotFoundException(); |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
protected function loopIsFound($firstOccurence) |
100
|
|
|
{ |
101
|
3 |
|
return $firstOccurence !== false; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
protected function loopIsTooShort($states, $firstOccurence) |
105
|
|
|
{ |
106
|
3 |
|
return ((count($states) - (int) $firstOccurence) === 1); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|