@@ -16,17 +16,9 @@ use imp_prelude::*;
16
16
17
17
use arraytraits;
18
18
use dimension;
19
- use iterators;
20
19
use error:: { self , ShapeError , ErrorKind } ;
21
20
use dimension:: IntoDimension ;
22
21
use dimension:: { abs_index, axes_of, Axes , do_slice, merge_axes, stride_offset} ;
23
- use iterators:: {
24
- new_lanes,
25
- new_lanes_mut,
26
- exact_chunks_of,
27
- exact_chunks_mut_of,
28
- windows
29
- } ;
30
22
use zip:: Zip ;
31
23
32
24
use {
@@ -199,6 +191,29 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
199
191
}
200
192
}
201
193
194
+ /// Returns a reference to the first element of the array, or `None` if it
195
+ /// is empty.
196
+ pub fn first ( & self ) -> Option < & A > {
197
+ if self . is_empty ( ) {
198
+ None
199
+ } else {
200
+ Some ( unsafe { & * self . as_ptr ( ) } )
201
+ }
202
+ }
203
+
204
+ /// Returns a mutable reference to the first element of the array, or
205
+ /// `None` if it is empty.
206
+ pub fn first_mut ( & mut self ) -> Option < & mut A >
207
+ where
208
+ S : DataMut ,
209
+ {
210
+ if self . is_empty ( ) {
211
+ None
212
+ } else {
213
+ Some ( unsafe { & mut * self . as_mut_ptr ( ) } )
214
+ }
215
+ }
216
+
202
217
/// Return an iterator of references to the elements of the array.
203
218
///
204
219
/// Elements are visited in the *logical order* of the array, which
@@ -303,8 +318,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
303
318
304
319
// Copy the dim and strides that remain after removing the subview axes.
305
320
let out_ndim = info. out_ndim ( ) ;
306
- let mut new_dim = Do :: zero_index_with_ndim ( out_ndim) ;
307
- let mut new_strides = Do :: zero_index_with_ndim ( out_ndim) ;
321
+ let mut new_dim = Do :: zeros ( out_ndim) ;
322
+ let mut new_strides = Do :: zeros ( out_ndim) ;
308
323
izip ! ( self . dim. slice( ) , self . strides. slice( ) , indices)
309
324
. filter_map ( |( d, s, slice_or_index) | match slice_or_index {
310
325
& SliceOrIndex :: Slice { ..} => Some ( ( d, s) ) ,
@@ -676,7 +691,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
676
691
pub fn genrows ( & self ) -> Lanes < A , D :: Smaller > {
677
692
let mut n = self . ndim ( ) ;
678
693
if n == 0 { n += 1 ; }
679
- new_lanes ( self . view ( ) , Axis ( n - 1 ) )
694
+ Lanes :: new ( self . view ( ) , Axis ( n - 1 ) )
680
695
}
681
696
682
697
/// Return a producer and iterable that traverses over the *generalized*
@@ -688,7 +703,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
688
703
{
689
704
let mut n = self . ndim ( ) ;
690
705
if n == 0 { n += 1 ; }
691
- new_lanes_mut ( self . view_mut ( ) , Axis ( n - 1 ) )
706
+ LanesMut :: new ( self . view_mut ( ) , Axis ( n - 1 ) )
692
707
}
693
708
694
709
/// Return a producer and iterable that traverses over the *generalized*
@@ -718,7 +733,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
718
733
/// }
719
734
/// ```
720
735
pub fn gencolumns ( & self ) -> Lanes < A , D :: Smaller > {
721
- new_lanes ( self . view ( ) , Axis ( 0 ) )
736
+ Lanes :: new ( self . view ( ) , Axis ( 0 ) )
722
737
}
723
738
724
739
/// Return a producer and iterable that traverses over the *generalized*
@@ -728,7 +743,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
728
743
pub fn gencolumns_mut ( & mut self ) -> LanesMut < A , D :: Smaller >
729
744
where S : DataMut
730
745
{
731
- new_lanes_mut ( self . view_mut ( ) , Axis ( 0 ) )
746
+ LanesMut :: new ( self . view_mut ( ) , Axis ( 0 ) )
732
747
}
733
748
734
749
/// Return a producer and iterable that traverses over all 1D lanes
@@ -760,7 +775,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
760
775
/// assert_eq!(inner2.into_iter().next().unwrap(), aview1(&[0, 1, 2]));
761
776
/// ```
762
777
pub fn lanes ( & self , axis : Axis ) -> Lanes < A , D :: Smaller > {
763
- new_lanes ( self . view ( ) , axis)
778
+ Lanes :: new ( self . view ( ) , axis)
764
779
}
765
780
766
781
/// Return a producer and iterable that traverses over all 1D lanes
@@ -770,7 +785,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
770
785
pub fn lanes_mut ( & mut self , axis : Axis ) -> LanesMut < A , D :: Smaller >
771
786
where S : DataMut
772
787
{
773
- new_lanes_mut ( self . view_mut ( ) , axis)
788
+ LanesMut :: new ( self . view_mut ( ) , axis)
774
789
}
775
790
776
791
@@ -819,7 +834,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
819
834
pub fn axis_iter ( & self , axis : Axis ) -> AxisIter < A , D :: Smaller >
820
835
where D : RemoveAxis ,
821
836
{
822
- iterators :: new_axis_iter ( self . view ( ) , axis. index ( ) )
837
+ AxisIter :: new ( self . view ( ) , axis)
823
838
}
824
839
825
840
@@ -834,7 +849,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimens
10000
ion
834
849
where S : DataMut ,
835
850
D : RemoveAxis ,
836
851
{
837
- iterators :: new_axis_iter_mut ( self . view_mut ( ) , axis. index ( ) )
852
+ AxisIterMut :: new ( self . view_mut ( ) , axis)
838
853
}
839
854
840
855
@@ -865,7 +880,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
865
880
/// [[26, 27]]]));
866
881
/// ```
867
882
pub fn axis_chunks_iter ( & self , axis : Axis , size : usize ) -> AxisChunksIter < A , D > {
868
- iterators :: new_chunk_iter ( self . view ( ) , axis. index ( ) , size)
883
+ AxisChunksIter :: new ( self . view ( ) , axis, size)
869
884
}
870
885
871
886
/// Return an iterator that traverses over `axis` by chunks of `size`,
@@ -878,7 +893,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
878
893
-> AxisChunksIterMut < A , D >
879
894
where S : DataMut
880
895
{
881
- iterators :: new_chunk_iter_mut ( self . view_mut ( ) , axis. index ( ) , size)
896
+ AxisChunksIterMut :: new ( self . view_mut ( ) , axis, size)
882
897
}
883
898
884
899
/// Return an exact chunks producer (and iterable).
@@ -895,7 +910,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
895
910
pub fn exact_chunks < E > ( & self , chunk_size : E ) -> ExactChunks < A , D >
896
911
where E : IntoDimension < Dim =D > ,
897
912
{
898
- exact_chunks_of ( self . view ( ) , chunk_size)
913
+ ExactChunks :: new ( self . view ( ) , chunk_size)
899
914
}
900
915
901
916
/// Return an exact chunks producer (and iterable).
@@ -934,7 +949,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
934
949
where E : IntoDimension < Dim =D > ,
935
950
S : DataMut
936
951
{
937
- exact_chunks_mut_of ( self . view_mut ( ) , chunk_size)
952
+ ExactChunksMut :: new ( self . view_mut ( ) , chunk_size)
938
953
}
939
954
940
955
/// Return a window producer and iterable.
@@ -954,7 +969,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
954
969
pub fn windows < E > ( & self , window_size : E ) -> Windows < A , D >
955
970
where E : IntoDimension < Dim =D >
956
971
{
957
- windows ( self . view ( ) , window_size)
972
+ Windows :: new ( self . view ( ) , window_size)
958
973
}
959
974
960
975
// Return (length, stride) for diagonal
@@ -1386,7 +1401,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1386
1401
{
1387
1402
let axes = axes. into_dimension ( ) ;
1388
1403
// Ensure that each axis is used exactly once.
1389
- let mut usage_counts = D :: zero_index_with_ndim ( self . ndim ( ) ) ;
1404
+ let mut usage_counts = D :: zeros ( self . ndim ( ) ) ;
1390
1405
for axis in axes. slice ( ) {
1391
1406
usage_counts[ * axis] += 1 ;
1392
1407
}
@@ -1395,7 +1410,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1395
1410
}
1396
1411
// Determine the new shape and strides.
1397
1412
let mut new_dim = usage_counts; // reuse to avoid an allocation
1398
- let mut new_strides = D :: zero_index_with_ndim ( self . ndim ( ) ) ;
1413
+ let mut new_strides = D :: zeros ( self . ndim ( ) ) ;
1399
1414
{
1400
1415
let dim = self . dim . slice ( ) ;
1401
1416
let strides = self . strides . slice ( ) ;
@@ -1597,8 +1612,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
1597
1612
// break the arrays up into their inner rows
1598
1613
let n = self . ndim ( ) ;
1599
1614
let dim = self . raw_dim ( ) ;
1600
- Zip :: from ( new_lanes_mut ( self . view_mut ( ) , Axis ( n - 1 ) ) )
1601
- . and ( new_lanes ( rhs. broadcast_assume ( dim) , Axis ( n - 1 ) ) )
1615
+ Zip :: from ( LanesMut :: new ( self . view_mut ( ) , Axis ( n - 1 ) ) )
1616
+ . and ( Lanes :: new ( rhs. broadcast_assume ( dim) , Axis ( n - 1 ) ) )
1602
1617
. apply ( move |s_row, r_row| {
1603
1618
Zip :: from ( s_row) . and ( r_row) . apply ( |a, b| f ( a, b) )
1604
1619
} ) ;
0 commit comments