1<?php
2
3declare(strict_types=1);
4
5namespace Antlr\Antlr4\Runtime\Utils;
6
7final class DoubleKeyMap
8{
9    /**
10     * Map<primaryKey, Map<secondaryKey, value>>
11     *
12     * @var Map
13     */
14    private $data;
15
16    public function __construct()
17    {
18        $this->data = new Map();
19    }
20
21    /**
22     * @param mixed $primaryKey
23     * @param mixed $secondaryKey
24     * @param mixed $value
25     */
26    public function set($primaryKey, $secondaryKey, $value) : void
27    {
28        $secondaryData = $this->data->get($primaryKey);
29
30        if ($secondaryData === null) {
31            $secondaryData = new Map();
32
33            $this->data->put($primaryKey, $secondaryData);
34        }
35
36        $secondaryData->put($secondaryKey, $value);
37    }
38
39    /**
40     * @param mixed $primaryKey
41     * @param mixed $secondaryKey
42     *
43     * @return mixed
44     */
45    public function getByTwoKeys($primaryKey, $secondaryKey)
46    {
47        $data2 = $this->data->get($primaryKey);
48
49        if ($data2 === null) {
50            return null;
51        }
52
53        return $data2->get($secondaryKey);
54    }
55
56    /**
57     * @param mixed $primaryKey
58     */
59    public function getByOneKey($primaryKey) : Map
60    {
61        return $this->data->get($primaryKey);
62    }
63
64    /**
65     * @param mixed $primaryKey
66     *
67     * @return array<mixed>|null
68     */
69    public function values($primaryKey) : ?array
70    {
71        $secondaryData = $this->data->get($primaryKey);
72
73        if ($secondaryData === null) {
74            return null;
75        }
76
77        return $secondaryData->getValues();
78    }
79
80    /**
81     * @return array<mixed>
82     */
83    public function primaryKeys() : array
84    {
85        return $this->data->getKeys();
86    }
87
88    /**
89     * @param mixed $primaryKey
90     *
91     * @return array<mixed>|null
92     */
93    public function secondaryKeys($primaryKey) : ?array
94    {
95        $secondaryData = $this->data->get($primaryKey);
96
97        if ($secondaryData === null) {
98            return null;
99        }
100
101        return $secondaryData->getKeys();
102    }
103}
104