@@ -94,6 +94,8 @@ pub use dimension::{
94
94
RemoveAxis ,
95
95
} ;
96
96
97
+ use dimension:: stride_offset;
98
+
97
99
pub use dimension:: NdIndex ;
98
100
pub use indexes:: Indexes ;
99
101
pub use shape_error:: ShapeError ;
@@ -923,6 +925,40 @@ impl<'a, A, D> ArrayView<'a, A, D>
923
925
{
924
926
iterators:: new_outer_iter ( self )
925
927
}
928
+
929
+ /// Split the array along `axis` and return one view strictly before the
930
+ /// split and one view after the split.
931
+ ///
932
+ /// **Panics** if `axis` is out of bounds.
933
+ pub fn axis_split_at ( self , axis : usize , index : Ix )
934
+ -> ( Self , Self )
935
+ {
936
+ assert ! ( index <= self . shape( ) [ axis] ) ;
937
+ let left_ptr = self . ptr ;
938
+ let right_ptr = if index == self . shape ( ) [ axis] {
939
+ self . ptr
940
+ } else {
941
+ let offset = stride_offset ( index, self . strides . slice ( ) [ axis] ) ;
942
+ unsafe {
943
+ self . ptr . offset ( offset)
944
+ }
945
+ } ;
946
+
947
+ let mut dim_left = self . dim . clone ( ) ;
948
+ dim_left. slice_mut ( ) [ axis] = index;
949
+ let left = unsafe {
950
+ Self :: new_ ( left_ptr, dim_left, self . strides . clone ( ) )
951
+ } ;
952
+
953
+ let mut dim_right = self . dim . clone ( ) ;
954
+ dim_right. slice_mut ( ) [ axis] = self . dim . slice ( ) [ axis] - index;
955
+ let right = unsafe {
956
+ Self :: new_ ( right_ptr, dim_right, self . strides . clone ( ) )
957
+ } ;
958
+
959
+ ( left, right)
960
+ }
961
+
926
962
}
927
963
928
964
impl < ' a , A , D > ArrayViewMut < ' a , A , D >
@@ -1018,6 +1054,41 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
1018
1054
{
1019
1055
iterators:: new_outer_iter_mut ( self )
1020
1056
}
1057
+
1058
+ /// Split the array along `axis` and return one mutable view strictly
1059
+ /// before the split and one mutable view after the split.
1060
+ ///
1061
+ /// **Panics** if `axis` is out of bounds.
1062
+ pub fn axis_split_at ( self , axis : usize , index : Ix )
1063
+ -> ( Self , Self )
1064
+ {
1065
+ assert ! ( index <= self . shape( ) [ axis] ) ;
1066
+ let left_ptr = self . ptr ;
1067
+ let right_ptr = if index == self . shape ( ) [ axis] {
1068
+ self . ptr
1069
+ }
1070
+ else {
1071
+ let offset = stride_offset ( index, self . strides . slice ( ) [ axis] ) ;
1072
+ unsafe {
1073
+ self . ptr . offset ( offset)
1074
+ }
1075
+ } ;
1076
+
1077
+ let mut dim_left = self . dim . clone ( ) ;
1078
+ dim_left. slice_mut ( ) [ axis] = index;
1079
+ let left = unsafe {
1080
+ Self :: new_ ( left_ptr, dim_left, self . strides . clone ( ) )
1081
+ } ;
1082
+
1083
+ let mut dim_right = self . dim . clone ( ) ;
1084
+ dim_right. slice_mut ( ) [ axis] = self . dim . slice ( ) [ axis] - index;
1085
+ let right = unsafe {
1086
+ Self :: new_ ( right_ptr, dim_right, self . strides . clone ( ) )
1087
+ } ;
1088
+
1089
+ ( left, right)
1090
+ }
1091
+
1021
1092
}
1022
1093
1023
1094
impl < A , S , D > ArrayBase < S , D > where S : Data < Elem =A > , D : Dimension
@@ -1439,6 +1510,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1439
1510
iterators:: new_axis_iter_mut ( self . view_mut ( ) , axis)
1440
1511
}
1441
1512
1513
+
1442
1514
/// Return an iterator that traverses over `axis` by chunks of `size`,
1443
1515
/// yielding non-overlapping views along that axis.
1444
1516
///
0 commit comments