/rust/registry/src/index.crates.io-6f17d22bba15001f/indexmap-2.10.0/src/map/slice.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::{ |
2 | | Bucket, IndexMap, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys, Values, ValuesMut, |
3 | | }; |
4 | | use crate::util::{slice_eq, try_simplify_range}; |
5 | | use crate::GetDisjointMutError; |
6 | | |
7 | | use alloc::boxed::Box; |
8 | | use alloc::vec::Vec; |
9 | | use core::cmp::Ordering; |
10 | | use core::fmt; |
11 | | use core::hash::{Hash, Hasher}; |
12 | | use core::ops::{self, Bound, Index, IndexMut, RangeBounds}; |
13 | | |
14 | | /// A dynamically-sized slice of key-value pairs in an [`IndexMap`]. |
15 | | /// |
16 | | /// This supports indexed operations much like a `[(K, V)]` slice, |
17 | | /// but not any hashed operations on the map keys. |
18 | | /// |
19 | | /// Unlike `IndexMap`, `Slice` does consider the order for [`PartialEq`] |
20 | | /// and [`Eq`], and it also implements [`PartialOrd`], [`Ord`], and [`Hash`]. |
21 | | #[repr(transparent)] |
22 | | pub struct Slice<K, V> { |
23 | | pub(crate) entries: [Bucket<K, V>], |
24 | | } |
25 | | |
26 | | // SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`, |
27 | | // and reference lifetimes are bound together in function signatures. |
28 | | #[allow(unsafe_code)] |
29 | | impl<K, V> Slice<K, V> { |
30 | 0 | pub(super) const fn from_slice(entries: &[Bucket<K, V>]) -> &Self { |
31 | 0 | unsafe { &*(entries as *const [Bucket<K, V>] as *const Self) } |
32 | 0 | } |
33 | | |
34 | 0 | pub(super) fn from_mut_slice(entries: &mut [Bucket<K, V>]) -> &mut Self { |
35 | 0 | unsafe { &mut *(entries as *mut [Bucket<K, V>] as *mut Self) } |
36 | 0 | } |
37 | | |
38 | 0 | pub(super) fn from_boxed(entries: Box<[Bucket<K, V>]>) -> Box<Self> { |
39 | 0 | unsafe { Box::from_raw(Box::into_raw(entries) as *mut Self) } |
40 | 0 | } |
41 | | |
42 | 0 | fn into_boxed(self: Box<Self>) -> Box<[Bucket<K, V>]> { |
43 | 0 | unsafe { Box::from_raw(Box::into_raw(self) as *mut [Bucket<K, V>]) } |
44 | 0 | } |
45 | | } |
46 | | |
47 | | impl<K, V> Slice<K, V> { |
48 | 0 | pub(crate) fn into_entries(self: Box<Self>) -> Vec<Bucket<K, V>> { |
49 | 0 | self.into_boxed().into_vec() |
50 | 0 | } |
51 | | |
52 | | /// Returns an empty slice. |
53 | 0 | pub const fn new<'a>() -> &'a Self { |
54 | 0 | Self::from_slice(&[]) |
55 | 0 | } |
56 | | |
57 | | /// Returns an empty mutable slice. |
58 | 0 | pub fn new_mut<'a>() -> &'a mut Self { |
59 | 0 | Self::from_mut_slice(&mut []) |
60 | 0 | } |
61 | | |
62 | | /// Return the number of key-value pairs in the map slice. |
63 | | #[inline] |
64 | 0 | pub const fn len(&self) -> usize { |
65 | 0 | self.entries.len() |
66 | 0 | } |
67 | | |
68 | | /// Returns true if the map slice contains no elements. |
69 | | #[inline] |
70 | 0 | pub const fn is_empty(&self) -> bool { |
71 | 0 | self.entries.is_empty() |
72 | 0 | } |
73 | | |
74 | | /// Get a key-value pair by index. |
75 | | /// |
76 | | /// Valid indices are `0 <= index < self.len()`. |
77 | 0 | pub fn get_index(&self, index: usize) -> Option<(&K, &V)> { |
78 | 0 | self.entries.get(index).map(Bucket::refs) |
79 | 0 | } |
80 | | |
81 | | /// Get a key-value pair by index, with mutable access to the value. |
82 | | /// |
83 | | /// Valid indices are `0 <= index < self.len()`. |
84 | 0 | pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { |
85 | 0 | self.entries.get_mut(index).map(Bucket::ref_mut) |
86 | 0 | } |
87 | | |
88 | | /// Returns a slice of key-value pairs in the given range of indices. |
89 | | /// |
90 | | /// Valid indices are `0 <= index < self.len()`. |
91 | 0 | pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> { |
92 | 0 | let range = try_simplify_range(range, self.entries.len())?; |
93 | 0 | self.entries.get(range).map(Slice::from_slice) |
94 | 0 | } |
95 | | |
96 | | /// Returns a mutable slice of key-value pairs in the given range of indices. |
97 | | /// |
98 | | /// Valid indices are `0 <= index < self.len()`. |
99 | 0 | pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Self> { |
100 | 0 | let range = try_simplify_range(range, self.entries.len())?; |
101 | 0 | self.entries.get_mut(range).map(Slice::from_mut_slice) |
102 | 0 | } |
103 | | |
104 | | /// Get the first key-value pair. |
105 | 0 | pub fn first(&self) -> Option<(&K, &V)> { |
106 | 0 | self.entries.first().map(Bucket::refs) |
107 | 0 | } |
108 | | |
109 | | /// Get the first key-value pair, with mutable access to the value. |
110 | 0 | pub fn first_mut(&mut self) -> Option<(&K, &mut V)> { |
111 | 0 | self.entries.first_mut().map(Bucket::ref_mut) |
112 | 0 | } |
113 | | |
114 | | /// Get the last key-value pair. |
115 | 0 | pub fn last(&self) -> Option<(&K, &V)> { |
116 | 0 | self.entries.last().map(Bucket::refs) |
117 | 0 | } |
118 | | |
119 | | /// Get the last key-value pair, with mutable access to the value. |
120 | 0 | pub fn last_mut(&mut self) -> Option<(&K, &mut V)> { |
121 | 0 | self.entries.last_mut().map(Bucket::ref_mut) |
122 | 0 | } |
123 | | |
124 | | /// Divides one slice into two at an index. |
125 | | /// |
126 | | /// ***Panics*** if `index > len`. |
127 | | #[track_caller] |
128 | 0 | pub fn split_at(&self, index: usize) -> (&Self, &Self) { |
129 | 0 | let (first, second) = self.entries.split_at(index); |
130 | 0 | (Self::from_slice(first), Self::from_slice(second)) |
131 | 0 | } |
132 | | |
133 | | /// Divides one mutable slice into two at an index. |
134 | | /// |
135 | | /// ***Panics*** if `index > len`. |
136 | | #[track_caller] |
137 | 0 | pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) { |
138 | 0 | let (first, second) = self.entries.split_at_mut(index); |
139 | 0 | (Self::from_mut_slice(first), Self::from_mut_slice(second)) |
140 | 0 | } |
141 | | |
142 | | /// Returns the first key-value pair and the rest of the slice, |
143 | | /// or `None` if it is empty. |
144 | 0 | pub fn split_first(&self) -> Option<((&K, &V), &Self)> { |
145 | 0 | if let [first, rest @ ..] = &self.entries { |
146 | 0 | Some((first.refs(), Self::from_slice(rest))) |
147 | | } else { |
148 | 0 | None |
149 | | } |
150 | 0 | } |
151 | | |
152 | | /// Returns the first key-value pair and the rest of the slice, |
153 | | /// with mutable access to the value, or `None` if it is empty. |
154 | 0 | pub fn split_first_mut(&mut self) -> Option<((&K, &mut V), &mut Self)> { |
155 | 0 | if let [first, rest @ ..] = &mut self.entries { |
156 | 0 | Some((first.ref_mut(), Self::from_mut_slice(rest))) |
157 | | } else { |
158 | 0 | None |
159 | | } |
160 | 0 | } |
161 | | |
162 | | /// Returns the last key-value pair and the rest of the slice, |
163 | | /// or `None` if it is empty. |
164 | 0 | pub fn split_last(&self) -> Option<((&K, &V), &Self)> { |
165 | 0 | if let [rest @ .., last] = &self.entries { |
166 | 0 | Some((last.refs(), Self::from_slice(rest))) |
167 | | } else { |
168 | 0 | None |
169 | | } |
170 | 0 | } |
171 | | |
172 | | /// Returns the last key-value pair and the rest of the slice, |
173 | | /// with mutable access to the value, or `None` if it is empty. |
174 | 0 | pub fn split_last_mut(&mut self) -> Option<((&K, &mut V), &mut Self)> { |
175 | 0 | if let [rest @ .., last] = &mut self.entries { |
176 | 0 | Some((last.ref_mut(), Self::from_mut_slice(rest))) |
177 | | } else { |
178 | 0 | None |
179 | | } |
180 | 0 | } |
181 | | |
182 | | /// Return an iterator over the key-value pairs of the map slice. |
183 | 0 | pub fn iter(&self) -> Iter<'_, K, V> { |
184 | 0 | Iter::new(&self.entries) |
185 | 0 | } |
186 | | |
187 | | /// Return an iterator over the key-value pairs of the map slice. |
188 | 0 | pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { |
189 | 0 | IterMut::new(&mut self.entries) |
190 | 0 | } |
191 | | |
192 | | /// Return an iterator over the keys of the map slice. |
193 | 0 | pub fn keys(&self) -> Keys<'_, K, V> { |
194 | 0 | Keys::new(&self.entries) |
195 | 0 | } |
196 | | |
197 | | /// Return an owning iterator over the keys of the map slice. |
198 | 0 | pub fn into_keys(self: Box<Self>) -> IntoKeys<K, V> { |
199 | 0 | IntoKeys::new(self.into_entries()) |
200 | 0 | } |
201 | | |
202 | | /// Return an iterator over the values of the map slice. |
203 | 0 | pub fn values(&self) -> Values<'_, K, V> { |
204 | 0 | Values::new(&self.entries) |
205 | 0 | } |
206 | | |
207 | | /// Return an iterator over mutable references to the the values of the map slice. |
208 | 0 | pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> { |
209 | 0 | ValuesMut::new(&mut self.entries) |
210 | 0 | } |
211 | | |
212 | | /// Return an owning iterator over the values of the map slice. |
213 | 0 | pub fn into_values(self: Box<Self>) -> IntoValues<K, V> { |
214 | 0 | IntoValues::new(self.into_entries()) |
215 | 0 | } |
216 | | |
217 | | /// Search over a sorted map for a key. |
218 | | /// |
219 | | /// Returns the position where that key is present, or the position where it can be inserted to |
220 | | /// maintain the sort. See [`slice::binary_search`] for more details. |
221 | | /// |
222 | | /// Computes in **O(log(n))** time, which is notably less scalable than looking the key up in |
223 | | /// the map this is a slice from using [`IndexMap::get_index_of`], but this can also position |
224 | | /// missing keys. |
225 | 0 | pub fn binary_search_keys(&self, x: &K) -> Result<usize, usize> |
226 | 0 | where |
227 | 0 | K: Ord, |
228 | 0 | { |
229 | 0 | self.binary_search_by(|p, _| p.cmp(x)) |
230 | 0 | } |
231 | | |
232 | | /// Search over a sorted map with a comparator function. |
233 | | /// |
234 | | /// Returns the position where that value is present, or the position where it can be inserted |
235 | | /// to maintain the sort. See [`slice::binary_search_by`] for more details. |
236 | | /// |
237 | | /// Computes in **O(log(n))** time. |
238 | | #[inline] |
239 | 0 | pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize> |
240 | 0 | where |
241 | 0 | F: FnMut(&'a K, &'a V) -> Ordering, |
242 | 0 | { |
243 | 0 | self.entries.binary_search_by(move |a| f(&a.key, &a.value)) |
244 | 0 | } |
245 | | |
246 | | /// Search over a sorted map with an extraction function. |
247 | | /// |
248 | | /// Returns the position where that value is present, or the position where it can be inserted |
249 | | /// to maintain the sort. See [`slice::binary_search_by_key`] for more details. |
250 | | /// |
251 | | /// Computes in **O(log(n))** time. |
252 | | #[inline] |
253 | 0 | pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize> |
254 | 0 | where |
255 | 0 | F: FnMut(&'a K, &'a V) -> B, |
256 | 0 | B: Ord, |
257 | 0 | { |
258 | 0 | self.binary_search_by(|k, v| f(k, v).cmp(b)) |
259 | 0 | } |
260 | | |
261 | | /// Returns the index of the partition point of a sorted map according to the given predicate |
262 | | /// (the index of the first element of the second partition). |
263 | | /// |
264 | | /// See [`slice::partition_point`] for more details. |
265 | | /// |
266 | | /// Computes in **O(log(n))** time. |
267 | | #[must_use] |
268 | 0 | pub fn partition_point<P>(&self, mut pred: P) -> usize |
269 | 0 | where |
270 | 0 | P: FnMut(&K, &V) -> bool, |
271 | 0 | { |
272 | 0 | self.entries |
273 | 0 | .partition_point(move |a| pred(&a.key, &a.value)) |
274 | 0 | } |
275 | | |
276 | | /// Get an array of `N` key-value pairs by `N` indices |
277 | | /// |
278 | | /// Valid indices are *0 <= index < self.len()* and each index needs to be unique. |
279 | 0 | pub fn get_disjoint_mut<const N: usize>( |
280 | 0 | &mut self, |
281 | 0 | indices: [usize; N], |
282 | 0 | ) -> Result<[(&K, &mut V); N], GetDisjointMutError> { |
283 | 0 | let indices = indices.map(Some); |
284 | 0 | let key_values = self.get_disjoint_opt_mut(indices)?; |
285 | 0 | Ok(key_values.map(Option::unwrap)) |
286 | 0 | } |
287 | | |
288 | | #[allow(unsafe_code)] |
289 | 0 | pub(crate) fn get_disjoint_opt_mut<const N: usize>( |
290 | 0 | &mut self, |
291 | 0 | indices: [Option<usize>; N], |
292 | 0 | ) -> Result<[Option<(&K, &mut V)>; N], GetDisjointMutError> { |
293 | 0 | // SAFETY: Can't allow duplicate indices as we would return several mutable refs to the same data. |
294 | 0 | let len = self.len(); |
295 | 0 | for i in 0..N { |
296 | 0 | if let Some(idx) = indices[i] { |
297 | 0 | if idx >= len { |
298 | 0 | return Err(GetDisjointMutError::IndexOutOfBounds); |
299 | 0 | } else if indices[..i].contains(&Some(idx)) { |
300 | 0 | return Err(GetDisjointMutError::OverlappingIndices); |
301 | 0 | } |
302 | 0 | } |
303 | | } |
304 | | |
305 | 0 | let entries_ptr = self.entries.as_mut_ptr(); |
306 | 0 | let out = indices.map(|idx_opt| { |
307 | 0 | match idx_opt { |
308 | 0 | Some(idx) => { |
309 | 0 | // SAFETY: The base pointer is valid as it comes from a slice and the reference is always |
310 | 0 | // in-bounds & unique as we've already checked the indices above. |
311 | 0 | let kv = unsafe { (*(entries_ptr.add(idx))).ref_mut() }; |
312 | 0 | Some(kv) |
313 | | } |
314 | 0 | None => None, |
315 | | } |
316 | 0 | }); |
317 | 0 |
|
318 | 0 | Ok(out) |
319 | 0 | } |
320 | | } |
321 | | |
322 | | impl<'a, K, V> IntoIterator for &'a Slice<K, V> { |
323 | | type IntoIter = Iter<'a, K, V>; |
324 | | type Item = (&'a K, &'a V); |
325 | | |
326 | 0 | fn into_iter(self) -> Self::IntoIter { |
327 | 0 | self.iter() |
328 | 0 | } |
329 | | } |
330 | | |
331 | | impl<'a, K, V> IntoIterator for &'a mut Slice<K, V> { |
332 | | type IntoIter = IterMut<'a, K, V>; |
333 | | type Item = (&'a K, &'a mut V); |
334 | | |
335 | 0 | fn into_iter(self) -> Self::IntoIter { |
336 | 0 | self.iter_mut() |
337 | 0 | } |
338 | | } |
339 | | |
340 | | impl<K, V> IntoIterator for Box<Slice<K, V>> { |
341 | | type IntoIter = IntoIter<K, V>; |
342 | | type Item = (K, V); |
343 | | |
344 | 0 | fn into_iter(self) -> Self::IntoIter { |
345 | 0 | IntoIter::new(self.into_entries()) |
346 | 0 | } |
347 | | } |
348 | | |
349 | | impl<K, V> Default for &'_ Slice<K, V> { |
350 | 0 | fn default() -> Self { |
351 | 0 | Slice::from_slice(&[]) |
352 | 0 | } |
353 | | } |
354 | | |
355 | | impl<K, V> Default for &'_ mut Slice<K, V> { |
356 | 0 | fn default() -> Self { |
357 | 0 | Slice::from_mut_slice(&mut []) |
358 | 0 | } |
359 | | } |
360 | | |
361 | | impl<K, V> Default for Box<Slice<K, V>> { |
362 | 0 | fn default() -> Self { |
363 | 0 | Slice::from_boxed(Box::default()) |
364 | 0 | } |
365 | | } |
366 | | |
367 | | impl<K: Clone, V: Clone> Clone for Box<Slice<K, V>> { |
368 | 0 | fn clone(&self) -> Self { |
369 | 0 | Slice::from_boxed(self.entries.to_vec().into_boxed_slice()) |
370 | 0 | } |
371 | | } |
372 | | |
373 | | impl<K: Copy, V: Copy> From<&Slice<K, V>> for Box<Slice<K, V>> { |
374 | 0 | fn from(slice: &Slice<K, V>) -> Self { |
375 | 0 | Slice::from_boxed(Box::from(&slice.entries)) |
376 | 0 | } |
377 | | } |
378 | | |
379 | | impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Slice<K, V> { |
380 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
381 | 0 | f.debug_list().entries(self).finish() |
382 | 0 | } |
383 | | } |
384 | | |
385 | | impl<K, V, K2, V2> PartialEq<Slice<K2, V2>> for Slice<K, V> |
386 | | where |
387 | | K: PartialEq<K2>, |
388 | | V: PartialEq<V2>, |
389 | | { |
390 | 0 | fn eq(&self, other: &Slice<K2, V2>) -> bool { |
391 | 0 | slice_eq(&self.entries, &other.entries, |b1, b2| { |
392 | 0 | b1.key == b2.key && b1.value == b2.value |
393 | 0 | }) |
394 | 0 | } |
395 | | } |
396 | | |
397 | | impl<K, V, K2, V2> PartialEq<[(K2, V2)]> for Slice<K, V> |
398 | | where |
399 | | K: PartialEq<K2>, |
400 | | V: PartialEq<V2>, |
401 | | { |
402 | 0 | fn eq(&self, other: &[(K2, V2)]) -> bool { |
403 | 0 | slice_eq(&self.entries, other, |b, t| b.key == t.0 && b.value == t.1) |
404 | 0 | } |
405 | | } |
406 | | |
407 | | impl<K, V, K2, V2> PartialEq<Slice<K2, V2>> for [(K, V)] |
408 | | where |
409 | | K: PartialEq<K2>, |
410 | | V: PartialEq<V2>, |
411 | | { |
412 | 0 | fn eq(&self, other: &Slice<K2, V2>) -> bool { |
413 | 0 | slice_eq(self, &other.entries, |t, b| t.0 == b.key && t.1 == b.value) |
414 | 0 | } |
415 | | } |
416 | | |
417 | | impl<K, V, K2, V2, const N: usize> PartialEq<[(K2, V2); N]> for Slice<K, V> |
418 | | where |
419 | | K: PartialEq<K2>, |
420 | | V: PartialEq<V2>, |
421 | | { |
422 | 0 | fn eq(&self, other: &[(K2, V2); N]) -> bool { |
423 | 0 | <Self as PartialEq<[_]>>::eq(self, other) |
424 | 0 | } |
425 | | } |
426 | | |
427 | | impl<K, V, const N: usize, K2, V2> PartialEq<Slice<K2, V2>> for [(K, V); N] |
428 | | where |
429 | | K: PartialEq<K2>, |
430 | | V: PartialEq<V2>, |
431 | | { |
432 | 0 | fn eq(&self, other: &Slice<K2, V2>) -> bool { |
433 | 0 | <[_] as PartialEq<_>>::eq(self, other) |
434 | 0 | } |
435 | | } |
436 | | |
437 | | impl<K: Eq, V: Eq> Eq for Slice<K, V> {} |
438 | | |
439 | | impl<K: PartialOrd, V: PartialOrd> PartialOrd for Slice<K, V> { |
440 | 0 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
441 | 0 | self.iter().partial_cmp(other) |
442 | 0 | } |
443 | | } |
444 | | |
445 | | impl<K: Ord, V: Ord> Ord for Slice<K, V> { |
446 | 0 | fn cmp(&self, other: &Self) -> Ordering { |
447 | 0 | self.iter().cmp(other) |
448 | 0 | } |
449 | | } |
450 | | |
451 | | impl<K: Hash, V: Hash> Hash for Slice<K, V> { |
452 | 0 | fn hash<H: Hasher>(&self, state: &mut H) { |
453 | 0 | self.len().hash(state); |
454 | 0 | for (key, value) in self { |
455 | 0 | key.hash(state); |
456 | 0 | value.hash(state); |
457 | 0 | } |
458 | 0 | } |
459 | | } |
460 | | |
461 | | impl<K, V> Index<usize> for Slice<K, V> { |
462 | | type Output = V; |
463 | | |
464 | 0 | fn index(&self, index: usize) -> &V { |
465 | 0 | &self.entries[index].value |
466 | 0 | } |
467 | | } |
468 | | |
469 | | impl<K, V> IndexMut<usize> for Slice<K, V> { |
470 | 0 | fn index_mut(&mut self, index: usize) -> &mut V { |
471 | 0 | &mut self.entries[index].value |
472 | 0 | } |
473 | | } |
474 | | |
475 | | // We can't have `impl<I: RangeBounds<usize>> Index<I>` because that conflicts |
476 | | // both upstream with `Index<usize>` and downstream with `Index<&Q>`. |
477 | | // Instead, we repeat the implementations for all the core range types. |
478 | | macro_rules! impl_index { |
479 | | ($($range:ty),*) => {$( |
480 | | impl<K, V, S> Index<$range> for IndexMap<K, V, S> { |
481 | | type Output = Slice<K, V>; |
482 | | |
483 | 0 | fn index(&self, range: $range) -> &Self::Output { |
484 | 0 | Slice::from_slice(&self.as_entries()[range]) |
485 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<core::ops::range::Range<usize>>>::index Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<core::ops::range::RangeFrom<usize>>>::index Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<core::ops::range::RangeFull>>::index Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<core::ops::range::RangeInclusive<usize>>>::index Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<core::ops::range::RangeTo<usize>>>::index Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<core::ops::range::RangeToInclusive<usize>>>::index Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::Index<(core::ops::range::Bound<usize>, core::ops::range::Bound<usize>)>>::index |
486 | | } |
487 | | |
488 | | impl<K, V, S> IndexMut<$range> for IndexMap<K, V, S> { |
489 | 0 | fn index_mut(&mut self, range: $range) -> &mut Self::Output { |
490 | 0 | Slice::from_mut_slice(&mut self.as_entries_mut()[range]) |
491 | 0 | } Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<core::ops::range::Range<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<core::ops::range::RangeFrom<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<core::ops::range::RangeInclusive<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<core::ops::range::RangeTo<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<core::ops::range::RangeToInclusive<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::IndexMap<_, _, _> as core::ops::index::IndexMut<(core::ops::range::Bound<usize>, core::ops::range::Bound<usize>)>>::index_mut |
492 | | } |
493 | | |
494 | | impl<K, V> Index<$range> for Slice<K, V> { |
495 | | type Output = Slice<K, V>; |
496 | | |
497 | 0 | fn index(&self, range: $range) -> &Self { |
498 | 0 | Self::from_slice(&self.entries[range]) |
499 | 0 | } Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<core::ops::range::Range<usize>>>::index Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<core::ops::range::RangeFrom<usize>>>::index Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<core::ops::range::RangeFull>>::index Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<core::ops::range::RangeInclusive<usize>>>::index Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<core::ops::range::RangeTo<usize>>>::index Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<core::ops::range::RangeToInclusive<usize>>>::index Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::Index<(core::ops::range::Bound<usize>, core::ops::range::Bound<usize>)>>::index |
500 | | } |
501 | | |
502 | | impl<K, V> IndexMut<$range> for Slice<K, V> { |
503 | 0 | fn index_mut(&mut self, range: $range) -> &mut Self { |
504 | 0 | Self::from_mut_slice(&mut self.entries[range]) |
505 | 0 | } Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<core::ops::range::Range<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<core::ops::range::RangeFrom<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<core::ops::range::RangeInclusive<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<core::ops::range::RangeTo<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<core::ops::range::RangeToInclusive<usize>>>::index_mut Unexecuted instantiation: <indexmap::map::slice::Slice<_, _> as core::ops::index::IndexMut<(core::ops::range::Bound<usize>, core::ops::range::Bound<usize>)>>::index_mut |
506 | | } |
507 | | )*} |
508 | | } |
509 | | impl_index!( |
510 | | ops::Range<usize>, |
511 | | ops::RangeFrom<usize>, |
512 | | ops::RangeFull, |
513 | | ops::RangeInclusive<usize>, |
514 | | ops::RangeTo<usize>, |
515 | | ops::RangeToInclusive<usize>, |
516 | | (Bound<usize>, Bound<usize>) |
517 | | ); |
518 | | |
519 | | #[cfg(test)] |
520 | | mod tests { |
521 | | use super::*; |
522 | | |
523 | | #[test] |
524 | | fn slice_index() { |
525 | | fn check( |
526 | | vec_slice: &[(i32, i32)], |
527 | | map_slice: &Slice<i32, i32>, |
528 | | sub_slice: &Slice<i32, i32>, |
529 | | ) { |
530 | | assert_eq!(map_slice as *const _, sub_slice as *const _); |
531 | | itertools::assert_equal( |
532 | | vec_slice.iter().copied(), |
533 | | map_slice.iter().map(|(&k, &v)| (k, v)), |
534 | | ); |
535 | | itertools::assert_equal(vec_slice.iter().map(|(k, _)| k), map_slice.keys()); |
536 | | itertools::assert_equal(vec_slice.iter().map(|(_, v)| v), map_slice.values()); |
537 | | } |
538 | | |
539 | | let vec: Vec<(i32, i32)> = (0..10).map(|i| (i, i * i)).collect(); |
540 | | let map: IndexMap<i32, i32> = vec.iter().cloned().collect(); |
541 | | let slice = map.as_slice(); |
542 | | |
543 | | // RangeFull |
544 | | check(&vec[..], &map[..], &slice[..]); |
545 | | |
546 | | for i in 0usize..10 { |
547 | | // Index |
548 | | assert_eq!(vec[i].1, map[i]); |
549 | | assert_eq!(vec[i].1, slice[i]); |
550 | | assert_eq!(map[&(i as i32)], map[i]); |
551 | | assert_eq!(map[&(i as i32)], slice[i]); |
552 | | |
553 | | // RangeFrom |
554 | | check(&vec[i..], &map[i..], &slice[i..]); |
555 | | |
556 | | // RangeTo |
557 | | check(&vec[..i], &map[..i], &slice[..i]); |
558 | | |
559 | | // RangeToInclusive |
560 | | check(&vec[..=i], &map[..=i], &slice[..=i]); |
561 | | |
562 | | // (Bound<usize>, Bound<usize>) |
563 | | let bounds = (Bound::Excluded(i), Bound::Unbounded); |
564 | | check(&vec[i + 1..], &map[bounds], &slice[bounds]); |
565 | | |
566 | | for j in i..=10 { |
567 | | // Range |
568 | | check(&vec[i..j], &map[i..j], &slice[i..j]); |
569 | | } |
570 | | |
571 | | for j in i..10 { |
572 | | // RangeInclusive |
573 | | check(&vec[i..=j], &map[i..=j], &slice[i..=j]); |
574 | | } |
575 | | } |
576 | | } |
577 | | |
578 | | #[test] |
579 | | fn slice_index_mut() { |
580 | | fn check_mut( |
581 | | vec_slice: &[(i32, i32)], |
582 | | map_slice: &mut Slice<i32, i32>, |
583 | | sub_slice: &mut Slice<i32, i32>, |
584 | | ) { |
585 | | assert_eq!(map_slice, sub_slice); |
586 | | itertools::assert_equal( |
587 | | vec_slice.iter().copied(), |
588 | | map_slice.iter_mut().map(|(&k, &mut v)| (k, v)), |
589 | | ); |
590 | | itertools::assert_equal( |
591 | | vec_slice.iter().map(|&(_, v)| v), |
592 | | map_slice.values_mut().map(|&mut v| v), |
593 | | ); |
594 | | } |
595 | | |
596 | | let vec: Vec<(i32, i32)> = (0..10).map(|i| (i, i * i)).collect(); |
597 | | let mut map: IndexMap<i32, i32> = vec.iter().cloned().collect(); |
598 | | let mut map2 = map.clone(); |
599 | | let slice = map2.as_mut_slice(); |
600 | | |
601 | | // RangeFull |
602 | | check_mut(&vec[..], &mut map[..], &mut slice[..]); |
603 | | |
604 | | for i in 0usize..10 { |
605 | | // IndexMut |
606 | | assert_eq!(&mut map[i], &mut slice[i]); |
607 | | |
608 | | // RangeFrom |
609 | | check_mut(&vec[i..], &mut map[i..], &mut slice[i..]); |
610 | | |
611 | | // RangeTo |
612 | | check_mut(&vec[..i], &mut map[..i], &mut slice[..i]); |
613 | | |
614 | | // RangeToInclusive |
615 | | check_mut(&vec[..=i], &mut map[..=i], &mut slice[..=i]); |
616 | | |
617 | | // (Bound<usize>, Bound<usize>) |
618 | | let bounds = (Bound::Excluded(i), Bound::Unbounded); |
619 | | check_mut(&vec[i + 1..], &mut map[bounds], &mut slice[bounds]); |
620 | | |
621 | | for j in i..=10 { |
622 | | // Range |
623 | | check_mut(&vec[i..j], &mut map[i..j], &mut slice[i..j]); |
624 | | } |
625 | | |
626 | | for j in i..10 { |
627 | | // RangeInclusive |
628 | | check_mut(&vec[i..=j], &mut map[i..=j], &mut slice[i..=j]); |
629 | | } |
630 | | } |
631 | | } |
632 | | |
633 | | #[test] |
634 | | fn slice_new() { |
635 | | let slice: &Slice<i32, i32> = Slice::new(); |
636 | | assert!(slice.is_empty()); |
637 | | assert_eq!(slice.len(), 0); |
638 | | } |
639 | | |
640 | | #[test] |
641 | | fn slice_new_mut() { |
642 | | let slice: &mut Slice<i32, i32> = Slice::new_mut(); |
643 | | assert!(slice.is_empty()); |
644 | | assert_eq!(slice.len(), 0); |
645 | | } |
646 | | |
647 | | #[test] |
648 | | fn slice_get_index_mut() { |
649 | | let mut map: IndexMap<i32, i32> = (0..10).map(|i| (i, i * i)).collect(); |
650 | | let slice: &mut Slice<i32, i32> = map.as_mut_slice(); |
651 | | |
652 | | { |
653 | | let (key, value) = slice.get_index_mut(0).unwrap(); |
654 | | assert_eq!(*key, 0); |
655 | | assert_eq!(*value, 0); |
656 | | |
657 | | *value = 11; |
658 | | } |
659 | | |
660 | | assert_eq!(slice[0], 11); |
661 | | |
662 | | { |
663 | | let result = slice.get_index_mut(11); |
664 | | assert!(result.is_none()); |
665 | | } |
666 | | } |
667 | | |
668 | | #[test] |
669 | | fn slice_split_first() { |
670 | | let slice: &mut Slice<i32, i32> = Slice::new_mut(); |
671 | | let result = slice.split_first(); |
672 | | assert!(result.is_none()); |
673 | | |
674 | | let mut map: IndexMap<i32, i32> = (0..10).map(|i| (i, i * i)).collect(); |
675 | | let slice: &mut Slice<i32, i32> = map.as_mut_slice(); |
676 | | |
677 | | { |
678 | | let (first, rest) = slice.split_first().unwrap(); |
679 | | assert_eq!(first, (&0, &0)); |
680 | | assert_eq!(rest.len(), 9); |
681 | | } |
682 | | assert_eq!(slice.len(), 10); |
683 | | } |
684 | | |
685 | | #[test] |
686 | | fn slice_split_first_mut() { |
687 | | let slice: &mut Slice<i32, i32> = Slice::new_mut(); |
688 | | let result = slice.split_first_mut(); |
689 | | assert!(result.is_none()); |
690 | | |
691 | | let mut map: IndexMap<i32, i32> = (0..10).map(|i| (i, i * i)).collect(); |
692 | | let slice: &mut Slice<i32, i32> = map.as_mut_slice(); |
693 | | |
694 | | { |
695 | | let (first, rest) = slice.split_first_mut().unwrap(); |
696 | | assert_eq!(first, (&0, &mut 0)); |
697 | | assert_eq!(rest.len(), 9); |
698 | | |
699 | | *first.1 = 11; |
700 | | } |
701 | | assert_eq!(slice.len(), 10); |
702 | | assert_eq!(slice[0], 11); |
703 | | } |
704 | | |
705 | | #[test] |
706 | | fn slice_split_last() { |
707 | | let slice: &mut Slice<i32, i32> = Slice::new_mut(); |
708 | | let result = slice.split_last(); |
709 | | assert!(result.is_none()); |
710 | | |
711 | | let mut map: IndexMap<i32, i32> = (0..10).map(|i| (i, i * i)).collect(); |
712 | | let slice: &mut Slice<i32, i32> = map.as_mut_slice(); |
713 | | |
714 | | { |
715 | | let (last, rest) = slice.split_last().unwrap(); |
716 | | assert_eq!(last, (&9, &81)); |
717 | | assert_eq!(rest.len(), 9); |
718 | | } |
719 | | assert_eq!(slice.len(), 10); |
720 | | } |
721 | | |
722 | | #[test] |
723 | | fn slice_split_last_mut() { |
724 | | let slice: &mut Slice<i32, i32> = Slice::new_mut(); |
725 | | let result = slice.split_last_mut(); |
726 | | assert!(result.is_none()); |
727 | | |
728 | | let mut map: IndexMap<i32, i32> = (0..10).map(|i| (i, i * i)).collect(); |
729 | | let slice: &mut Slice<i32, i32> = map.as_mut_slice(); |
730 | | |
731 | | { |
732 | | let (last, rest) = slice.split_last_mut().unwrap(); |
733 | | assert_eq!(last, (&9, &mut 81)); |
734 | | assert_eq!(rest.len(), 9); |
735 | | |
736 | | *last.1 = 100; |
737 | | } |
738 | | |
739 | | assert_eq!(slice.len(), 10); |
740 | | assert_eq!(slice[slice.len() - 1], 100); |
741 | | } |
742 | | |
743 | | #[test] |
744 | | fn slice_get_range() { |
745 | | let mut map: IndexMap<i32, i32> = (0..10).map(|i| (i, i * i)).collect(); |
746 | | let slice: &mut Slice<i32, i32> = map.as_mut_slice(); |
747 | | let subslice = slice.get_range(3..6).unwrap(); |
748 | | assert_eq!(subslice.len(), 3); |
749 | | assert_eq!(subslice, &[(3, 9), (4, 16), (5, 25)]); |
750 | | } |
751 | | } |