index_many/generic/
sorted_indices.rs

1use super::Indices;
2
3unsafe impl<const N: usize> Indices<N> for [usize; N] {
4    #[inline]
5    fn to_raw_indices(&self) -> [usize; N] {
6        *self
7    }
8
9    #[inline]
10    fn is_valid(&self, len: usize) -> bool {
11        let mut valid = true;
12
13        for &[a, b] in self.array_windows() {
14            valid &= a < b;
15        }
16
17        if let Some(&idx) = self.last() {
18            valid &= idx < len;
19        }
20
21        valid
22    }
23
24    #[inline(always)]
25    fn cause_invalid_panic(&self, len: usize) -> ! {
26        crate::sorted_bound_check_failed(self, len)
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::super::*;
33
34    #[test]
35    fn test_mut_normal() {
36        let mut v = vec![1, 2, 3, 4, 5];
37        let [a, b, c] = index_many_mut(&mut v, [0, 2, 4]);
38        *a += 10;
39        *b += 100;
40        *c += 1000;
41        assert_eq!(v, vec![11, 2, 103, 4, 1005]);
42    }
43
44    #[test]
45    fn test_ref_normal() {
46        let v = vec![1, 2, 3, 4, 5];
47        let [a, b, c] = index_many(&v, [0, 2, 4]);
48        assert_eq!(a, &1);
49        assert_eq!(b, &3);
50        assert_eq!(c, &5);
51        assert_eq!(v, vec![1, 2, 3, 4, 5]);
52    }
53
54    #[test]
55    fn test_mut_empty() {
56        let mut v = vec![1, 2, 3, 4, 5];
57        let [] = index_many_mut(&mut v, []);
58        assert_eq!(v, vec![1, 2, 3, 4, 5]);
59    }
60
61    #[test]
62    fn test_ref_empty() {
63        let v = vec![1, 2, 3, 4, 5];
64        let [] = index_many(&v, []);
65        assert_eq!(v, vec![1, 2, 3, 4, 5]);
66    }
67
68    #[test]
69    fn test_mut_single_first() {
70        let mut v = vec![1, 2, 3, 4, 5];
71        let [a] = index_many_mut(&mut v, [0]);
72        *a += 10;
73        assert_eq!(v, vec![11, 2, 3, 4, 5]);
74    }
75
76    #[test]
77    fn test_ref_single_first() {
78        let v = vec![1, 2, 3, 4, 5];
79        let [a] = index_many(&v, [0]);
80        assert_eq!(a, &1);
81        assert_eq!(v, vec![1, 2, 3, 4, 5]);
82    }
83
84    #[test]
85    fn test_mut_single_last() {
86        let mut v = vec![1, 2, 3, 4, 5];
87        let [a] = index_many_mut(&mut v, [4]);
88        *a += 10;
89        assert_eq!(v, vec![1, 2, 3, 4, 15]);
90    }
91
92    #[test]
93    fn test_ref_single_last() {
94        let v = vec![1, 2, 3, 4, 5];
95        let [a] = index_many(&v, [4]);
96        assert_eq!(a, &5);
97        assert_eq!(v, vec![1, 2, 3, 4, 5]);
98    }
99
100    #[test]
101    #[should_panic(
102        expected = "Index 5 is out of bounds of slice with len 5 (indices [5], position 0)"
103    )]
104    fn test_mut_oob_nonempty() {
105        let mut v = vec![1, 2, 3, 4, 5];
106        index_many_mut(&mut v, [5]);
107    }
108
109    #[test]
110    #[should_panic(
111        expected = "Index 5 is out of bounds of slice with len 5 (indices [5], position 0)"
112    )]
113    fn test_ref_oob_nonempty() {
114        let v = vec![1, 2, 3, 4, 5];
115        index_many(&v, [5]);
116    }
117
118    #[test]
119    #[should_panic(
120        expected = "Index 0 is out of bounds of slice with len 0 (indices [0], position 0)"
121    )]
122    fn test_mut_oob_empty() {
123        let mut v: Vec<i32> = vec![];
124        index_many_mut(&mut v, [0]);
125    }
126
127    #[test]
128    #[should_panic(
129        expected = "Index 0 is out of bounds of slice with len 0 (indices [0], position 0)"
130    )]
131    fn test_ref_oob_empty() {
132        let v: Vec<i32> = vec![];
133        index_many(&v, [0]);
134    }
135
136    #[test]
137    #[should_panic(expected = "Indices [3, 1, 4] are not sorted")]
138    fn test_mut_unsorted() {
139        let mut v = vec![1, 2, 3, 4, 5];
140        index_many_mut(&mut v, [3, 1, 4]);
141    }
142
143    #[test]
144    #[should_panic(expected = "Indices [3, 1, 4] are not sorted")]
145    fn test_ref_unsorted() {
146        let v = vec![1, 2, 3, 4, 5];
147        index_many(&v, [3, 1, 4]);
148    }
149
150    #[test]
151    #[should_panic(
152        expected = "Index 3 appears more than once (indices [1, 3, 3, 4], position 1 and 2)"
153    )]
154    fn test_mut_duplicate() {
155        let mut v = vec![1, 2, 3, 4, 5];
156        index_many_mut(&mut v, [1, 3, 3, 4]);
157    }
158
159    #[test]
160    #[should_panic(
161        expected = "Index 3 appears more than once (indices [1, 3, 3, 4], position 1 and 2)"
162    )]
163    fn test_ref_duplicate() {
164        let v = vec![1, 2, 3, 4, 5];
165        index_many(&v, [1, 3, 3, 4]);
166    }
167}