fera_array/
vec.rs

1use {Array, DynamicArray};
2
3use std::ops::{Index, IndexMut};
4
5#[derive(Clone)]
6pub struct VecArray<T>(Vec<T>);
7
8impl<T> Index<usize> for VecArray<T> {
9    type Output = T;
10
11    #[inline]
12    fn index(&self, index: usize) -> &T {
13        self.0.index(index)
14    }
15}
16
17impl<T> IndexMut<usize> for VecArray<T> {
18    #[inline]
19    fn index_mut(&mut self, index: usize) -> &mut T {
20        self.0.index_mut(index)
21    }
22}
23
24impl<T> Array<T> for VecArray<T> {
25    #[inline]
26    fn with_value(value: T, n: usize) -> Self
27        where Self: Sized,
28              T: Clone
29    {
30        VecArray(vec![value; n])
31    }
32
33    #[inline]
34    fn len(&self) -> usize {
35        self.0.len()
36    }
37
38    #[inline]
39    fn is_empty(&self) -> bool {
40        self.0.is_empty()
41    }
42
43    #[inline]
44    fn get(&self, index: usize) -> Option<&T> {
45        self.0.get(index)
46    }
47
48    #[inline]
49    fn get_mut(&mut self, index: usize) -> Option<&mut T> {
50        self.0.get_mut(index)
51    }
52
53    #[inline]
54    unsafe fn get_unchecked(&self, index: usize) -> &T {
55        self.0.get_unchecked(index)
56    }
57
58    #[inline]
59    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
60        self.0.get_unchecked_mut(index)
61    }
62}
63
64impl<T> DynamicArray<T> for VecArray<T> {
65    #[inline]
66    fn with_capacity(capacity: usize) -> Self
67        where Self: Sized
68    {
69        VecArray(Vec::with_capacity(capacity))
70    }
71
72    #[inline]
73    fn capacity(&self) -> usize {
74        self.0.capacity()
75    }
76
77    #[inline]
78    unsafe fn set_len(&mut self, len: usize) {
79        self.0.set_len(len);
80    }
81
82    #[inline]
83    fn reserve(&mut self, additional: usize) {
84        self.0.reserve(additional);
85    }
86
87    #[inline]
88    fn push(&mut self, value: T) {
89        self.0.push(value);
90    }
91
92    #[inline]
93    fn extend<I>(&mut self, iter: I)
94        where I: IntoIterator<Item = T>
95    {
96        Extend::extend(&mut self.0, iter);
97    }
98
99    #[inline]
100    fn extend_from_slice(&mut self, other: &[T])
101        where T: Clone
102    {
103        self.0.extend_from_slice(other);
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    use {ArrayTests, DynamicArrayTests};
111
112    struct T;
113
114    impl ArrayTests for T {
115        type A = VecArray<usize>;
116    }
117
118    delegate_tests!{
119        T,
120        basic_0,
121        basic_001k,
122        basic_100k,
123        clone_001k,
124        clone_100k
125    }
126
127    impl DynamicArrayTests for T {}
128
129    delegate_tests!{
130        T,
131        capacity,
132        push_1k,
133        clone_push
134    }
135
136    #[cfg(all(feature = "nightly", test))]
137    mod benchs {
138        use super::T;
139        use test::Bencher;
140        use ArrayBenchs;
141
142        impl ArrayBenchs for T {}
143
144        delegate_benchs!{
145            T,
146            fold_xor_0001k,
147            fold_xor_0010k,
148            fold_xor_0100k,
149            fold_xor_1000k,
150            clone_change_0001k,
151            clone_change_0010k,
152            clone_change_0100k,
153            clone_change_1000k
154        }
155    }
156}