@@ -75,10 +75,15 @@ use std::marker::PhantomData;
75
75
76
76
use itertools:: ZipSlices ;
77
77
78
- pub use dimension:: { Dimension , RemoveAxis } ;
78
+ pub use dimension:: {
79
+ Dimension ,
80
+ RemoveAxis ,
81
+ } ;
82
+
79
83
pub use dimension:: NdIndex ;
80
84
pub use indexes:: Indexes ;
81
85
pub use shape_error:: ShapeError ;
86
+ pub use stride_error:: StrideError ;
82
87
pub use si:: { Si , S } ;
83
88
84
89
use dimension:: stride_offset;
@@ -107,6 +112,7 @@ mod linspace;
107
112
mod numeric_util;
108
113
mod si;
109
114
mod shape_error;
115
+ mod stride_error;
110
116
111
117
// NOTE: In theory, the whole library should compile
112
118
// and pass tests even if you change Ix and Ixs.
@@ -605,12 +611,43 @@ impl<S, A, D> ArrayBase<S, D>
605
611
}
606
612
}
607
613
614
+ /// Create an array with copies of `elem`, dimension `dim` and fortran
615
+ /// ordering.
616
+ ///
617
+ /// ```
618
+ /// use ndarray::Array;
619
+ /// use ndarray::arr3;
620
+ ///
621
+ /// let a = Array::from_elem_f((2, 2, 2), 1.);
622
+ ///
623
+ /// assert!(
624
+ /// a == arr3(&[[[1., 1.],
625
+ /// [1., 1.]],
626
+ /// [[1., 1.],
627
+ /// [1., 1.]]])
628
+ /// );
629
+ /// assert!(a.strides() == &[1, 2, 4]);
630
+ /// ```
631
+ pub fn from_elem_f ( dim : D , elem : A ) -> ArrayBase < S , D > where A : Clone
632
+ {
633
+ let v = vec ! [ elem; dim. size( ) ] ;
634
+ unsafe {
635
+ Self :: from_vec_dim_f ( dim, v)
636
+ }
637
+ }
638
+
608
639
/// Create an array with zeros, dimension `dim`.
609
640
pub fn zeros ( dim : D ) -> ArrayBase < S , D > where A : Clone + libnum:: Zero
610
641
{
611
642
Self :: from_elem ( dim, libnum:: zero ( ) )
612
643
}
613
644
645
+ /// Create an array with zeros, dimension `dim` and fortran ordering.
646
+ pub fn zeros_f ( dim : D ) -> ArrayBase < S , D > where A : Clone + libnum:: Zero
647
+ {
648
+ Self :: from_elem_f ( dim, libnum:: zero ( ) )
649
+ }
650
+
614
651
/// Create an array with default values, dimension `dim`.
615
652
pub fn default ( dim : D ) -> ArrayBase < S , D >
616
653
where A : Default
@@ -634,6 +671,58 @@ impl<S, A, D> ArrayBase<S, D>
634
671
dim : dim
635
672
}
636
673
}
674
+
675
+ /// Create an array from a vector (with no allocation needed),
676
+ /// using fortran ordering to interpret the data.
677
+ ///
678
+ /// Unsafe because dimension is unchecked, and must be correct.
679
+ pub unsafe fn from_vec_dim_f ( dim : D , mut v : Vec < A > ) -> ArrayBase < S , D >
680
+ {
681
+ debug_assert ! ( dim. size( ) == v. len( ) ) ;
682
+ ArrayBase {
683
+ ptr : v. as_mut_ptr ( ) ,
684
+ data : DataOwned :: new ( v) ,
685
+ strides : dim. fortran_strides ( ) ,
686
+ dim : dim
687
+ }
688
+ }
689
+
690
+
691
+ /// Create an array from a vector and interpret it according to the
692
+ /// provided dimensions and strides. No allocation needed.
693
+ ///
694
+ /// Unsafe because dimension and strides are unchecked.
695
+ pub unsafe fn from_vec_dim_stride_uchk ( dim : D ,
696
+ strides : D ,
697
+ mut v : Vec < A >
698
+ ) -> ArrayBase < S , D >
699
+ {
700
+ ArrayBase {
701
+ ptr : v. as_mut_ptr ( ) ,
702
+ data : DataOwned :: new ( v) ,
703
+ strides : strides,
704
+ dim : dim
705
+ }
706
+ }
707
+
708
+ /// Create an array from a vector and interpret it according to the
709
+ /// provided dimensions and strides. No allocation needed.
710
+ ///
711
+ /// Checks whether `dim` and `strides` are compatible with the vector's
712
+ /// length, returning an `Err` if not compatible.
713
+ pub fn from_vec_dim_stride ( dim : D ,
714
+ strides : D ,
715
+ v : Vec < A >
716
+ ) -> Result < ArrayBase < S , D > , StrideError >
717
+ {
718
+ dimension:: can_index_slice ( & v,
719
+ & dim,
720
+ & strides) . map ( |_| {
721
+ unsafe {
722
+ Self :: from_vec_dim_stride_uchk ( dim, strides, v)
723
+ }
724
+ } )
725
+ }
637
726
}
638
727
639
728
@@ -666,6 +755,44 @@ impl<'a, A, D> ArrayView<'a, A, D>
666
755
}
667
756
}
668
757
758
+ /// Create an `ArrayView` borrowing its data from a slice.
759
+ ///
760
+ /// Checks whether `dim` and `strides` are compatible with the slice's
761
+ /// length, returning an `Err` if not compatible.
762
+ ///
763
+ /// ```
764
+ /// use ndarray::ArrayView;
765
+ /// use ndarray::arr3;
766
+ ///
767
+ /// let s = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
768
+ /// let a = ArrayView::from_slice_dim_stride((2, 3, 2),
769
+ /// (1, 4, 2),
770
+ /// s).unwrap();
771
+ ///
772
+ /// assert!(
773
+ /// a == arr3(&[[[0, 2],
774
+ /// [4, 6],
775
+ /// [8, 10]],
776
+ /// [[1, 3],
777
+ /// [5, 7],
778
+ /// [9, 11]]])
779
+ /// );
780
+ /// assert!(a.strides() == &[1, 4, 2]);
781
+ /// ```
782
+ pub fn from_slice_dim_stride ( dim : D ,
783
+ strides : D ,
784
+ s : & ' a [ A ]
785
+ ) -> Result < Self , StrideError >
786
+ {
787
+ dimension:: can_index_slice ( s,
788
+ & dim,
789
+ & strides) . map ( |_| {
790
+ unsafe {
791
+ Self :: new_ ( s. as_ptr ( ) , dim, strides)
792
+ }
793
+ } )
794
+ }
795
+
669
796
#[ inline]
670
797
fn into_base_iter ( self ) -> Baseiter < ' a , A , D > {
671
798
unsafe {
@@ -723,6 +850,45 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
723
850
}
724
851
}
725
852
853
+ /// Create an `ArrayView` borrowing its data from a slice.
854
+ ///
855
+ /// Checks whether `dim` and `strides` are compatible with the slice's
856
+ /// length, returning an `Err` if not compatible.
857
+ ///
858
+ /// ```
859
+ /// use ndarray::ArrayViewMut;
860
+ /// use ndarray::arr3;
861
+ ///
862
+ /// let s = &mut [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
863
+ /// let mut a = ArrayViewMut::from_slice_dim_stride((2, 3, 2),
864
+ /// (1, 4, 2),
865
+ /// s).unwrap();
866
+ ///
867
+ /// a[[0, 0, 0]] = 1;
868
+ /// assert!(
869
+ /// a == arr3(&[[[1, 2],
870
+ /// [4, 6],
871
+ /// [8, 10]],
872
+ /// [[1, 3],
873
+ /// [5, 7],
874
+ /// [9, 11]]])
875
+ /// );
876
+ /// assert!(a.strides() == &[1, 4, 2]);
877
+ /// ```
878
+ pub fn from_slice_dim_stride ( dim : D ,
879
+ strides : D ,
880
+ s : & ' a mut [ A ]
881
+ ) -> Result < Self , StrideError >
882
+ {
883
+ dimension:: can_index_slice ( s,
884
+ & dim,
885
+ & strides) . map ( |_| {
886
+ unsafe {
887
+ Self :: new_ ( s. as_mut_ptr ( ) , dim, strides)
888
+ }
889
+ } )
890
+ }
891
+
726
892
#[ inline]
727
893
fn into_base_iter ( self ) -> Baseiter < ' a , A , D > {
728
894
unsafe {
0 commit comments