@@ -379,17 +379,49 @@ pub trait ConstSliceSortExt<T> {
379
379
fn const_is_sorted_by < F > ( & self , compare : F ) -> bool
380
380
where
381
381
F : FnMut ( & T , & T ) -> Option < Ordering > ;
382
+ /// Checks if the elements of this slice are sorted using the given key extraction function.
383
+ ///
384
+ /// Instead of comparing the slice's elements directly, this function compares the keys of the
385
+ /// elements, as determined by `f`. Apart from that, it's equivalent to [`is_sorted`]; see its
386
+ /// documentation for more information.
387
+ ///
388
+ /// [`is_sorted`]: slice::is_sorted
389
+ ///
390
+ /// # Examples
391
+ ///
392
+ /// ```
393
+ /// #![feature(const_mut_refs)]
394
+ /// #![feature(const_trait_impl)]
395
+ /// use const_sort_rs::ConstSliceSortExt;
396
+ ///
397
+ /// const fn map_len(s: &&str) -> usize {
398
+ /// s.len()
399
+ /// }
400
+ /// const A: bool = ["c", "bb", "aaa"].const_is_sorted_by_key(map_len);
401
+ /// assert!(A);
402
+ ///
403
+ /// const fn map_abs(i: &i32) -> i32 {
404
+ /// i.abs()
405
+ /// }
406
+ /// const B: bool = [-2i32, -1, 0, 3].const_is_sorted_by_key(map_abs);
407
+ /// assert!(!B);
408
+ /// ```
409
+ #[ must_use]
410
+ fn const_is_sorted_by_key < F , K > ( & self , f : F ) -> bool
411
+ where
412
+ F : FnMut ( & T ) -> K ,
413
+ K : PartialOrd ;
382
414
}
383
415
384
416
pub ( crate ) const fn const_pred_lt < T : Ord + ~const PartialOrd > ( a : & T , b : & T ) -> bool {
385
417
a. lt ( b)
386
418
}
387
419
388
- impl < T : ~ const PartialOrd > const ConstSliceSortExt < T > for [ T ] {
420
+ impl < T > const ConstSliceSortExt < T > for [ T ] {
389
421
#[ inline]
390
422
fn const_sort_unstable ( & mut self )
391
423
where
392
- T : Ord ,
424
+ T : ~ const PartialOrd + Ord ,
393
425
{
394
426
const_sort:: const_quicksort ( self , const_pred_lt) ;
395
427
}
@@ -425,7 +457,7 @@ impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
425
457
#[ inline]
426
458
fn const_select_nth_unstable ( & mut self , index : usize ) -> ( & mut [ T ] , & mut T , & mut [ T ] )
427
459
where
428
- T : Ord ,
460
+ T : ~ const PartialOrd + Ord ,
429
461
{
430
462
// let mut f = |a: &T, b: &T| a.lt(b);
431
463
// sort::partition_at_index(self, index, &mut f)
@@ -473,7 +505,10 @@ impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
473
505
}
474
506
475
507
#[ inline]
476
- fn const_is_sorted ( & self ) -> bool {
508
+ fn const_is_sorted ( & self ) -> bool
509
+ where
510
+ T : ~const PartialOrd ,
511
+ {
477
512
const fn const_pred_p_cmp < T : ~const PartialOrd > ( a : & T , b : & T ) -> Option < Ordering > {
478
513
a. partial_cmp ( b)
479
514
}
@@ -499,4 +534,16 @@ impl<T: ~const PartialOrd> const ConstSliceSortExt<T> for [T] {
499
534
}
500
535
true
501
536
}
537
+ #[ inline]
538
+ fn const_is_sorted_by_key < F , K > ( & self , mut f : F ) -> bool
539
+ where
540
+ F : ~const FnMut ( & T ) -> K + ~const Destruct ,
541
+ K : ~const PartialOrd + ~const Destruct ,
542
+ {
543
+ self . const_is_sorted_by (
544
+ const_closure ! ( FnMut for <T , K : PartialOrd , F : FnMut ( & T ) -> K > [ f: F ] ( a: & T , b: & T ) -> Option <Ordering > {
545
+ f( a) . partial_cmp( & f( b) )
546
+ } ) ,
547
+ )
548
+ }
502
549
}
0 commit comments