-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.rs
42 lines (36 loc) · 1.2 KB
/
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
37
38
39
40
41
42
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
}
}
/**
* Your AuthenticationManager object will be instantiated and called as such:
* let obj = AuthenticationManager::new(timeToLive);
* obj.generate(tokenId, currentTime);
* obj.renew(tokenId, currentTime);
* let ret_3: i32 = obj.count_unexpired_tokens(currentTime);
*/