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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
use super::{
Bucket, Entries, IndexMap, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys, Values,
ValuesMut,
};
use crate::util::try_simplify_range;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{self, Bound, Index, IndexMut, RangeBounds};
/// A dynamically-sized slice of key-value pairs in an [`IndexMap`].
///
/// This supports indexed operations much like a `[(K, V)]` slice,
/// but not any hashed operations on the map keys.
///
/// Unlike `IndexMap`, `Slice` does consider the order for [`PartialEq`]
/// and [`Eq`], and it also implements [`PartialOrd`], [`Ord`], and [`Hash`].
#[repr(transparent)]
pub struct Slice<K, V> {
pub(crate) entries: [Bucket<K, V>],
}
// SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
// and reference lifetimes are bound together in function signatures.
#[allow(unsafe_code)]
impl<K, V> Slice<K, V> {
pub(super) const fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
unsafe { &*(entries as *const [Bucket<K, V>] as *const Self) }
}
pub(super) fn from_mut_slice(entries: &mut [Bucket<K, V>]) -> &mut Self {
unsafe { &mut *(entries as *mut [Bucket<K, V>] as *mut Self) }
}
pub(super) fn from_boxed(entries: Box<[Bucket<K, V>]>) -> Box<Self> {
unsafe { Box::from_raw(Box::into_raw(entries) as *mut Self) }
}
fn into_boxed(self: Box<Self>) -> Box<[Bucket<K, V>]> {
unsafe { Box::from_raw(Box::into_raw(self) as *mut [Bucket<K, V>]) }
}
}
impl<K, V> Slice<K, V> {
pub(crate) fn into_entries(self: Box<Self>) -> Vec<Bucket<K, V>> {
self.into_boxed().into_vec()
}
/// Returns an empty slice.
pub const fn new<'a>() -> &'a Self {
Self::from_slice(&[])
}
/// Returns an empty mutable slice.
pub fn new_mut<'a>() -> &'a mut Self {
Self::from_mut_slice(&mut [])
}
/// Return the number of key-value pairs in the map slice.
#[inline]
pub const fn len(&self) -> usize {
self.entries.len()
}
/// Returns true if the map slice contains no elements.
#[inline]
pub const fn is_empty(&self) -> bool {
self.entries.is_empty()
}
/// Get a key-value pair by index.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
self.entries.get(index).map(Bucket::refs)
}
/// Get a key-value pair by index, with mutable access to the value.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> {
self.entries.get_mut(index).map(Bucket::ref_mut)
}
/// Returns a slice of key-value pairs in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {
let range = try_simplify_range(range, self.entries.len())?;
self.entries.get(range).map(Slice::from_slice)
}
/// Returns a mutable slice of key-value pairs in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Self> {
let range = try_simplify_range(range, self.entries.len())?;
self.entries.get_mut(range).map(Slice::from_mut_slice)
}
/// Get the first key-value pair.
pub fn first(&self) -> Option<(&K, &V)> {
self.entries.first().map(Bucket::refs)
}
/// Get the first key-value pair, with mutable access to the value.
pub fn first_mut(&mut self) -> Option<(&K, &mut V)> {
self.entries.first_mut().map(Bucket::ref_mut)
}
/// Get the last key-value pair.
pub fn last(&self) -> Option<(&K, &V)> {
self.entries.last().map(Bucket::refs)
}
/// Get the last key-value pair, with mutable access to the value.
pub fn last_mut(&mut self) -> Option<(&K, &mut V)> {
self.entries.last_mut().map(Bucket::ref_mut)
}
/// Divides one slice into two at an index.
///
/// ***Panics*** if `index > len`.
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
let (first, second) = self.entries.split_at(index);
(Self::from_slice(first), Self::from_slice(second))
}
/// Divides one mutable slice into two at an index.
///
/// ***Panics*** if `index > len`.
pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
let (first, second) = self.entries.split_at_mut(index);
(Self::from_mut_slice(first), Self::from_mut_slice(second))
}
/// Returns the first key-value pair and the rest of the slice,
/// or `None` if it is empty.
pub fn split_first(&self) -> Option<((&K, &V), &Self)> {
if let [first, rest @ ..] = &self.entries {
Some((first.refs(), Self::from_slice(rest)))
} else {
None
}
}
/// Returns the first key-value pair and the rest of the slice,
/// with mutable access to the value, or `None` if it is empty.
pub fn split_first_mut(&mut self) -> Option<((&K, &mut V), &mut Self)> {
if let [first, rest @ ..] = &mut self.entries {
Some((first.ref_mut(), Self::from_mut_slice(rest)))
} else {
None
}
}
/// Returns the last key-value pair and the rest of the slice,
/// or `None` if it is empty.
pub fn split_last(&self) -> Option<((&K, &V), &Self)> {
if let [rest @ .., last] = &self.entries {
Some((last.refs(), Self::from_slice(rest)))
} else {
None
}
}
/// Returns the last key-value pair and the rest of the slice,
/// with mutable access to the value, or `None` if it is empty.
pub fn split_last_mut(&mut self) -> Option<((&K, &mut V), &mut Self)> {
if let [rest @ .., last] = &mut self.entries {
Some((last.ref_mut(), Self::from_mut_slice(rest)))
} else {
None
}
}
/// Return an iterator over the key-value pairs of the map slice.
pub fn iter(&self) -> Iter<'_, K, V> {
Iter::new(&self.entries)
}
/// Return an iterator over the key-value pairs of the map slice.
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut::new(&mut self.entries)
}
/// Return an iterator over the keys of the map slice.
pub fn keys(&self) -> Keys<'_, K, V> {
Keys::new(&self.entries)
}
/// Return an owning iterator over the keys of the map slice.
pub fn into_keys(self: Box<Self>) -> IntoKeys<K, V> {
IntoKeys::new(self.into_entries())
}
/// Return an iterator over the values of the map slice.
pub fn values(&self) -> Values<'_, K, V> {
Values::new(&self.entries)
}
/// Return an iterator over mutable references to the the values of the map slice.
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut::new(&mut self.entries)
}
/// Return an owning iterator over the values of the map slice.
pub fn into_values(self: Box<Self>) -> IntoValues<K, V> {
IntoValues::new(self.into_entries())
}
/// Search over a sorted map for a key.
///
/// Returns the position where that key is present, or the position where it can be inserted to
/// maintain the sort. See [`slice::binary_search`] for more details.
///
/// Computes in **O(log(n))** time, which is notably less scalable than looking the key up in
/// the map this is a slice from using [`IndexMap::get_index_of`], but this can also position
/// missing keys.
pub fn binary_search_keys(&self, x: &K) -> Result<usize, usize>
where
K: Ord,
{
self.binary_search_by(|p, _| p.cmp(x))
}
/// Search over a sorted map with a comparator function.
///
/// Returns the position where that value is present, or the position where it can be inserted
/// to maintain the sort. See [`slice::binary_search_by`] for more details.
///
/// Computes in **O(log(n))** time.
#[inline]
pub fn binary_search_by<'a, F>(&'a self, mut f: F) -> Result<usize, usize>
where
F: FnMut(&'a K, &'a V) -> Ordering,
{
self.entries.binary_search_by(move |a| f(&a.key, &a.value))
}
/// Search over a sorted map with an extraction function.
///
/// Returns the position where that value is present, or the position where it can be inserted
/// to maintain the sort. See [`slice::binary_search_by_key`] for more details.
///
/// Computes in **O(log(n))** time.
#[inline]
pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, mut f: F) -> Result<usize, usize>
where
F: FnMut(&'a K, &'a V) -> B,
B: Ord,
{
self.binary_search_by(|k, v| f(k, v).cmp(b))
}
/// Returns the index of the partition point of a sorted map according to the given predicate
/// (the index of the first element of the second partition).
///
/// See [`slice::partition_point`] for more details.
///
/// Computes in **O(log(n))** time.
#[must_use]
pub fn partition_point<P>(&self, mut pred: P) -> usize
where
P: FnMut(&K, &V) -> bool,
{
self.entries
.partition_point(move |a| pred(&a.key, &a.value))
}
}
impl<'a, K, V> IntoIterator for &'a Slice<K, V> {
type IntoIter = Iter<'a, K, V>;
type Item = (&'a K, &'a V);
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut Slice<K, V> {
type IntoIter = IterMut<'a, K, V>;
type Item = (&'a K, &'a mut V);
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<K, V> IntoIterator for Box<Slice<K, V>> {
type IntoIter = IntoIter<K, V>;
type Item = (K, V);
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self.into_entries())
}
}
impl<K, V> Default for &'_ Slice<K, V> {
fn default() -> Self {
Slice::from_slice(&[])
}
}
impl<K, V> Default for &'_ mut Slice<K, V> {
fn default() -> Self {
Slice::from_mut_slice(&mut [])
}
}
impl<K, V> Default for Box<Slice<K, V>> {
fn default() -> Self {
Slice::from_boxed(Box::default())
}
}
impl<K: Clone, V: Clone> Clone for Box<Slice<K, V>> {
fn clone(&self) -> Self {
Slice::from_boxed(self.entries.to_vec().into_boxed_slice())
}
}
impl<K: Copy, V: Copy> From<&Slice<K, V>> for Box<Slice<K, V>> {
fn from(slice: &Slice<K, V>) -> Self {
Slice::from_boxed(Box::from(&slice.entries))
}
}
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Slice<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self).finish()
}
}
impl<K: PartialEq, V: PartialEq> PartialEq for Slice<K, V> {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.iter().eq(other)
}
}
impl<K: Eq, V: Eq> Eq for Slice<K, V> {}
impl<K: PartialOrd, V: PartialOrd> PartialOrd for Slice<K, V> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.iter().partial_cmp(other)
}
}
impl<K: Ord, V: Ord> Ord for Slice<K, V> {
fn cmp(&self, other: &Self) -> Ordering {
self.iter().cmp(other)
}
}
impl<K: Hash, V: Hash> Hash for Slice<K, V> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.len().hash(state);
for (key, value) in self {
key.hash(state);
value.hash(state);
}
}
}
impl<K, V> Index<usize> for Slice<K, V> {
type Output = V;
fn index(&self, index: usize) -> &V {
&self.entries[index].value
}
}
impl<K, V> IndexMut<usize> for Slice<K, V> {
fn index_mut(&mut self, index: usize) -> &mut V {
&mut self.entries[index].value
}
}
// We can't have `impl<I: RangeBounds<usize>> Index<I>` because that conflicts
// both upstream with `Index<usize>` and downstream with `Index<&Q>`.
// Instead, we repeat the implementations for all the core range types.
macro_rules! impl_index {
($($range:ty),*) => {$(
impl<K, V, S> Index<$range> for IndexMap<K, V, S> {
type Output = Slice<K, V>;
fn index(&self, range: $range) -> &Self::Output {
Slice::from_slice(&self.as_entries()[range])
}
}
impl<K, V, S> IndexMut<$range> for IndexMap<K, V, S> {
fn index_mut(&mut self, range: $range) -> &mut Self::Output {
Slice::from_mut_slice(&mut self.as_entries_mut()[range])
}
}
impl<K, V> Index<$range> for Slice<K, V> {
type Output = Slice<K, V>;
fn index(&self, range: $range) -> &Self {
Self::from_slice(&self.entries[range])
}
}
impl<K, V> IndexMut<$range> for Slice<K, V> {
fn index_mut(&mut self, range: $range) -> &mut Self {
Self::from_mut_slice(&mut self.entries[range])
}
}
)*}
}
impl_index!(
ops::Range<usize>,
ops::RangeFrom<usize>,
ops::RangeFull,
ops::RangeInclusive<usize>,
ops::RangeTo<usize>,
ops::RangeToInclusive<usize>,
(Bound<usize>, Bound<usize>)
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slice_index() {
fn check(
vec_slice: &[(i32, i32)],
map_slice: &Slice<i32, i32>,
sub_slice: &Slice<i32, i32>,
) {
assert_eq!(map_slice as *const _, sub_slice as *const _);
itertools::assert_equal(
vec_slice.iter().copied(),
map_slice.iter().map(|(&k, &v)| (k, v)),
);
itertools::assert_equal(vec_slice.iter().map(|(k, _)| k), map_slice.keys());
itertools::assert_equal(vec_slice.iter().map(|(_, v)| v), map_slice.values());
}
let vec: Vec<(i32, i32)> = (0..10).map(|i| (i, i * i)).collect();
let map: IndexMap<i32, i32> = vec.iter().cloned().collect();
let slice = map.as_slice();
// RangeFull
check(&vec[..], &map[..], &slice[..]);
for i in 0usize..10 {
// Index
assert_eq!(vec[i].1, map[i]);
assert_eq!(vec[i].1, slice[i]);
assert_eq!(map[&(i as i32)], map[i]);
assert_eq!(map[&(i as i32)], slice[i]);
// RangeFrom
check(&vec[i..], &map[i..], &slice[i..]);
// RangeTo
check(&vec[..i], &map[..i], &slice[..i]);
// RangeToInclusive
check(&vec[..=i], &map[..=i], &slice[..=i]);
// (Bound<usize>, Bound<usize>)
let bounds = (Bound::Excluded(i), Bound::Unbounded);
check(&vec[i + 1..], &map[bounds], &slice[bounds]);
for j in i..=10 {
// Range
check(&vec[i..j], &map[i..j], &slice[i..j]);
}
for j in i..10 {
// RangeInclusive
check(&vec[i..=j], &map[i..=j], &slice[i..=j]);
}
}
}
#[test]
fn slice_index_mut() {
fn check_mut(
vec_slice: &[(i32, i32)],
map_slice: &mut Slice<i32, i32>,
sub_slice: &mut Slice<i32, i32>,
) {
assert_eq!(map_slice, sub_slice);
itertools::assert_equal(
vec_slice.iter().copied(),
map_slice.iter_mut().map(|(&k, &mut v)| (k, v)),
);
itertools::assert_equal(
vec_slice.iter().map(|&(_, v)| v),
map_slice.values_mut().map(|&mut v| v),
);
}
let vec: Vec<(i32, i32)> = (0..10).map(|i| (i, i * i)).collect();
let mut map: IndexMap<i32, i32> = vec.iter().cloned().collect();
let mut map2 = map.clone();
let slice = map2.as_mut_slice();
// RangeFull
check_mut(&vec[..], &mut map[..], &mut slice[..]);
for i in 0usize..10 {
// IndexMut
assert_eq!(&mut map[i], &mut slice[i]);
// RangeFrom
check_mut(&vec[i..], &mut map[i..], &mut slice[i..]);
// RangeTo
check_mut(&vec[..i], &mut map[..i], &mut slice[..i]);
// RangeToInclusive
check_mut(&vec[..=i], &mut map[..=i], &mut slice[..=i]);
// (Bound<usize>, Bound<usize>)
let bounds = (Bound::Excluded(i), Bound::Unbounded);
check_mut(&vec[i + 1..], &mut map[bounds], &mut slice[bounds]);
for j in i..=10 {
// Range
check_mut(&vec[i..j], &mut map[i..j], &mut slice[i..j]);
}
for j in i..10 {
// RangeInclusive
check_mut(&vec[i..=j], &mut map[i..=j], &mut slice[i..=j]);
}
}
}
}
|