mdarray 0.7.2

Multidimensional array for Rust
Documentation
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
#[cfg(feature = "nightly")]
use alloc::alloc::{Allocator, Global};
#[cfg(not(feature = "std"))]
use alloc::borrow::ToOwned;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use alloc::collections::TryReserveError;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use core::borrow::{Borrow, BorrowMut};
use core::fmt::{self, Debug, Formatter};
use core::hash::{Hash, Hasher};
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::ops::{Deref, DerefMut, Index, IndexMut, RangeBounds};
use core::{ptr, slice};

#[cfg(not(feature = "nightly"))]
use crate::allocator::{Allocator, Global};
use crate::array::Array;
use crate::dim::{Const, Dim, Dyn};
use crate::expr::{self, Drain, IntoExpr, Iter, Map, Zip};
use crate::expr::{Apply, Expression, FromExpression, IntoExpression};
use crate::index::SliceIndex;
use crate::layout::{Dense, Layout};
use crate::mapping::{DenseMapping, Mapping};
use crate::raw_tensor::RawTensor;
use crate::shape::{ConstShape, DynRank, IntoShape, Rank, Shape};
use crate::slice::Slice;
use crate::traits::{IntoCloned, Owned};
use crate::view::{View, ViewMut};

#[cfg(not(feature = "nightly"))]
macro_rules! vec_t {
    ($type:ty, $alloc:ty) => {
        Vec<$type>
    };
}

#[cfg(feature = "nightly")]
macro_rules! vec_t {
    ($type:ty, $alloc:ty) => {
        Vec<$type, $alloc>
    };
}

/// Dense multidimensional array.
pub struct Tensor<T, S: Shape = DynRank, A: Allocator = Global> {
    tensor: RawTensor<T, S, A>,
}

/// Multidimensional array with dynamically-sized dimensions and dense layout.
pub type DTensor<T, const N: usize, A = Global> = Tensor<T, Rank<N>, A>;

impl<T, S: Shape, A: Allocator> Tensor<T, S, A> {
    /// Returns a reference to the underlying allocator.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn allocator(&self) -> &A {
        self.tensor.allocator()
    }

    /// Moves all elements from another array into the array along the first dimension.
    ///
    /// If the array is empty, it is reshaped to match the shape of the other array.
    ///
    /// # Panics
    ///
    /// Panics if the inner dimensions do not match, if the rank is not the same and
    /// at least 1, or if the first dimension is not dynamically-sized.
    #[inline]
    pub fn append(&mut self, other: &mut Self) {
        self.expand(other.drain(..));
    }

    /// Returns the number of elements the array can hold without reallocating.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.tensor.capacity()
    }

    /// Clears the array, removing all values.
    ///
    /// If the array type has dynamic rank, the rank is set to 1.
    ///
    /// Note that this method has no effect on the allocated capacity of the array.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[inline]
    pub fn clear(&mut self) {
        assert!(S::default().len() == 0, "default length not zero");

        unsafe {
            self.tensor.with_mut_parts(|vec, mapping| {
                vec.clear();
                *mapping = DenseMapping::default();
            });
        }
    }

    /// Removes the specified range from the array along the first dimension,
    /// and returns the removed range as an expression.
    ///
    /// # Panics
    ///
    /// Panics if the rank is not at least 1, or if the first dimension
    /// is not dynamically-sized.
    #[inline]
    pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> IntoExpr<Drain<'_, T, S, A>> {
        assert!(self.rank() > 0, "invalid rank");
        assert!(S::Head::SIZE.is_none(), "first dimension not dynamically-sized");

        #[cfg(not(feature = "nightly"))]
        let range = crate::index::range(range, ..self.dim(0));
        #[cfg(feature = "nightly")]
        let range = slice::range(range, ..self.dim(0));

        IntoExpr::new(Drain::new(self, range.start, range.end))
    }

    /// Appends an expression to the array along the first dimension with broadcasting,
    /// cloning elements if needed.
    ///
    /// If the array is empty, it is reshaped to match the shape of the expression.
    ///
    /// # Panics
    ///
    /// Panics if the inner dimensions do not match, if the rank is not the same and
    /// at least 1, or if the first dimension is not dynamically-sized.
    #[inline]
    pub fn expand<I: IntoExpression<Item: IntoCloned<T>>>(&mut self, expr: I) {
        assert!(self.rank() > 0, "invalid rank");
        assert!(S::Head::SIZE.is_none(), "first dimension not dynamically-sized");

        let expr = expr.into_expr();
        let len = expr.len();

        if len > 0 {
            unsafe {
                self.tensor.with_mut_parts(|vec, mapping| {
                    vec.reserve(len);

                    expr.shape().with_dims(|src| {
                        if mapping.is_empty() {
                            if src.len() == mapping.rank() {
                                mapping.shape_mut().with_mut_dims(|dims| dims.copy_from_slice(src));
                            } else {
                                *mapping = DenseMapping::new(Shape::from_dims(src));
                            }
                        } else {
                            mapping.shape_mut().with_mut_dims(|dims| {
                                assert!(src.len() == dims.len(), "invalid rank");
                                assert!(src[1..] == dims[1..], "inner dimensions mismatch");

                                dims[0] += src[0];
                            });
                        }
                    });

                    expr.clone_into_vec(vec);
                });
            }
        }
    }

    /// Creates an array from the given element with the specified allocator.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn from_elem_in<I: IntoShape<IntoShape = S>>(shape: I, elem: T, alloc: A) -> Self
    where
        T: Clone,
    {
        Self::from_expr_in(expr::from_elem(shape, elem), alloc)
    }

    /// Creates an array with the results from the given function with the specified allocator.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn from_fn_in<I: IntoShape<IntoShape = S>, F>(shape: I, f: F, alloc: A) -> Self
    where
        F: FnMut(&[usize]) -> T,
    {
        Self::from_expr_in(expr::from_fn(shape, f), alloc)
    }

    /// Creates an array from raw components of another array with the specified allocator.
    ///
    /// # Safety
    ///
    /// The pointer must be a valid allocation given the mapping, capacity and allocator.
    #[cfg(feature = "nightly")]
    #[inline]
    pub unsafe fn from_raw_parts_in(
        ptr: *mut T,
        mapping: DenseMapping<S>,
        capacity: usize,
        alloc: A,
    ) -> Self {
        unsafe {
            Self::from_parts(Vec::from_raw_parts_in(ptr, mapping.len(), capacity, alloc), mapping)
        }
    }

    /// Converts the array into an array with dynamic rank.
    #[inline]
    pub fn into_dyn(self) -> Tensor<T, DynRank, A> {
        self.into_mapping()
    }

    /// Converts the array into a one-dimensional array.
    #[inline]
    pub fn into_flat(self) -> Tensor<T, (Dyn,), A> {
        self.into_vec().into()
    }

    /// Converts the array into a remapped array.
    ///
    /// # Panics
    ///
    /// Panics if the shape is not matching static rank or constant-sized dimensions.
    #[inline]
    pub fn into_mapping<R: Shape>(self) -> Tensor<T, R, A> {
        let (vec, mapping) = self.tensor.into_parts();

        unsafe { Tensor::from_parts(vec, Mapping::remap(&mapping)) }
    }

    /// Decomposes an array into its raw components including the allocator.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn into_raw_parts_with_alloc(self) -> (*mut T, DenseMapping<S>, usize, A) {
        let (vec, mapping) = self.tensor.into_parts();
        let (ptr, _, capacity, alloc) = vec.into_raw_parts_with_alloc();

        (ptr, mapping, capacity, alloc)
    }

    /// Converts an array with a single element into the contained value.
    ///
    /// # Panics
    ///
    /// Panics if the array length is not equal to one.
    #[inline]
    pub fn into_scalar(self) -> T {
        assert!(self.len() == 1, "invalid length");

        self.into_vec().pop().unwrap()
    }

    /// Converts the array into a reshaped array, which must have the same length.
    ///
    /// At most one dimension can have dynamic size `usize::MAX`, and is then inferred
    /// from the other dimensions and the array length.
    ///
    /// # Examples
    ///
    /// ```
    /// use mdarray::{tensor, view};
    ///
    /// let t = tensor![[1, 2, 3], [4, 5, 6]];
    ///
    /// assert_eq!(t.into_shape([!0, 2]), view![[1, 2], [3, 4], [5, 6]]);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the array length is changed.
    #[inline]
    pub fn into_shape<I: IntoShape>(self, shape: I) -> Tensor<T, I::IntoShape, A> {
        let (vec, mapping) = self.tensor.into_parts();

        unsafe { Tensor::from_parts(vec, mapping.reshape(shape.into_shape())) }
    }

    /// Converts the array into a vector.
    #[inline]
    pub fn into_vec(self) -> vec_t!(T, A) {
        let (vec, _) = self.tensor.into_parts();

        vec
    }

    /// Returns the array with the given closure applied to each element.
    #[inline]
    pub fn map<F: FnMut(T) -> T>(self, mut f: F) -> Self {
        self.zip_with(expr::fill(()), |(x, ())| f(x))
    }

    /// Creates a new, empty array with the specified allocator.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn new_in(alloc: A) -> Self {
        assert!(S::default().checked_len() == Some(0), "default length not zero");

        unsafe { Self::from_parts(Vec::new_in(alloc), DenseMapping::default()) }
    }

    /// Reserves capacity for at least the additional number of elements in the array.
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        unsafe {
            self.tensor.with_mut_parts(|vec, _| vec.reserve(additional));
        }
    }

    /// Reserves the minimum capacity for the additional number of elements in the array.
    #[inline]
    pub fn reserve_exact(&mut self, additional: usize) {
        unsafe {
            self.tensor.with_mut_parts(|vec, _| vec.reserve_exact(additional));
        }
    }

    /// Resizes the array to the new shape, creating new elements with the given value.
    #[inline]
    pub fn resize(&mut self, new_dims: &[usize], value: T)
    where
        T: Clone,
        A: Clone,
    {
        self.tensor.resize_with(new_dims, || value.clone());
    }

    /// Resizes the array to the new shape, creating new elements from the given closure.
    #[inline]
    pub fn resize_with<F: FnMut() -> T>(&mut self, new_dims: &[usize], f: F)
    where
        A: Clone,
    {
        self.tensor.resize_with(new_dims, f);
    }

    /// Forces the array layout mapping to the new mapping.
    ///
    /// # Safety
    ///
    /// All elements within the array length must be initialized.
    #[inline]
    pub unsafe fn set_mapping(&mut self, new_mapping: DenseMapping<S>) {
        unsafe {
            self.tensor.set_mapping(new_mapping);
        }
    }

    /// Shrinks the capacity of the array with a lower bound.
    #[inline]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        unsafe {
            self.tensor.with_mut_parts(|vec, _| vec.shrink_to(min_capacity));
        }
    }

    /// Shrinks the capacity of the array as much as possible.
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        unsafe {
            self.tensor.with_mut_parts(|vec, _| vec.shrink_to_fit());
        }
    }

    /// Returns the remaining spare capacity of the array as a slice of `MaybeUninit<T>`.
    ///
    /// The returned slice can be used to fill the array with data, before marking
    /// the data as initialized using the `set_shape` method.
    #[inline]
    pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
        let ptr = self.as_mut_ptr();
        let len = self.capacity() - self.len();

        unsafe { slice::from_raw_parts_mut(ptr.add(self.len()).cast(), len) }
    }

    /// Shortens the array along the first dimension, keeping the first `size` indices.
    ///
    /// If `size` is greater or equal to the current dimension size, this has no effect.
    ///
    /// Note that this method has no effect on the allocated capacity of the array.
    ///
    /// # Panics
    ///
    /// Panics if the rank is not at least 1, or if the first dimension
    /// is not dynamically-sized.
    #[inline]
    pub fn truncate(&mut self, size: usize) {
        assert!(self.rank() > 0, "invalid rank");
        assert!(S::Head::SIZE.is_none(), "first dimension not dynamically-sized");

        if size < self.dim(0) {
            unsafe {
                self.tensor.with_mut_parts(|vec, mapping| {
                    mapping.shape_mut().with_mut_dims(|dims| dims[0] = size);
                    vec.truncate(mapping.len());
                });
            }
        }
    }

    /// Tries to reserve capacity for at least the additional number of elements in the array.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an error is returned.
    #[inline]
    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
        unsafe { self.tensor.with_mut_parts(|vec, _| vec.try_reserve(additional)) }
    }

    /// Tries to reserve the minimum capacity for the additional number of elements in the array.
    ///
    /// # Errors
    ///
    /// If the capacity overflows, or the allocator reports a failure, then an error is returned.
    #[inline]
    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
        unsafe { self.tensor.with_mut_parts(|vec, _| vec.try_reserve_exact(additional)) }
    }

    /// Creates an array with uninitialized elements and the specified allocator.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn uninit_in<I: IntoShape<IntoShape = S>>(
        shape: I,
        alloc: A,
    ) -> Tensor<MaybeUninit<T>, S, A> {
        let shape = shape.into_shape();
        let len = shape.checked_len().expect("invalid length");

        let vec = Vec::from(Box::new_uninit_slice_in(len, alloc));

        unsafe { Tensor::from_parts(vec, DenseMapping::new(shape)) }
    }

    /// Creates a new, empty array with the specified capacity and allocator.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
        assert!(S::default().checked_len() == Some(0), "default length not zero");

        unsafe { Self::from_parts(Vec::with_capacity_in(capacity, alloc), DenseMapping::default()) }
    }

    /// Creates an array with elements set to zero.
    ///
    /// Zero elements are created using `Default::default()`.
    #[cfg(feature = "nightly")]
    #[inline]
    pub fn zeros_in<I: IntoShape<IntoShape = S>>(shape: I, alloc: A) -> Self
    where
        T: Default,
    {
        let mut tensor = Tensor::uninit_in(shape, alloc);

        tensor.expr_mut().for_each(|x| {
            _ = x.write(T::default());
        });

        unsafe { tensor.assume_init() }
    }

    #[cfg(not(feature = "nightly"))]
    #[inline]
    fn from_expr<E: Expression<Item = T, Shape = S>>(expr: E) -> Self {
        let shape = expr.shape().clone();
        let mut vec = Vec::with_capacity(shape.len());

        expr.clone_into_vec(&mut vec);

        unsafe { Self::from_parts(vec, DenseMapping::new(shape)) }
    }

    #[cfg(feature = "nightly")]
    #[inline]
    pub(crate) fn from_expr_in<E>(expr: E, alloc: A) -> Self
    where
        E: Expression<Item = T, Shape = S>,
    {
        let shape = expr.shape().clone();
        let mut vec = Vec::with_capacity_in(shape.len(), alloc);

        expr.clone_into_vec(&mut vec);

        unsafe { Self::from_parts(vec, DenseMapping::new(shape)) }
    }

    #[inline]
    pub(crate) unsafe fn from_parts(vec: vec_t!(T, A), mapping: DenseMapping<S>) -> Self {
        unsafe { Self { tensor: RawTensor::from_parts(vec, mapping) } }
    }

    #[inline]
    fn zip_with<I: IntoExpression, F>(self, expr: I, mut f: F) -> Self
    where
        F: FnMut((T, I::Item)) -> T,
    {
        struct DropGuard<T, S: Shape, A: Allocator> {
            tensor: ManuallyDrop<Tensor<T, S, A>>,
            index: usize,
        }

        impl<T, S: Shape, A: Allocator> Drop for DropGuard<T, S, A> {
            #[inline]
            fn drop(&mut self) {
                let ptr = self.tensor.as_mut_ptr();
                let tail = self.tensor.len() - self.index;

                // Drop all elements except the current one, which is read but not written back.
                unsafe {
                    let mut vec = ManuallyDrop::take(&mut self.tensor).into_vec();

                    vec.set_len(0);

                    if self.index > 1 {
                        ptr::slice_from_raw_parts_mut(ptr, self.index - 1).drop_in_place();
                    }

                    ptr::slice_from_raw_parts_mut(ptr.add(self.index), tail).drop_in_place();
                }
            }
        }

        let mut guard = DropGuard { tensor: ManuallyDrop::new(self), index: 0 };
        let expr = guard.tensor.expr_mut().zip(expr);

        expr.for_each(|(x, y)| unsafe {
            guard.index += 1;
            ptr::write(x, f((ptr::read(x), y)));
        });

        let tensor = unsafe { ManuallyDrop::take(&mut guard.tensor) };

        mem::forget(guard);

        tensor
    }
}

#[cfg(not(feature = "nightly"))]
impl<T, S: Shape> Tensor<T, S> {
    /// Creates an array from the given element.
    #[inline]
    pub fn from_elem<I: IntoShape<IntoShape = S>>(shape: I, elem: T) -> Self
    where
        T: Clone,
    {
        Self::from_expr(expr::from_elem(shape, elem))
    }

    /// Creates an array with the results from the given function.
    #[inline]
    pub fn from_fn<I: IntoShape<IntoShape = S>, F>(shape: I, f: F) -> Self
    where
        F: FnMut(&[usize]) -> T,
    {
        Self::from_expr(expr::from_fn(shape, f))
    }

    /// Creates an array from raw components of another array.
    ///
    /// # Safety
    ///
    /// The pointer must be a valid allocation given the shape and capacity.
    #[inline]
    pub unsafe fn from_raw_parts(ptr: *mut T, mapping: DenseMapping<S>, capacity: usize) -> Self {
        unsafe { Self::from_parts(Vec::from_raw_parts(ptr, mapping.len(), capacity), mapping) }
    }

    /// Decomposes an array into its raw components.
    #[inline]
    pub fn into_raw_parts(self) -> (*mut T, DenseMapping<S>, usize) {
        let (vec, mapping) = self.tensor.into_parts();
        let mut vec = mem::ManuallyDrop::new(vec);

        (vec.as_mut_ptr(), mapping, vec.capacity())
    }

    /// Creates a new, empty array.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[inline]
    pub fn new() -> Self {
        assert!(S::default().checked_len() == Some(0), "default length not zero");

        unsafe { Self::from_parts(Vec::new(), DenseMapping::default()) }
    }

    /// Creates an array with uninitialized elements.
    #[inline]
    pub fn uninit<I: IntoShape<IntoShape = S>>(shape: I) -> Tensor<MaybeUninit<T>, S> {
        let shape = shape.into_shape();
        let len = shape.checked_len().expect("invalid length");

        let vec = Vec::from(Box::new_uninit_slice(len));

        unsafe { Tensor::from_parts(vec, DenseMapping::new(shape)) }
    }

    /// Creates a new, empty array with the specified capacity.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        assert!(S::default().checked_len() == Some(0), "default length not zero");

        unsafe { Self::from_parts(Vec::with_capacity(capacity), DenseMapping::default()) }
    }

    /// Creates an array with elements set to zero.
    ///
    /// Zero elements are created using `Default::default()`.
    #[inline]
    pub fn zeros<I: IntoShape<IntoShape = S>>(shape: I) -> Self
    where
        T: Default,
    {
        let mut tensor = Tensor::uninit(shape);

        tensor.expr_mut().for_each(|x| {
            _ = x.write(T::default());
        });

        unsafe { tensor.assume_init() }
    }
}

#[cfg(feature = "nightly")]
impl<T, S: Shape> Tensor<T, S> {
    /// Creates an array from the given element.
    #[inline]
    pub fn from_elem<I: IntoShape<IntoShape = S>>(shape: I, elem: T) -> Self
    where
        T: Clone,
    {
        Self::from_elem_in(shape, elem, Global)
    }

    /// Creates an array with the results from the given function.
    #[inline]
    pub fn from_fn<I: IntoShape<IntoShape = S>, F>(shape: I, f: F) -> Self
    where
        F: FnMut(&[usize]) -> T,
    {
        Self::from_fn_in(shape, f, Global)
    }

    /// Creates an array from raw components of another array.
    ///
    /// # Safety
    ///
    /// The pointer must be a valid allocation given the shape and capacity.
    #[inline]
    pub unsafe fn from_raw_parts(ptr: *mut T, mapping: DenseMapping<S>, capacity: usize) -> Self {
        unsafe { Self::from_raw_parts_in(ptr, mapping, capacity, Global) }
    }

    /// Decomposes an array into its raw components.
    #[inline]
    pub fn into_raw_parts(self) -> (*mut T, DenseMapping<S>, usize) {
        let (ptr, mapping, capacity, _) = self.into_raw_parts_with_alloc();

        (ptr, mapping, capacity)
    }

    /// Creates a new, empty array.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[inline]
    pub fn new() -> Self {
        Self::new_in(Global)
    }

    /// Creates an array with uninitialized elements.
    #[inline]
    pub fn uninit<I: IntoShape<IntoShape = S>>(shape: I) -> Tensor<MaybeUninit<T>, S> {
        Self::uninit_in(shape, Global)
    }

    /// Creates a new, empty array with the specified capacity.
    ///
    /// # Panics
    ///
    /// Panics if the default array length for the layout mapping is not zero.
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self::with_capacity_in(capacity, Global)
    }

    /// Creates an array with elements set to zero.
    ///
    /// Zero elements are created using `Default::default()`.
    #[inline]
    pub fn zeros<I: IntoShape<IntoShape = S>>(shape: I) -> Self
    where
        T: Default,
    {
        Self::zeros_in(shape, Global)
    }
}

#[cfg(not(feature = "nightly"))]
impl<T, S: Shape, A: Allocator> Tensor<MaybeUninit<T>, S, A> {
    /// Converts the array element type from `MaybeUninit<T>` to `T`.
    ///
    /// # Safety
    ///
    /// All elements in the array must be initialized, or the behavior is undefined.
    #[inline]
    pub unsafe fn assume_init(self) -> Tensor<T, S, A> {
        let (vec, mapping) = self.tensor.into_parts();

        let mut vec = mem::ManuallyDrop::new(vec);
        let (ptr, len, capacity) = (vec.as_mut_ptr(), vec.len(), vec.capacity());

        unsafe { Tensor::from_parts(Vec::from_raw_parts(ptr.cast(), len, capacity), mapping) }
    }
}

#[cfg(feature = "nightly")]
impl<T, S: Shape, A: Allocator> Tensor<MaybeUninit<T>, S, A> {
    /// Converts the array element type from `MaybeUninit<T>` to `T`.
    ///
    /// # Safety
    ///
    /// All elements in the array must be initialized, or the behavior is undefined.
    #[inline]
    pub unsafe fn assume_init(self) -> Tensor<T, S, A> {
        let (ptr, mapping, capacity, alloc) = self.into_raw_parts_with_alloc();

        unsafe { Tensor::from_raw_parts_in(ptr.cast(), mapping, capacity, alloc) }
    }
}

impl<'a, T, U, S: Shape, A: Allocator> Apply<U> for &'a Tensor<T, S, A> {
    type Output<F: FnMut(&'a T) -> U> = Map<Self::IntoExpr, F>;
    type ZippedWith<I: IntoExpression, F: FnMut((&'a T, I::Item)) -> U> =
        Map<Zip<Self::IntoExpr, I::IntoExpr>, F>;

    #[inline]
    fn apply<F: FnMut(&'a T) -> U>(self, f: F) -> Self::Output<F> {
        self.expr().map(f)
    }

    #[inline]
    fn zip_with<I: IntoExpression, F>(self, expr: I, f: F) -> Self::ZippedWith<I, F>
    where
        F: FnMut((&'a T, I::Item)) -> U,
    {
        self.expr().zip(expr).map(f)
    }
}

impl<'a, T, U, S: Shape, A: Allocator> Apply<U> for &'a mut Tensor<T, S, A> {
    type Output<F: FnMut(&'a mut T) -> U> = Map<Self::IntoExpr, F>;
    type ZippedWith<I: IntoExpression, F: FnMut((&'a mut T, I::Item)) -> U> =
        Map<Zip<Self::IntoExpr, I::IntoExpr>, F>;

    #[inline]
    fn apply<F: FnMut(&'a mut T) -> U>(self, f: F) -> Self::Output<F> {
        self.expr_mut().map(f)
    }

    #[inline]
    fn zip_with<I: IntoExpression, F>(self, expr: I, f: F) -> Self::ZippedWith<I, F>
    where
        F: FnMut((&'a mut T, I::Item)) -> U,
    {
        self.expr_mut().zip(expr).map(f)
    }
}

impl<T, S: Shape, A: Allocator> Apply<T> for Tensor<T, S, A> {
    type Output<F: FnMut(T) -> T> = Self;
    type ZippedWith<I: IntoExpression, F: FnMut((T, I::Item)) -> T> = Self;

    #[inline]
    fn apply<F: FnMut(T) -> T>(self, f: F) -> Self {
        self.map(f)
    }

    #[inline]
    fn zip_with<I: IntoExpression, F>(self, expr: I, f: F) -> Self
    where
        F: FnMut((T, I::Item)) -> T,
    {
        self.zip_with(expr, f)
    }
}

impl<T, U: ?Sized, S: Shape, A: Allocator> AsMut<U> for Tensor<T, S, A>
where
    Slice<T, S>: AsMut<U>,
{
    #[inline]
    fn as_mut(&mut self) -> &mut U {
        (**self).as_mut()
    }
}

impl<T, U: ?Sized, S: Shape, A: Allocator> AsRef<U> for Tensor<T, S, A>
where
    Slice<T, S>: AsRef<U>,
{
    #[inline]
    fn as_ref(&self) -> &U {
        (**self).as_ref()
    }
}

impl<T, S: Shape, A: Allocator> Borrow<Slice<T, S>> for Tensor<T, S, A> {
    #[inline]
    fn borrow(&self) -> &Slice<T, S> {
        self
    }
}

impl<T, S: Shape, A: Allocator> BorrowMut<Slice<T, S>> for Tensor<T, S, A> {
    #[inline]
    fn borrow_mut(&mut self) -> &mut Slice<T, S> {
        self
    }
}

impl<T: Clone, S: Shape, A: Allocator + Clone> Clone for Tensor<T, S, A> {
    #[inline]
    fn clone(&self) -> Self {
        Self { tensor: self.tensor.clone() }
    }

    #[inline]
    fn clone_from(&mut self, source: &Self) {
        self.tensor.clone_from(&source.tensor);
    }
}

impl<T: Debug, S: Shape, A: Allocator> Debug for Tensor<T, S, A> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        (**self).fmt(f)
    }
}

impl<T: Default, S: Shape> Default for Tensor<T, S> {
    #[inline]
    fn default() -> Self {
        Self::zeros(S::default())
    }
}

impl<T, S: Shape, A: Allocator> Deref for Tensor<T, S, A> {
    type Target = Slice<T, S>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.tensor.as_slice()
    }
}

impl<T, S: Shape, A: Allocator> DerefMut for Tensor<T, S, A> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.tensor.as_mut_slice()
    }
}

impl<'a, T: Copy, A: Allocator> Extend<&'a T> for Tensor<T, (Dyn,), A> {
    #[inline]
    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
        self.extend(iter.into_iter().copied());
    }
}

impl<T, A: Allocator> Extend<T> for Tensor<T, (Dyn,), A> {
    #[inline]
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        unsafe {
            self.tensor.with_mut_parts(|vec, mapping| {
                vec.extend(iter);
                *mapping = DenseMapping::new((vec.len(),));
            });
        }
    }
}

impl<T: Clone> From<&[T]> for Tensor<T, (Dyn,)> {
    #[inline]
    fn from(value: &[T]) -> Self {
        Self::from(value.to_vec())
    }
}

impl<T, S: ConstShape> From<Array<T, S>> for Tensor<T, S> {
    #[inline]
    fn from(value: Array<T, S>) -> Self {
        Self::from_expr(value.into_expr())
    }
}

impl<'a, T: 'a + Clone, S: Shape, L: Layout, I: IntoExpression<IntoExpr = View<'a, T, S, L>>>
    From<I> for Tensor<T, S>
{
    #[inline]
    fn from(value: I) -> Self {
        Self::from_expr(value.into_expr().cloned())
    }
}

impl<T, D: Dim, A: Allocator> From<Tensor<T, (D,), A>> for vec_t!(T, A) {
    #[inline]
    fn from(value: Tensor<T, (D,), A>) -> Self {
        value.into_vec()
    }
}

impl<T, A: Allocator> From<vec_t!(T, A)> for Tensor<T, (Dyn,), A> {
    #[inline]
    fn from(value: vec_t!(T, A)) -> Self {
        let mapping = DenseMapping::new((value.len(),));

        unsafe { Self::from_parts(value, mapping) }
    }
}

macro_rules! impl_from_array {
    (($($xyz:tt),+), ($($abc:tt),+), $array:tt) => {
        impl<T: Clone $(,$xyz: Dim + From<Const<$abc>>)+ $(,const $abc: usize)+> From<&$array>
            for Tensor<T, ($($xyz,)+)>
        {
            #[inline]
            fn from(value: &$array) -> Self {
                Self::from_expr(View::from(value).cloned())
            }
        }

        impl<T $(,$xyz: Dim + From<Const<$abc>>)+ $(,const $abc: usize)+> From<$array>
            for Tensor<T, ($($xyz,)+)>
        {
            #[inline]
            fn from(value: $array) -> Self {
                let mapping = DenseMapping::new(($($xyz::from(Const::<$abc>),)+));
                let capacity = mapping.shape().checked_len().expect("invalid length");

                let ptr = Box::into_raw(Box::new(value));

                unsafe { Self::from_raw_parts(ptr.cast(), mapping, capacity) }
            }
        }
    };
}

impl_from_array!((X), (A), [T; A]);
impl_from_array!((X, Y), (A, B), [[T; B]; A]);
impl_from_array!((X, Y, Z), (A, B, C), [[[T; C]; B]; A]);
impl_from_array!((X, Y, Z, W), (A, B, C, D), [[[[T; D]; C]; B]; A]);
impl_from_array!((X, Y, Z, W, U), (A, B, C, D, E), [[[[[T; E]; D]; C]; B]; A]);
impl_from_array!((X, Y, Z, W, U, V), (A, B, C, D, E, F), [[[[[[T; F]; E]; D]; C]; B]; A]);

impl<T, S: Shape> FromExpression<T, S> for Tensor<T, S> {
    #[cfg(not(feature = "nightly"))]
    #[inline]
    fn from_expr<I: IntoExpression<Item = T, Shape = S>>(expr: I) -> Self {
        Self::from_expr(expr.into_expr())
    }

    #[cfg(feature = "nightly")]
    #[inline]
    fn from_expr<I: IntoExpression<Item = T, Shape = S>>(expr: I) -> Self {
        Self::from_expr_in(expr.into_expr(), Global)
    }
}

impl<T> FromIterator<T> for Tensor<T, (Dyn,)> {
    #[inline]
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        Self::from(Vec::from_iter(iter))
    }
}

impl<T: Hash, S: Shape, A: Allocator> Hash for Tensor<T, S, A> {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        (**self).hash(state)
    }
}

impl<T, S: Shape, A: Allocator, I: SliceIndex<T, S, Dense>> Index<I> for Tensor<T, S, A> {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &I::Output {
        index.index(self)
    }
}

impl<T, S: Shape, A: Allocator, I: SliceIndex<T, S, Dense>> IndexMut<I> for Tensor<T, S, A> {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut I::Output {
        index.index_mut(self)
    }
}

impl<'a, T, S: Shape, A: Allocator> IntoExpression for &'a Tensor<T, S, A> {
    type Shape = S;
    type IntoExpr = View<'a, T, S>;

    #[inline]
    fn into_expr(self) -> Self::IntoExpr {
        self.expr()
    }
}

impl<'a, T, S: Shape, A: Allocator> IntoExpression for &'a mut Tensor<T, S, A> {
    type Shape = S;
    type IntoExpr = ViewMut<'a, T, S>;

    #[inline]
    fn into_expr(self) -> Self::IntoExpr {
        self.expr_mut()
    }
}

impl<T, S: Shape, A: Allocator> IntoExpression for Tensor<T, S, A> {
    type Shape = S;
    type IntoExpr = IntoExpr<Tensor<ManuallyDrop<T>, S, A>>;

    #[cfg(not(feature = "nightly"))]
    #[inline]
    fn into_expr(self) -> Self::IntoExpr {
        let (vec, mapping) = self.tensor.into_parts();

        let mut vec = mem::ManuallyDrop::new(vec);
        let (ptr, len, capacity) = (vec.as_mut_ptr(), vec.len(), vec.capacity());

        let tensor =
            unsafe { Tensor::from_parts(Vec::from_raw_parts(ptr.cast(), len, capacity), mapping) };

        IntoExpr::new(tensor)
    }

    #[cfg(feature = "nightly")]
    #[inline]
    fn into_expr(self) -> Self::IntoExpr {
        let (ptr, mapping, capacity, alloc) = self.into_raw_parts_with_alloc();

        let tensor = unsafe { Tensor::from_raw_parts_in(ptr.cast(), mapping, capacity, alloc) };

        IntoExpr::new(tensor)
    }
}

impl<'a, T, S: Shape, A: Allocator> IntoIterator for &'a Tensor<T, S, A> {
    type Item = &'a T;
    type IntoIter = Iter<View<'a, T, S>>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T, S: Shape, A: Allocator> IntoIterator for &'a mut Tensor<T, S, A> {
    type Item = &'a mut T;
    type IntoIter = Iter<ViewMut<'a, T, S>>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T, S: Shape, A: Allocator> IntoIterator for Tensor<T, S, A> {
    type Item = T;
    type IntoIter = Iter<IntoExpr<Tensor<ManuallyDrop<T>, S, A>>>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.into_expr().into_iter()
    }
}

impl<T, S: Shape> Owned<T, S> for Tensor<T, S> {
    type WithConst<const N: usize> = Tensor<T, S::Prepend<Const<N>>>;

    #[inline]
    fn clone_from_slice(&mut self, slice: &Slice<T, S>)
    where
        T: Clone,
    {
        unsafe {
            self.tensor.with_mut_parts(|vec, mapping| {
                slice[..].clone_into(vec);
                mapping.clone_from(slice.mapping());
            });
        }
    }
}

macro_rules! impl_try_from_array {
    (($($xyz:tt),+), ($($abc:tt),+), $array:tt) => {
        impl<T $(,$xyz: Dim)+ $(,const $abc: usize)+> TryFrom<Tensor<T, ($($xyz,)+)>> for $array {
            type Error = Tensor<T, ($($xyz,)+)>;

            #[inline]
            fn try_from(value: Tensor<T, ($($xyz,)+)>) -> Result<Self, Self::Error> {
                if value.shape().with_dims(|dims| dims == &[$($abc),+]) {
                    let mut vec = value.into_vec();

                    unsafe {
                        vec.set_len(0);

                        Ok((vec.as_ptr() as *const $array).read())
                    }
                } else {
                    Err(value)
                }
            }
        }
    };
}

impl_try_from_array!((X), (A), [T; A]);
impl_try_from_array!((X, Y), (A, B), [[T; B]; A]);
impl_try_from_array!((X, Y, Z), (A, B, C), [[[T; C]; B]; A]);
impl_try_from_array!((X, Y, Z, W), (A, B, C, D), [[[[T; D]; C]; B]; A]);
impl_try_from_array!((X, Y, Z, W, U), (A, B, C, D, E), [[[[[T; E]; D]; C]; B]; A]);
impl_try_from_array!((X, Y, Z, W, U, V), (A, B, C, D, E, F), [[[[[[T; F]; E]; D]; C]; B]; A]);