forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
36 lines (32 loc) · 994 Bytes
/
Solution.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
use std::collections::HashMap;
struct AuthenticationManager {
time_to_live: i32,
map: HashMap<String, i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl AuthenticationManager {
fn new(timeToLive: i32) -> Self {
Self {
time_to_live: timeToLive,
map: HashMap::new(),
}
}
fn generate(&mut self, token_id: String, current_time: i32) {
self.map.insert(token_id, current_time + self.time_to_live);
}
fn renew(&mut self, token_id: String, current_time: i32) {
if self.map.get(&token_id).unwrap_or(&0) <= ¤t_time {
return;
}
self.map.insert(token_id, current_time + self.time_to_live);
}
fn count_unexpired_tokens(&self, current_time: i32) -> i32 {
self.map
.values()
.filter(|&time| *time > current_time)
.count() as i32
}
}