-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsnapshot_array.rs
More file actions
60 lines (50 loc) · 1.38 KB
/
snapshot_array.rs
File metadata and controls
60 lines (50 loc) · 1.38 KB
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
use std::collections::HashMap;
#[derive(Debug)]
struct SnapshotArray {
snapshot: i32,
data: HashMap<i32, Vec<(i32, i32)>>,
}
#[allow(dead_code)]
impl SnapshotArray {
fn new(length: i32) -> Self {
let mut x = Self {
data: HashMap::new(),
snapshot: 0,
};
for i in 0..length {
x.data.insert(i, vec![(0, 0)]);
}
x
}
fn set(&mut self, index: i32, val: i32) {
self.data.entry(index).and_modify(|e| {
if let Some(last) = e.iter().last() {
if last.0 == self.snapshot {
let len = e.len() - 1;
e[len] = (self.snapshot, val);
} else {
e.push((self.snapshot, val));
}
}
});
}
fn snap(&mut self) -> i32 {
self.snapshot += 1;
self.snapshot - 1
}
fn get(&self, index: i32, snap_id: i32) -> i32 {
if let Some(versions) = self.data.get(&index) {
let (mut left, mut right) = (0, versions.len());
while left < right {
let mid = left + (right - left) / 2;
if versions[mid].0 <= snap_id {
left = mid + 1;
} else {
right = mid;
}
}
return versions[left - 1].1;
}
0
}
}