@@ -769,6 +769,21 @@ pub fn set_difference<T: Borrow<usize>, S: Borrow<usize>>(
769769 . collect ( )
770770}
771771
772+ /// Checks whether the given index sequence is monotonically non-decreasing.
773+ #[ deprecated( since = "45.0.0" , note = "Use std::Iterator::is_sorted instead" ) ]
774+ pub fn is_sorted < T : Borrow < usize > > ( sequence : impl IntoIterator < Item = T > ) -> bool {
775+ // TODO: Remove this function when `is_sorted` graduates from Rust nightly.
776+ let mut previous = 0 ;
777+ for item in sequence. into_iter ( ) {
778+ let current = * item. borrow ( ) ;
779+ if current < previous {
780+ return false ;
781+ }
782+ previous = current;
783+ }
784+ true
785+ }
786+
772787/// Find indices of each element in `targets` inside `items`. If one of the
773788/// elements is absent in `items`, returns an error.
774789pub fn find_indices < T : PartialEq , S : Borrow < T > > (
@@ -1157,6 +1172,19 @@ mod tests {
11571172 assert_eq ! ( set_difference( [ 3 , 4 , 0 ] , [ 4 , 1 , 2 ] ) , vec![ 3 , 0 ] ) ;
11581173 }
11591174
1175+ #[ test]
1176+ #[ expect( deprecated) ]
1177+ fn test_is_sorted ( ) {
1178+ assert ! ( is_sorted:: <usize >( [ ] ) ) ;
1179+ assert ! ( is_sorted( [ 0 ] ) ) ;
1180+ assert ! ( is_sorted( [ 0 , 3 , 4 ] ) ) ;
1181+ assert ! ( is_sorted( [ 0 , 1 , 2 ] ) ) ;
1182+ assert ! ( is_sorted( [ 0 , 1 , 4 ] ) ) ;
1183+ assert ! ( is_sorted( [ 0usize ; 0 ] ) ) ;
1184+ assert ! ( is_sorted( [ 1 , 2 ] ) ) ;
1185+ assert ! ( !is_sorted( [ 3 , 2 ] ) ) ;
1186+ }
1187+
11601188 #[ test]
11611189 fn test_find_indices ( ) -> Result < ( ) > {
11621190 assert_eq ! ( find_indices( & [ 0 , 3 , 4 ] , [ 0 , 3 , 4 ] ) ?, vec![ 0 , 1 , 2 ] ) ;
0 commit comments