-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-skiplist.rs
82 lines (73 loc) · 1.81 KB
/
design-skiplist.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
72
73
74
75
76
77
78
79
80
81
82
use std::collections::HashMap;
struct Skiplist {
skip_list: HashMap<i32, u16>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Skiplist {
fn new() -> Self {
Self {
skip_list: HashMap::new(),
}
}
fn search(&self, target: i32) -> bool {
self.skip_list.get(&target).is_some()
}
fn add(&mut self, num: i32) {
match self.skip_list.get_mut(&num) {
Some(v) => *v += 1,
None => {
self.skip_list.insert(num, 1);
}
}
}
fn erase(&mut self, num: i32) -> bool {
if self.skip_list.get(&num).is_none() {
return false;
}
let v = unsafe { self.skip_list.get_mut(&num).unwrap_unchecked() };
*v -= 1;
if *v == 0 {
self.skip_list.remove(&num);
}
true
}
}
/**
* Your Skiplist object will be instantiated and called as such:
* let obj = Skiplist::new();
* let ret_1: bool = obj.search(target);
* obj.add(num);
* let ret_3: bool = obj.erase(num);
*/
fn main() {
let mut obj = Skiplist::new();
obj.add(1);
obj.add(2);
obj.add(3);
println!("{:?} = false", obj.search(0));
obj.add(4);
println!("{:?} = true", obj.search(1));
println!("{:?} = false", obj.erase(0));
println!("{:?} = true", obj.erase(1));
println!("{:?} = false", obj.search(1));
}
#[cfg(test)]
mod tests {
use crate::Skiplist;
#[test]
fn test() {
let mut obj = Skiplist::new();
obj.add(1);
obj.add(2);
obj.add(3);
assert!(!obj.search(0));
obj.add(4);
assert!(obj.search(1));
assert!(!obj.erase(0));
assert!(obj.erase(1));
assert!(!obj.search(1));
}
}