@@ -423,10 +423,10 @@ extension Sequence {
423
423
/// if its first argument should be ordered before its second
424
424
/// argument; otherwise, `false`.
425
425
/// - Returns: A tuple with the sequence's minimum element, followed by its
426
- /// maximum element. For either member, if the sequence provides multiple
427
- /// qualifying elements, the one chosen is unspecified. The same element may
428
- /// be used for both members if all the elements are equivalent. If the
429
- /// sequence has no elements, returns `nil`.
426
+ /// maximum element. If the sequence provides multiple qualifying minimum
427
+ /// elements, the first equivalent element is returned; of multiple maximum
428
+ /// elements, the last is returned. If the sequence has no elements, the
429
+ /// method returns `nil`.
430
430
///
431
431
/// - Complexity: O(*n*), where *n* is the length of the sequence.
432
432
public func minAndMax(
@@ -440,55 +440,16 @@ extension Sequence {
440
440
// Confirm the initial bounds.
441
441
if try areInIncreasingOrder ( highest, lowest) { swap ( & lowest, & highest) }
442
442
443
- #if true
444
443
// Read the elements in pairwise. Structuring the comparisons around this
445
444
// is actually faster than loops based on extracting and testing elements
446
445
// one-at-a-time.
447
446
while var low = iterator. next ( ) {
448
- if var high = iterator. next ( ) {
449
- // Update the upper bound with the larger new element.
450
- if try areInIncreasingOrder ( high, low) { swap ( & low, & high) }
451
- if try ! areInIncreasingOrder( high, highest) { highest = high }
452
- } else {
453
- // Update the upper bound by reusing the last element. The next element
454
- // iteration will also fail, ending the loop.
455
- if try ! areInIncreasingOrder( low, highest) { highest = low }
456
- }
457
-
458
- // Update the lower bound with the smaller new element, which may need a
459
- // swap first to determine.
460
- if try areInIncreasingOrder ( low, lowest) { lowest = low }
461
- }
462
- #else
463
- /// Ensure the second argument has a value that is ranked at least as much as
464
- /// the first argument.
465
- func sort( _ a: inout Element , _ b: inout Element ) throws {
466
- if try areInIncreasingOrder ( b, a) { swap ( & a, & b) }
467
- }
468
-
469
- /// Find the smallest and largest values out of a group of four arguments.
470
- func minAndMaxOf4(
471
- _ a: Element , _ b: Element , _ c: Element , _ d: Element
472
- ) throws -> ( min: Element , max: Element ) {
473
- var ( a, b, c, d) = ( a, b, c, d)
474
- try sort ( & a, & b)
475
- try sort ( & c, & d)
476
- try sort ( & a, & c)
477
- try sort ( & b, & d)
478
- return ( a, d)
479
- }
480
-
481
- // Read the elements in four-at-a-time. Some say this is more effective
482
- // than a two-at-a-time loop.
483
- while let a = iterator. next ( ) {
484
- let b = iterator. next ( ) ?? a
485
- let c = iterator. next ( ) ?? b
486
- let d = iterator. next ( ) ?? c
487
- let ( low, high) = try minAndMaxOf4 ( a, b, c, d)
447
+ var high = iterator. next ( ) ?? low
448
+ if try areInIncreasingOrder ( high, low) { swap ( & low, & high) }
488
449
if try areInIncreasingOrder ( low, lowest) { lowest = low }
489
450
if try ! areInIncreasingOrder( high, highest) { highest = high }
490
451
}
491
- #endif
452
+
492
453
return ( lowest, highest)
493
454
}
494
455
}
@@ -510,10 +471,10 @@ extension Sequence where Element: Comparable {
510
471
/// - Precondition: The sequence is finite.
511
472
///
512
473
/// - Returns: A tuple with the sequence's minimum element, followed by its
513
- /// maximum element. For either member, if there is a tie for the extreme
514
- /// value , the element chosen is unspecified. The same element may be used
515
- /// for both members if all the elements are equal . If the sequence has no
516
- /// elements, returns `nil`.
474
+ /// maximum element. If the sequence provides multiple qualifying minimum
475
+ /// elements , the first equivalent element is returned; of multiple maximum
476
+ /// elements, the last is returned . If the sequence has no elements, the
477
+ /// method returns `nil`.
517
478
///
518
479
/// - Complexity: O(*n*), where *n* is the length of the sequence.
519
480
@inlinable
0 commit comments