1use super::*;
2
3#[derive(Copy, Clone)]
38#[repr(transparent)]
39pub struct IndexSlice<I: Idx, T: ?Sized> {
40 _marker: PhantomData<fn(&I)>,
41 pub raw: T,
42}
43
44unsafe impl<I: Idx, T> Send for IndexSlice<I, [T]> where T: Send {}
47
48impl<I: Idx, T: fmt::Debug + ?Sized> fmt::Debug for IndexSlice<I, T> {
49 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
50 fmt::Debug::fmt(&self.raw, fmt)
51 }
52}
53pub type IndexBox<I, T> = Box<IndexSlice<I, T>>;
55
56type SliceMapped<Iter, I, T> = iter::Map<Iter, fn(&[T]) -> &IndexSlice<I, [T]>>;
57type SliceMappedMut<Iter, I, T> = iter::Map<Iter, fn(&mut [T]) -> &mut IndexSlice<I, [T]>>;
58
59impl<I: Idx, T> IndexSlice<I, [T]> {
60 #[inline(always)]
62 pub fn new<S: AsRef<[T]> + ?Sized>(s: &S) -> &Self {
63 Self::from_slice(s.as_ref())
64 }
65
66 #[inline(always)]
68 pub fn new_mut<S: AsMut<[T]> + ?Sized>(s: &mut S) -> &mut Self {
69 Self::from_slice_mut(s.as_mut())
70 }
71
72 #[inline(always)]
74 pub const fn from_slice(s: &[T]) -> &Self {
75 unsafe { &*(s as *const [T] as *const Self) }
76 }
77
78 #[inline(always)]
80 pub fn from_slice_mut(s: &mut [T]) -> &mut Self {
81 unsafe { &mut *(s as *mut [T] as *mut Self) }
82 }
83
84 #[inline]
86 pub fn to_vec(&self) -> IndexVec<I, T>
87 where
88 T: Clone,
89 {
90 IndexVec::from_vec(self.raw.to_vec())
91 }
92
93 #[inline]
98 #[allow(clippy::wrong_self_convention)]
99 pub fn into_vec(self: Box<Self>) -> IndexVec<I, T> {
100 unsafe {
101 let len = self.len();
102 let b = Box::into_raw(self);
103 let xs = Vec::from_raw_parts(b as *mut T, len, len);
104 IndexVec::from_vec(xs)
105 }
106 }
107
108 #[inline(always)]
110 pub fn as_raw_slice_mut(&mut self) -> &mut [T] {
111 &mut self.raw
112 }
113
114 #[inline(always)]
116 pub const fn as_raw_slice(&self) -> &[T] {
117 &self.raw
118 }
119
120 #[inline]
122 pub fn as_mut_ptr(&mut self) -> *mut T {
123 self.raw.as_mut_ptr()
124 }
125
126 #[inline]
128 pub const fn as_ptr(&self) -> *const T {
129 self.raw.as_ptr()
130 }
131
132 #[inline]
134 pub fn last_idx(&self) -> I {
135 assert!(!self.is_empty());
138 I::from_usize(self.len() - 1)
139 }
140
141 #[inline]
143 pub const fn len(&self) -> usize {
144 self.raw.len()
145 }
146
147 #[inline]
149 pub fn len_idx(&self) -> I {
150 I::from_usize(self.raw.len())
151 }
152
153 #[inline]
155 pub const fn is_empty(&self) -> bool {
156 self.raw.is_empty()
157 }
158
159 #[inline]
164 pub fn iter(&self) -> slice::Iter<'_, T> {
165 self.raw.iter()
166 }
167
168 #[inline]
173 pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
174 self.raw.iter_mut()
175 }
176
177 #[inline(always)]
180 pub fn iter_enumerated(&self) -> Enumerated<slice::Iter<'_, T>, I, &T> {
181 self.raw
182 .iter()
183 .enumerate()
184 .map(|(i, t)| (I::from_usize(i), t))
185 }
186
187 #[inline(always)]
189 pub fn indices(&self) -> iter::Map<Range<usize>, fn(usize) -> I> {
190 (0..self.raw.len()).map(I::from_usize)
191 }
192
193 #[inline(always)]
196 pub fn iter_mut_enumerated(&mut self) -> Enumerated<slice::IterMut<'_, T>, I, &mut T> {
197 self.raw
198 .iter_mut()
199 .enumerate()
200 .map(|(i, t)| (I::from_usize(i), t))
201 }
202
203 #[inline]
205 pub fn sort(&mut self)
206 where
207 T: Ord,
208 {
209 self.raw.sort()
210 }
211
212 #[inline]
214 pub fn sort_by<F: FnMut(&T, &T) -> core::cmp::Ordering>(&mut self, compare: F) {
215 self.raw.sort_by(compare)
216 }
217
218 #[inline]
220 pub fn sort_by_key<F: FnMut(&T) -> K, K: Ord>(&mut self, f: F) {
221 self.raw.sort_by_key(f)
222 }
223
224 #[inline]
226 pub fn sort_by_cached_key<F: FnMut(&T) -> K, K: Ord>(&mut self, f: F) {
227 self.raw.sort_by_cached_key(f)
228 }
229
230 #[inline]
232 pub fn sort_unstable(&mut self)
233 where
234 T: Ord,
235 {
236 self.raw.sort_unstable()
237 }
238
239 #[inline]
241 pub fn sort_unstable_by<F: FnMut(&T, &T) -> core::cmp::Ordering>(&mut self, compare: F) {
242 self.raw.sort_unstable_by(compare)
243 }
244
245 #[inline]
247 pub fn sort_unstable_by_key<F: FnMut(&T) -> K, K: Ord>(&mut self, f: F) {
248 self.raw.sort_unstable_by_key(f)
249 }
250
251 #[inline]
253 pub fn ends_with<S: AsRef<[T]> + ?Sized>(&self, needle: &S) -> bool
254 where
255 T: PartialEq,
256 {
257 self.raw.ends_with(needle.as_ref())
258 }
259
260 #[inline]
262 pub fn starts_with<S: AsRef<[T]> + ?Sized>(&self, needle: &S) -> bool
263 where
264 T: PartialEq,
265 {
266 self.raw.starts_with(needle.as_ref())
267 }
268
269 #[inline]
271 pub fn contains(&self, x: &T) -> bool
272 where
273 T: PartialEq,
274 {
275 self.raw.contains(x)
276 }
277
278 #[inline]
280 pub fn reverse(&mut self) {
281 self.raw.reverse()
282 }
283
284 #[inline]
287 pub fn binary_search(&self, value: &T) -> Result<I, I>
288 where
289 T: Ord,
290 {
291 match self.raw.binary_search(value) {
292 Ok(i) => Ok(I::from_usize(i)),
293 Err(i) => Err(I::from_usize(i)),
294 }
295 }
296
297 #[inline]
300 pub fn binary_search_by<'a, F: FnMut(&'a T) -> core::cmp::Ordering>(
301 &'a self,
302 f: F,
303 ) -> Result<I, I> {
304 match self.raw.binary_search_by(f) {
305 Ok(i) => Ok(I::from_usize(i)),
306 Err(i) => Err(I::from_usize(i)),
307 }
308 }
309
310 #[inline]
312 pub fn copy_from_slice(&mut self, src: &Self)
313 where
314 T: Copy,
315 {
316 self.raw.copy_from_slice(&src.raw)
317 }
318
319 #[inline]
321 pub fn clone_from_slice(&mut self, src: &Self)
322 where
323 T: Clone,
324 {
325 self.raw.clone_from_slice(&src.raw)
326 }
327
328 #[inline]
330 pub fn swap_with_slice(&mut self, other: &mut Self) {
331 self.raw.swap_with_slice(&mut other.raw)
332 }
333
334 #[inline]
337 pub fn binary_search_by_key<'a, B: Ord, F: FnMut(&'a T) -> B>(
338 &'a self,
339 b: &B,
340 f: F,
341 ) -> Result<I, I> {
342 match self.raw.binary_search_by_key(b, f) {
343 Ok(i) => Ok(I::from_usize(i)),
344 Err(i) => Err(I::from_usize(i)),
345 }
346 }
347 #[inline(always)]
350 pub fn position<F: FnMut(&T) -> bool>(&self, f: F) -> Option<I> {
351 self.raw.iter().position(f).map(I::from_usize)
352 }
353
354 #[inline(always)]
358 pub fn rposition<F: FnMut(&T) -> bool>(&self, f: F) -> Option<I> {
359 self.raw.iter().rposition(f).map(I::from_usize)
360 }
361
362 #[inline]
364 pub fn swap(&mut self, a: I, b: I) {
365 self.raw.swap(a.index(), b.index())
366 }
367
368 #[inline]
370 pub fn split_at(&self, a: I) -> (&Self, &Self) {
371 let (a, b) = self.raw.split_at(a.index());
372 (Self::from_slice(a), Self::from_slice(b))
373 }
374
375 #[inline]
377 pub fn split_at_mut(&mut self, a: I) -> (&mut Self, &mut Self) {
378 let (a, b) = self.raw.split_at_mut(a.index());
379 (Self::new_mut(a), Self::new_mut(b))
380 }
381
382 #[inline]
386 pub fn rotate_left(&mut self, mid: I) {
387 self.raw.rotate_left(mid.index())
388 }
389
390 #[inline]
394 pub fn rotate_right(&mut self, k: I) {
395 self.raw.rotate_right(k.index())
396 }
397
398 #[inline(always)]
400 pub const fn last(&self) -> Option<&T> {
401 self.raw.last()
402 }
403
404 #[inline]
406 pub fn last_mut(&mut self) -> Option<&mut T> {
407 let i = self.len().checked_sub(1)?;
408 self.get_mut(I::from_usize(i))
409 }
410
411 #[inline]
413 pub const fn first(&self) -> Option<&T> {
414 self.raw.first()
415 }
416
417 #[inline]
419 pub fn first_mut(&mut self) -> Option<&mut T> {
420 self.get_mut(I::from_usize(0))
421 }
422
423 #[inline]
426 pub fn copy_within<R: IdxRangeBounds<I>>(&mut self, src: R, dst: I)
427 where
428 T: Copy,
429 {
430 self.raw.copy_within(src.into_range(), dst.index())
431 }
432
433 #[inline]
435 pub fn get<J: IdxSliceIndex<I, T>>(&self, index: J) -> Option<&J::Output> {
436 index.get(self)
437 }
438
439 #[inline]
442 pub fn get_mut<J: IdxSliceIndex<I, T>>(&mut self, index: J) -> Option<&mut J::Output> {
443 index.get_mut(self)
444 }
445
446 #[inline]
449 pub fn windows(&self, size: usize) -> SliceMapped<slice::Windows<'_, T>, I, T> {
450 self.raw.windows(size).map(IndexSlice::new)
451 }
452
453 #[inline]
456 pub fn chunks(&self, size: usize) -> SliceMapped<slice::Chunks<'_, T>, I, T> {
457 self.raw.chunks(size).map(IndexSlice::new)
458 }
459
460 #[inline]
463 pub fn chunks_mut(&mut self, size: usize) -> SliceMappedMut<slice::ChunksMut<'_, T>, I, T> {
464 self.raw.chunks_mut(size).map(IndexSlice::new_mut)
465 }
466
467 #[inline]
470 pub fn chunks_exact(&self, chunk_size: usize) -> SliceMapped<slice::ChunksExact<'_, T>, I, T> {
471 self.raw.chunks_exact(chunk_size).map(IndexSlice::new)
472 }
473
474 #[inline]
477 pub fn chunks_exact_mut(
478 &mut self,
479 chunk_size: usize,
480 ) -> SliceMappedMut<slice::ChunksExactMut<'_, T>, I, T> {
481 self.raw
482 .chunks_exact_mut(chunk_size)
483 .map(IndexSlice::new_mut)
484 }
485
486 #[inline]
489 pub fn rchunks(&self, size: usize) -> SliceMapped<slice::RChunks<'_, T>, I, T> {
490 self.raw.rchunks(size).map(IndexSlice::new)
491 }
492
493 #[inline]
496 pub fn rchunks_mut(&mut self, size: usize) -> SliceMappedMut<slice::RChunksMut<'_, T>, I, T> {
497 self.raw.rchunks_mut(size).map(IndexSlice::new_mut)
498 }
499
500 #[inline]
503 pub fn rchunks_exact(
504 &self,
505 chunk_size: usize,
506 ) -> SliceMapped<slice::RChunksExact<'_, T>, I, T> {
507 self.raw.rchunks_exact(chunk_size).map(IndexSlice::new)
508 }
509
510 #[inline]
513 pub fn rchunks_exact_mut(
514 &mut self,
515 chunk_size: usize,
516 ) -> SliceMappedMut<slice::RChunksExactMut<'_, T>, I, T> {
517 self.raw
518 .rchunks_exact_mut(chunk_size)
519 .map(IndexSlice::new_mut)
520 }
521
522 #[inline]
525 pub fn split<F: FnMut(&T) -> bool>(&self, f: F) -> SliceMapped<slice::Split<'_, T, F>, I, T> {
526 self.raw.split(f).map(IndexSlice::new)
527 }
528
529 #[inline]
532 pub fn split_mut<F: FnMut(&T) -> bool>(
533 &mut self,
534 f: F,
535 ) -> SliceMappedMut<slice::SplitMut<'_, T, F>, I, T> {
536 self.raw.split_mut(f).map(IndexSlice::new_mut)
537 }
538
539 #[inline]
542 pub fn rsplit<F: FnMut(&T) -> bool>(&self, f: F) -> SliceMapped<slice::RSplit<'_, T, F>, I, T> {
543 self.raw.rsplit(f).map(IndexSlice::new)
544 }
545
546 #[inline]
549 pub fn rsplit_mut<F: FnMut(&T) -> bool>(
550 &mut self,
551 f: F,
552 ) -> SliceMappedMut<slice::RSplitMut<'_, T, F>, I, T> {
553 self.raw.rsplit_mut(f).map(IndexSlice::new_mut)
554 }
555
556 #[inline]
559 pub fn splitn<F: FnMut(&T) -> bool>(
560 &self,
561 n: usize,
562 f: F,
563 ) -> SliceMapped<slice::SplitN<'_, T, F>, I, T> {
564 self.raw.splitn(n, f).map(IndexSlice::new)
565 }
566 #[inline]
569 pub fn splitn_mut<F: FnMut(&T) -> bool>(
570 &mut self,
571 n: usize,
572 f: F,
573 ) -> SliceMappedMut<slice::SplitNMut<'_, T, F>, I, T> {
574 self.raw.splitn_mut(n, f).map(IndexSlice::new_mut)
575 }
576
577 #[inline]
580 pub fn rsplitn<F: FnMut(&T) -> bool>(
581 &self,
582 n: usize,
583 f: F,
584 ) -> SliceMapped<slice::RSplitN<'_, T, F>, I, T> {
585 self.raw.rsplitn(n, f).map(IndexSlice::new)
586 }
587
588 #[inline]
591 pub fn rsplitn_mut<F: FnMut(&T) -> bool>(
592 &mut self,
593 n: usize,
594 f: F,
595 ) -> SliceMappedMut<slice::RSplitNMut<'_, T, F>, I, T> {
596 self.raw.rsplitn_mut(n, f).map(IndexSlice::new_mut)
597 }
598
599 #[inline]
606 pub unsafe fn from_raw_parts<'a>(data: *const T, len: usize) -> &'a Self {
607 Self::new(slice::from_raw_parts(data, len))
608 }
609
610 #[inline]
617 pub unsafe fn from_raw_parts_mut<'a>(data: *mut T, len: usize) -> &'a mut Self {
618 Self::new_mut(slice::from_raw_parts_mut(data, len))
619 }
620
621 #[inline]
623 pub const fn split_first(&self) -> Option<(&T, &IndexSlice<I, [T]>)> {
624 if let Some((first, rem)) = self.raw.split_first() {
625 Some((first, Self::from_slice(rem)))
626 } else {
627 None
628 }
629 }
630
631 #[inline]
633 pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut IndexSlice<I, [T]>)> {
634 if self.is_empty() {
635 None
636 } else {
637 let split = self.split_at_mut(I::from_usize(1));
638 Some((&mut split.0[I::from_usize(0)], split.1))
639 }
640 }
641
642 #[inline]
644 pub const fn split_last(&self) -> Option<(&T, &IndexSlice<I, [T]>)> {
645 if let Some((last, rem)) = self.raw.split_last() {
646 Some((last, Self::from_slice(rem)))
647 } else {
648 None
649 }
650 }
651
652 #[inline]
654 pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut IndexSlice<I, [T]>)> {
655 if self.is_empty() {
656 None
657 } else {
658 let last = self.last_idx();
659 let split = self.split_at_mut(last);
660 Some((&mut split.1[0], split.0))
661 }
662 }
663}
664
665impl<I: Idx, A, B> PartialEq<IndexSlice<I, [B]>> for IndexSlice<I, [A]>
666where
667 A: PartialEq<B>,
668{
669 #[inline]
670 fn eq(&self, other: &IndexSlice<I, [B]>) -> bool {
671 PartialEq::eq(&self.raw, &other.raw)
672 }
673 #[inline]
674 fn ne(&self, other: &IndexSlice<I, [B]>) -> bool {
675 PartialEq::ne(&self.raw, &other.raw)
676 }
677}
678
679impl<I: Idx, A: Eq> Eq for IndexSlice<I, [A]> {}
680
681impl<I: Idx, A, B> PartialEq<[B]> for IndexSlice<I, [A]>
682where
683 A: PartialEq<B>,
684{
685 #[inline]
686 fn eq(&self, other: &[B]) -> bool {
687 PartialEq::eq(&self.raw, other)
688 }
689 #[inline]
690 fn ne(&self, other: &[B]) -> bool {
691 PartialEq::ne(&self.raw, other)
692 }
693}
694
695impl<I: Idx, T: PartialOrd> PartialOrd for IndexSlice<I, [T]> {
696 #[inline]
697 fn partial_cmp(&self, other: &IndexSlice<I, [T]>) -> Option<core::cmp::Ordering> {
698 PartialOrd::partial_cmp(&self.raw, &other.raw)
699 }
700}
701
702impl<I: Idx, T: core::cmp::Ord> core::cmp::Ord for IndexSlice<I, [T]> {
703 #[inline]
704 fn cmp(&self, other: &IndexSlice<I, [T]>) -> core::cmp::Ordering {
705 core::cmp::Ord::cmp(&self.raw, &other.raw)
706 }
707}
708
709impl<I: Idx, T: core::hash::Hash> core::hash::Hash for IndexSlice<I, [T]> {
710 #[inline]
711 fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
712 self.raw.hash(h)
713 }
714}
715
716impl<I: Idx, T> alloc::borrow::ToOwned for IndexSlice<I, [T]>
717where
718 T: Clone,
719{
720 type Owned = IndexVec<I, T>;
721 #[inline]
722 fn to_owned(&self) -> Self::Owned {
723 IndexVec::from(self.raw.to_vec())
724 }
725}
726
727impl<'a, I: Idx, T> IntoIterator for &'a IndexSlice<I, [T]> {
728 type Item = &'a T;
729 type IntoIter = slice::Iter<'a, T>;
730
731 #[inline]
732 fn into_iter(self) -> slice::Iter<'a, T> {
733 self.raw.iter()
734 }
735}
736
737impl<'a, I: Idx, T> IntoIterator for &'a mut IndexSlice<I, [T]> {
738 type Item = &'a mut T;
739 type IntoIter = slice::IterMut<'a, T>;
740
741 #[inline]
742 fn into_iter(self) -> slice::IterMut<'a, T> {
743 self.raw.iter_mut()
744 }
745}
746
747impl<I: Idx, T> Default for &IndexSlice<I, [T]> {
748 #[inline]
749 fn default() -> Self {
750 IndexSlice::new(&[])
751 }
752}
753
754impl<I: Idx, T> Default for &mut IndexSlice<I, [T]> {
755 #[inline]
756 fn default() -> Self {
757 IndexSlice::new_mut(&mut [])
758 }
759}
760
761impl<'a, I: Idx, T> From<&'a [T]> for &'a IndexSlice<I, [T]> {
762 #[inline]
763 fn from(a: &'a [T]) -> Self {
764 IndexSlice::new(a)
765 }
766}
767
768impl<'a, I: Idx, T> From<&'a mut [T]> for &'a mut IndexSlice<I, [T]> {
769 #[inline]
770 fn from(a: &'a mut [T]) -> Self {
771 IndexSlice::new_mut(a)
772 }
773}
774
775impl<I: Idx, T> From<Box<[T]>> for Box<IndexSlice<I, [T]>> {
776 #[inline]
777 fn from(b: Box<[T]>) -> Self {
778 unsafe { Box::from_raw(Box::into_raw(b) as *mut IndexSlice<I, [T]>) }
779 }
780}
781
782impl<I: Idx, A> AsRef<[A]> for IndexSlice<I, [A]> {
783 #[inline]
784 fn as_ref(&self) -> &[A] {
785 &self.raw
786 }
787}
788
789impl<I: Idx, A> AsMut<[A]> for IndexSlice<I, [A]> {
790 #[inline]
791 fn as_mut(&mut self) -> &mut [A] {
792 &mut self.raw
793 }
794}
795
796impl<I: Idx, T: Clone> Clone for Box<IndexSlice<I, [T]>> {
797 #[inline]
798 fn clone(&self) -> Self {
799 self.to_vec().into_boxed_slice()
801 }
802}
803
804impl<I: Idx, A> FromIterator<A> for Box<IndexSlice<I, [A]>> {
805 #[inline]
806 fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
807 iter.into_iter()
808 .collect::<IndexVec<I, _>>()
809 .into_boxed_slice()
810 }
811}
812
813impl<I: Idx, A> IntoIterator for Box<IndexSlice<I, [A]>> {
814 type Item = A;
815 type IntoIter = vec::IntoIter<A>;
816 #[inline]
817 fn into_iter(self) -> Self::IntoIter {
818 let v: IndexVec<I, A> = self.into();
819 v.into_iter()
820 }
821}
822
823impl<I: Idx, A> Default for Box<IndexSlice<I, [A]>> {
824 #[inline(always)]
825 fn default() -> Self {
826 index_vec![].into()
827 }
828}