blob: f35bd69988e9259329cb919216e91539bb084dd2 (
plain)
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
|
fn main() {
println!("Hello, world!");
let mut a = MyHashMap::new();
a.put(1,1);
a.put(2,2);
a.get(1);
a.get(3);
a.put(2,1);
a.get(2);
a.remove(2);
a.get(2);
}
struct MyHashMap {
map: Vec<(i32,i32)>
}
impl MyHashMap {
fn new() -> Self {
MyHashMap {
map: Vec::new()
}
}
fn put(&mut self, key: i32, value: i32) {
match self.map.iter_mut().find(|x| x.0 == key) {
Some(e) => e.1 = value,
None => self.map.push((key, value))
};
}
fn get(&self, key: i32) -> i32 {
self.map.iter().find(|&x| x.0 == key).map(|x| x.1).unwrap_or(-1)
}
fn remove(&mut self, key: i32) {
if let Some(e) = self.map.iter().position(|x| x.0 == key) {
self.map.swap_remove(e);
}
}
}
|