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
use std::collections::VecDeque;
use std::ops::Index;
use crate::array::{Array, ArrayMut, HasLength};
impl<A> HasLength for VecDeque<A> {
fn len(&self) -> usize {
VecDeque::len(self)
}
}
impl<A> Array for VecDeque<A> {
fn get(&self, index: usize) -> Option<&<Self as Index<usize>>::Output> {
VecDeque::get(self, index)
}
fn contains(&self, target: &<Self as Index<usize>>::Output) -> bool
where
<Self as Index<usize>>::Output: PartialEq,
{
VecDeque::contains(self, target)
}
}
impl<A> ArrayMut for VecDeque<A> {
fn get_mut(&mut self, index: usize) -> Option<&mut <Self as Index<usize>>::Output> {
VecDeque::get_mut(self, index)
}
fn swap(&mut self, index1: usize, index2: usize)
where
<Self as Index<usize>>::Output: Sized,
{
VecDeque::swap(self, index1, index2)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn vec_deque() {
let mut vec: VecDeque<_> = vec![3, 2, 1].into();
assert_eq!(3, HasLength::len(&vec));
assert_eq!(Some(&3), Array::first(&vec));
assert_eq!(Some(&1), Array::last(&vec));
ArrayMut::sort_unstable(&mut vec);
assert_eq!(Some(&1), Array::first(&vec));
assert_eq!(Some(&3), Array::last(&vec));
}
}