-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-an-ordered-stream.rs
71 lines (65 loc) · 1.98 KB
/
design-an-ordered-stream.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
struct OrderedStream {
buf: Vec<(i32, String)>,
ptr: i32,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl OrderedStream {
fn new(n: i32) -> Self {
Self {
buf: vec![(0, "".to_owned()); n as usize],
ptr: 1,
}
}
fn insert(&mut self, id_key: i32, value: String) -> Vec<String> {
self.buf[id_key as usize - 1] = (id_key, value.clone());
let mut ans = vec![];
if id_key == self.ptr {
ans.push(value);
for i in self.buf[id_key as usize..].iter() {
if i.0 > self.ptr {
ans.push(i.1.clone());
self.ptr = i.0;
} else {
self.ptr += 1;
break;
}
}
}
ans
}
}
/**
* Your OrderedStream object will be instantiated and called as such:
* let obj = OrderedStream::new(n);
* let ret_1: Vec<String> = obj.insert(idKey, value);
*/
fn main() {
let mut obj = OrderedStream::new(5);
println!("{:?}", obj.insert(3, "ccccc".to_owned()));
println!("{:?}", obj.insert(1, "aaaaa".to_owned()));
println!("{:?}", obj.insert(2, "bbbbb".to_owned()));
println!("{:?}", obj.insert(5, "eeeee".to_owned()));
println!("{:?}", obj.insert(4, "ddddd".to_owned()));
}
#[cfg(test)]
mod tests {
use crate::OrderedStream;
#[test]
fn test() {
let mut obj = OrderedStream::new(5);
assert!(obj.insert(3, "ccccc".to_owned()).is_empty());
assert_eq!(obj.insert(1, "aaaaa".to_owned()), ["aaaaa".to_owned()]);
assert_eq!(
obj.insert(2, "bbbbb".to_owned()),
["bbbbb".to_owned(), "ccccc".to_owned()]
);
assert!(obj.insert(5, "eeeee".to_owned()).is_empty());
assert_eq!(
obj.insert(4, "ddddd".to_owned()),
["ddddd".to_owned(), "eeeee".to_owned()]
);
}
}