@@ -6,11 +6,17 @@ class InMemoryStateSet implements StateSetInterface
6
6
{
7
7
/**
8
8
* Key: State
9
- * Value: Children
10
- * @var array<int, array>
9
+ * Value: array<parent,mappedChar>
10
+ *
11
+ * @var array<int, array<int,int>>
11
12
*/
12
13
private array $ states = [];
13
14
15
+ /**
16
+ * @var array<int, array<int>>
17
+ */
18
+ private array $ children = [];
19
+
14
20
/**
15
21
* Key: State
16
22
* Value: Mapped char
@@ -27,20 +33,25 @@ class InMemoryStateSet implements StateSetInterface
27
33
28
34
public function add (int $ state , int $ parentState , int $ mappedChar ): self
29
35
{
36
+ $ this ->states [$ state ] = [$ parentState , $ mappedChar ];
30
37
$ this ->mappedChars [$ state ] = $ mappedChar ;
31
-
32
- if (! isset ($ this ->states [$ parentState ])) {
33
- $ this ->states [$ parentState ] = [];
34
- }
35
-
36
- $ this ->states [$ parentState ][] = $ state ;
38
+ $ this ->children [$ parentState ][$ state ] = true ;
37
39
38
40
return $ this ;
39
41
}
40
42
43
+ public function all (): array
44
+ {
45
+ return $ this ->states ;
46
+ }
47
+
41
48
public function getChildrenOfState (int $ state ): array
42
49
{
43
- return $ this ->states [$ state ] ?? [];
50
+ if (!isset ($ this ->children [$ state ])) {
51
+ return [];
52
+ }
53
+
54
+ return array_keys ($ this ->children [$ state ]);
44
55
}
45
56
46
57
public function getCharForState (int $ state ): int
0 commit comments