-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlru-cache.rs
204 lines (169 loc) · 5.2 KB
/
lru-cache.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#![allow(dead_code, unused, unused_variables, non_snake_case)]
use std::{
cell::RefCell,
rc::{Rc, Weak},
};
fn main() {
// ["LRUCache","put","put","put","put","get","get","get","get","put","get","get","get","get","get"]
// [[3], [1,1],[2,2],[3,3],[4,4],[4], [3], [2], [1], [5,5], [1], [2], [3], [4], [5]]
// [null, null, null, null, null, 4, 3, 2, -1, null, -1, 2, 3, -1, 5]
let mut lru = LRUCache::new(3);
lru.put(1, 1);
lru.put(2, 2);
lru.put(3, 3);
lru.put(4, 4);
assert_eq!(lru.get(4), 4);
assert_eq!(lru.get(3), 3);
assert_eq!(lru.get(2), 2);
assert_eq!(lru.get(1), -1);
lru.put(5, 5);
assert_eq!(lru.get(1), -1);
assert_eq!(lru.get(2), 2);
assert_eq!(lru.get(3), 3);
assert_eq!(lru.get(4), -1);
assert_eq!(lru.get(5), 5);
}
struct Solution;
#[derive(Clone)]
struct KeyValuePair {
key: i32,
value: i32,
}
struct LRUCache {
map: std::collections::HashMap<i32, Rc<RefCell<Node>>>,
list: List,
capacity: usize,
}
struct Node {
value: KeyValuePair,
next: Option<Rc<RefCell<Node>>>,
prev: Option<Rc<RefCell<Node>>>,
}
struct List {
length: usize,
head: Option<Rc<RefCell<Node>>>,
tail: Option<Rc<RefCell<Node>>>,
}
impl List {
fn new() -> Self {
Self {
length: 0,
head: None,
tail: None,
}
}
// 从链表中移除当前节点
// 当前节点的prev节点的next节点指向当前节点的next节点
// 当前节点的next节点的prev节点指向当前节点的prev节点
fn remove(&mut self, node: Rc<RefCell<Node>>) {
if self.length == 0 {
return;
}
self.length -= 1;
let prev = node.borrow_mut().prev.take();
let next = node.borrow_mut().next.take();
// 说明移除的是头节点, head要指向next节点
if prev.is_none() {
self.head = next.clone();
}
// 说明移除的尾节点,tail要指向prev节点
if next.is_none() {
self.tail = prev.clone();
}
prev.clone().map(|x| x.borrow_mut().next = next.clone());
next.clone().map(|x| x.borrow_mut().prev = prev.clone());
}
/// 向列表头部插入数据
/// head指向新的数据
/// 新数据的next指向原列表头部
fn push_front(&mut self, value: KeyValuePair) -> Rc<RefCell<Node>> {
self.length += 1;
let node = Rc::new(RefCell::new(Node {
value,
next: None,
prev: None,
}));
// 获取当前头头节点
let head = self.head.take();
// 新的头节点的下一个元素指向旧的头节点
node.borrow_mut().next = head.clone();
// 旧的头节点的prev指向新的头节点
head.clone()
.map(|x| x.borrow_mut().prev = Some(node.clone()));
// 如果长度为1,尾节点和头节点指向头一个节点
if self.length == 1 {
self.tail = Some(node.clone());
}
self.head = Some(node.clone());
node
}
/// 移除尾部元素
/// 获取到tail的值,
/// 将tail.prev设置为tail
/// tail.prev.next设置为None
/// 返回最后节点的key
fn pop(&mut self) -> Option<i32> {
match self.tail.take() {
None => None,
Some(node) => {
self.length -= 1;
let prev = node.borrow().prev.clone();
prev.clone().map(|x| x.borrow_mut().next = None);
self.tail = prev.clone(); // 将tail节点设置为 prev
node.borrow_mut().prev = None;
if self.length == 0 {
self.head = None;
}
Some(node.borrow().value.key)
}
}
}
}
impl Drop for List {
fn drop(&mut self) {
// 回收所有节点
while let Some(_) = self.pop() {}
}
}
impl LRUCache {
fn new(capacity: i32) -> Self {
Self {
capacity: capacity as usize,
map: Default::default(),
list: List::new(),
}
}
fn get(&mut self, key: i32) -> i32 {
match self._get(key) {
None => -1,
Some(x) => x.borrow().value.value,
}
}
fn _get(&mut self, key: i32) -> Option<Rc<RefCell<Node>>> {
match self.map.get_mut(&key) {
None => None,
Some(x) => {
self.list.remove(x.clone()); // 先移除当前节点
let new = self.list.push_front(x.borrow().value.clone()); // 新插入节点
*x = new.clone();
Some(new)
}
}
}
fn put(&mut self, key: i32, value: i32) {
match self._get(key) {
None => {
if self.map.len() == self.capacity {
if let Some(x) = self.list.pop() {
self.map.remove(&x);
}
}
let x = self.list.push_front(KeyValuePair { key, value });
self.map.insert(key, x);
}
Some(x) => {
x.borrow_mut().value.value = value;
}
}
}
}