-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-based-key-value-store_improved_syntax.rs
46 lines (38 loc) · 1.19 KB
/
time-based-key-value-store_improved_syntax.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
// https://leetcode.com/problems/time-based-key-value-store
use std::collections::HashMap;
#[derive(Default)]
struct TimeMap {
store: HashMap<String, Vec<(i32, String)>>
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl TimeMap {
fn new() -> Self {
Self::default()
}
fn set(&mut self, key: String, value: String, timestamp: i32) {
self.store.entry(key).or_default().push((timestamp, value));
}
fn get(&self, key: String, timestamp: i32) -> String {
match self.store.get(&key) {
Some(val_snaps) => {
return match val_snaps.partition_point(|&(ts, _)| ts <= timestamp) {
0 => Self::default_value(),
partition_point => val_snaps[partition_point - 1].1.clone(),
}
},
_ => Self::default_value(),
}
}
fn default_value() -> String{
"".to_string()
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* let obj = TimeMap::new();
* obj.set(key, value, timestamp);
* let ret_2: String = obj.get(key, timestamp);
*/