@@ -16,17 +16,9 @@ use imp_prelude::*;
1616
1717use arraytraits;
1818use dimension;
19- use iterators;
2019use error:: { self , ShapeError , ErrorKind } ;
2120use dimension:: IntoDimension ;
2221use 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- } ;
3022use zip:: Zip ;
3123
3224use {
@@ -199,6 +191,29 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
199191 }
200192 }
201193
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+
202217 /// Return an iterator of references to the elements of the array.
203218 ///
204219 /// 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
303318
304319 // Copy the dim and strides that remain after removing the subview axes.
305320 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) ;
308323 izip ! ( self . dim. slice( ) , self . strides. slice( ) , indices)
309324 . filter_map ( |( d, s, slice_or_index) | match slice_or_index {
310325 & SliceOrIndex :: Slice { ..} => Some ( ( d, s) ) ,
@@ -676,7 +691,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
676691 pub fn genrows ( & self ) -> Lanes < A , D :: Smaller > {
677692 let mut n = self . ndim ( ) ;
678693 if n == 0 { n += 1 ; }
679- new_lanes ( self . view ( ) , Axis ( n - 1 ) )
694+ Lanes :: new ( self . view ( ) , Axis ( n - 1 ) )
680695 }
681696
682697 /// 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
688703 {
689704 let mut n = self . ndim ( ) ;
690705 if n == 0 { n += 1 ; }
691- new_lanes_mut ( self . view_mut ( ) , Axis ( n - 1 ) )
706+ LanesMut :: new ( self . view_mut ( ) , Axis ( n - 1 ) )
692707 }
693708
694709 /// 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
718733 /// }
719734 /// ```
720735 pub fn gencolumns ( & self ) -> Lanes < A , D :: Smaller > {
721- new_lanes ( self . view ( ) , Axis ( 0 ) )
736+ Lanes :: new ( self . view ( ) , Axis ( 0 ) )
722737 }
723738
724739 /// 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
728743 pub fn gencolumns_mut ( & mut self ) -> LanesMut < A , D :: Smaller >
729744 where S : DataMut
730745 {
731- new_lanes_mut ( self . view_mut ( ) , Axis ( 0 ) )
746+ LanesMut :: new ( self . view_mut ( ) , Axis ( 0 ) )
732747 }
733748
734749 /// 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
760775 /// assert_eq!(inner2.into_iter().next().unwrap(), aview1(&[0, 1, 2]));
761776 /// ```
762777 pub fn lanes ( & self , axis : Axis ) -> Lanes < A , D :: Smaller > {
763- new_lanes ( self . view ( ) , axis)
778+ Lanes :: new ( self . view ( ) , axis)
764779 }
765780
766781 /// 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
770785 pub fn lanes_mut ( & mut self , axis : Axis ) -> LanesMut < A , D :: Smaller >
771786 where S : DataMut
772787 {
773- new_lanes_mut ( self . view_mut ( ) , axis)
788+ LanesMut :: new ( self . view_mut ( ) , axis)
774789 }
775790
776791
@@ -819,7 +834,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
819834 pub fn axis_iter ( & self , axis : Axis ) -> AxisIter < A , D :: Smaller >
820835 where D : RemoveAxis ,
821836 {
822- iterators :: new_axis_iter ( self . view ( ) , axis. index ( ) )
837+ AxisIter :: new ( self . view ( ) , axis)
823838 }
824839
825840
@@ -834,7 +849,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
834849 where S : DataMut ,
835850 D : RemoveAxis ,
836851 {
837- iterators :: new_axis_iter_mut ( self . view_mut ( ) , axis. index ( ) )
852+ AxisIterMut :: new ( self . view_mut ( ) , axis)
838853 }
839854
840855
@@ -865,7 +880,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
865880 /// [[26, 27]]]));
866881 /// ```
867882 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)
869884 }
870885
871886 /// 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
878893 -> AxisChunksIterMut < A , D >
879894 where S : DataMut
880895 {
881- iterators :: new_chunk_iter_mut ( self . view_mut ( ) , axis. index ( ) , size)
896+ AxisChunksIterMut :: new ( self . view_mut ( ) , axis, size)
882897 }
883898
884899 /// 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
895910 pub fn exact_chunks < E > ( & self , chunk_size : E ) -> ExactChunks < A , D >
896911 where E : IntoDimension < Dim =D > ,
897912 {
898- exact_chunks_of ( self . view ( ) , chunk_size)
913+ ExactChunks :: new ( self . view ( ) , chunk_size)
899914 }
900915
901916 /// 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
934949 where E : IntoDimension < Dim =D > ,
935950 S : DataMut
936951 {
937- exact_chunks_mut_of ( self . view_mut ( ) , chunk_size)
952+ ExactChunksMut :: new ( self . view_mut ( ) , chunk_size)
938953 }
939954
940955 /// Return a window producer and iterable.
@@ -954,7 +969,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
954969 pub fn windows < E > ( & self , window_size : E ) -> Windows < A , D >
955970 where E : IntoDimension < Dim =D >
956971 {
957- windows ( self . view ( ) , window_size)
972+ Windows :: new ( self . view ( ) , window_size)
958973 }
959974
960975 // Return (length, stride) for diagonal
@@ -1386,7 +1401,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
13861401 {
13871402 let axes = axes. into_dimension ( ) ;
13881403 // 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 ( ) ) ;
13901405 for axis in axes. slice ( ) {
13911406 usage_counts[ * axis] += 1 ;
13921407 }
@@ -1395,7 +1410,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
13951410 }
13961411 // Determine the new shape and strides.
13971412 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 ( ) ) ;
13991414 {
14001415 let dim = self . dim . slice ( ) ;
14011416 let strides = self . strides . slice ( ) ;
@@ -1597,8 +1612,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
15971612 // break the arrays up into their inner rows
15981613 let n = self . ndim ( ) ;
15991614 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 ) ) )
16021617 . apply ( move |s_row, r_row| {
16031618 Zip :: from ( s_row) . and ( r_row) . apply ( |a, b| f ( a, b) )
16041619 } ) ;
0 commit comments