|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace leetcode; |
|
6
|
|
|
|
|
7
|
|
|
use leetcode\util\LRUNode; |
|
8
|
|
|
|
|
9
|
|
|
class LRUCache |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array */ |
|
12
|
|
|
private array $map; |
|
13
|
|
|
|
|
14
|
|
|
/** @var \leetcode\util\LRUNode */ |
|
15
|
|
|
private LRUNode $head; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \leetcode\util\LRUNode */ |
|
18
|
|
|
private LRUNode $tail; |
|
19
|
|
|
|
|
20
|
|
|
/** @var int */ |
|
21
|
|
|
protected int $capacity; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(int $capacity) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->head = new LRUNode(0, 0); |
|
26
|
|
|
$this->tail = new LRUNode(0, 0); |
|
27
|
|
|
$this->capacity = $capacity; |
|
28
|
|
|
|
|
29
|
|
|
$this->head->setNext($this->tail); |
|
30
|
|
|
$this->tail->setPrev($this->head); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function get(int $key) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!isset($this->map[$key])) { |
|
36
|
|
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** @var \leetcode\util\LRUNode $node */ |
|
40
|
|
|
$node = $this->map[$key]; |
|
41
|
|
|
|
|
42
|
|
|
if (count($this->map) === 1) { |
|
43
|
|
|
return $node->getVal(); |
|
44
|
|
|
} |
|
45
|
|
|
$this->detach($node); |
|
46
|
|
|
$this->attach($this->head, $node); |
|
47
|
|
|
|
|
48
|
|
|
return $node->getVal(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function put(int $key, $val): bool |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->capacity <= 0) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (isset($this->map[$key]) && !empty($this->map[$key])) { |
|
58
|
|
|
/** @var \leetcode\util\LRUNode $node */ |
|
59
|
|
|
$node = $this->map[$key]; |
|
60
|
|
|
$this->detach($node); |
|
61
|
|
|
$this->attach($this->head, $node); |
|
62
|
|
|
$node->setVal($val); |
|
63
|
|
|
} else { |
|
64
|
|
|
$node = new LRUNode($key, $val); |
|
65
|
|
|
$this->map[$key] = $node; |
|
66
|
|
|
$this->attach($this->head, $node); |
|
67
|
|
|
|
|
68
|
|
|
if (count($this->map) > $this->capacity) { |
|
69
|
|
|
$nodeToRemove = $this->tail->getPrev(); |
|
70
|
|
|
$this->detach($nodeToRemove); |
|
71
|
|
|
unset($this->map[$nodeToRemove->getKey()]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function remove(int $key): bool |
|
79
|
|
|
{ |
|
80
|
|
|
if (!isset($this->map[$key])) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** @var \leetcode\util\LRUNode $nodeToRemove */ |
|
85
|
|
|
$nodeToRemove = $this->map[$key]; |
|
86
|
|
|
$this->detach($nodeToRemove); |
|
87
|
|
|
unset($this->map[$nodeToRemove->getKey()]); |
|
88
|
|
|
|
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function all(): array |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->map; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function attach(LRUNode $head, LRUNode $node): void |
|
98
|
|
|
{ |
|
99
|
|
|
$node->setPrev($head); |
|
100
|
|
|
$node->setNext($head->getNext()); |
|
101
|
|
|
$node->getPrev()->setNext($node); |
|
102
|
|
|
$node->getNext()->setPrev($node); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
private function detach(LRUNode $node): void |
|
106
|
|
|
{ |
|
107
|
|
|
$node->getPrev()->setNext($node->getNext()); |
|
108
|
|
|
$node->getNext()->setPrev($node->getPrev()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|