@@ -46,13 +46,18 @@ public func find<
46
46
/// Returns the lesser of `x` and `y`.
47
47
@warn_unused_result
48
48
public func min< T : Comparable > ( x: T , _ y: T ) -> T {
49
+ // In case `x == y` we pick `x`.
50
+ // This preserves any pre-existing order in case `T` has identity,
51
+ // which is important for e.g. the stability of sorting algorithms.
52
+ // `(min(x, y), max(x, y))` should return `(x, y)` in case `x == y`.
49
53
return y < x ? y : x
50
54
}
51
55
52
56
/// Returns the least argument passed.
53
57
@warn_unused_result
54
58
public func min< T : Comparable > ( x: T , _ y: T , _ z: T , _ rest: T ... ) -> T {
55
59
var minValue = min ( min ( x, y) , z)
60
+ // In case `value == minValue`, we pick `minValue`. See min(_:_:).
56
61
for value in rest where value < minValue {
57
62
minValue = value
58
63
}
@@ -62,13 +67,15 @@ public func min<T : Comparable>(x: T, _ y: T, _ z: T, _ rest: T...) -> T {
62
67
/// Returns the greater of `x` and `y`.
63
68
@warn_unused_result
64
69
public func max< T : Comparable > ( x: T , _ y: T ) -> T {
70
+ // In case `x == y`, we pick `y`. See min(_:_:).
65
71
return y >= x ? y : x
66
72
}
67
73
68
74
/// Returns the greatest argument passed.
69
75
@warn_unused_result
70
76
public func max< T : Comparable > ( x: T , _ y: T , _ z: T , _ rest: T ... ) -> T {
71
77
var maxValue = max ( max ( x, y) , z)
78
+ // In case `value == maxValue`, we pick `value`. See min(_:_:).
72
79
for value in rest where value >= maxValue {
73
80
maxValue = value
74
81
}
0 commit comments