From 220f2d1c890602ba24d4eb0680b335896e2124a7 Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Thu, 17 Dec 2015 13:59:48 +1100 Subject: [PATCH 1/5] [stdlib] Refactor min(_:_:) --- stdlib/public/core/Algorithm.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/stdlib/public/core/Algorithm.swift b/stdlib/public/core/Algorithm.swift index 15b2fd94f918a..f001e2757dcad 100644 --- a/stdlib/public/core/Algorithm.swift +++ b/stdlib/public/core/Algorithm.swift @@ -46,11 +46,7 @@ public func find< /// Returns the lesser of `x` and `y`. @warn_unused_result public func min(x: T, _ y: T) -> T { - var r = x - if y < x { - r = y - } - return r + return y < x ? y : x } /// Returns the least argument passed. From 370f4f0de4b5d99de8a20bcc788ba5e12fd146c5 Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Thu, 17 Dec 2015 14:00:03 +1100 Subject: [PATCH 2/5] [stdlib] Refactor min(_:_:_:_:) --- stdlib/public/core/Algorithm.swift | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/stdlib/public/core/Algorithm.swift b/stdlib/public/core/Algorithm.swift index f001e2757dcad..0b238f5ceadb6 100644 --- a/stdlib/public/core/Algorithm.swift +++ b/stdlib/public/core/Algorithm.swift @@ -52,17 +52,9 @@ public func min(x: T, _ y: T) -> T { /// Returns the least argument passed. @warn_unused_result public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { - var r = x - if y < x { - r = y - } - if z < r { - r = z - } - for t in rest { - if t < r { - r = t - } + var r = min(min(x, y), z) + for t in rest where t < r { + r = t } return r } From 75a6609c6154c9c9f9d727c94e0f8921357f17c3 Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Thu, 17 Dec 2015 14:07:58 +1100 Subject: [PATCH 3/5] [stdlib] Unabbreviate local variable names --- stdlib/public/core/Algorithm.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/public/core/Algorithm.swift b/stdlib/public/core/Algorithm.swift index 0b238f5ceadb6..357684b06fe57 100644 --- a/stdlib/public/core/Algorithm.swift +++ b/stdlib/public/core/Algorithm.swift @@ -52,11 +52,11 @@ public func min(x: T, _ y: T) -> T { /// Returns the least argument passed. @warn_unused_result public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { - var r = min(min(x, y), z) - for t in rest where t < r { - r = t + var minValue = min(min(x, y), z) + for value in rest where value < minValue { + minValue = value } - return r + return minValue } /// Returns the greater of `x` and `y`. @@ -68,11 +68,11 @@ public func max(x: T, _ y: T) -> T { /// Returns the greatest argument passed. @warn_unused_result public func max(x: T, _ y: T, _ z: T, _ rest: T...) -> T { - var r = max(max(x, y), z) - for t in rest where t >= r { - r = t + var maxValue = max(max(x, y), z) + for value in rest where value >= maxValue { + maxValue = value } - return r + return maxValue } /// Returns the result of slicing `elements` into sub-sequences that From fe478b7633d4630e5382c85399e6c70375c167f1 Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Thu, 17 Dec 2015 14:17:50 +1100 Subject: [PATCH 4/5] [stdlib] Add sort stability comment to min()/max() --- stdlib/public/core/Algorithm.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stdlib/public/core/Algorithm.swift b/stdlib/public/core/Algorithm.swift index 357684b06fe57..d081342f8264c 100644 --- a/stdlib/public/core/Algorithm.swift +++ b/stdlib/public/core/Algorithm.swift @@ -46,6 +46,10 @@ public func find< /// Returns the lesser of `x` and `y`. @warn_unused_result public func min(x: T, _ y: T) -> T { + // In case `x == y` we pick `x`. + // This preserves any pre-existing order in case `T` has identity, + // which is important for e.g. the stability of sorting algorithms. + // `(min(x, y), max(x, y))` should return `(x, y)` in case `x == y`. return y < x ? y : x } @@ -53,6 +57,7 @@ public func min(x: T, _ y: T) -> T { @warn_unused_result public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { var minValue = min(min(x, y), z) + // In case `value == minValue`, we pick `minValue`. See min(_:_:). for value in rest where value < minValue { minValue = value } @@ -62,6 +67,7 @@ public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { /// Returns the greater of `x` and `y`. @warn_unused_result public func max(x: T, _ y: T) -> T { + // In case `x == y`, we pick `y`. See min(_:_:). return y >= x ? y : x } @@ -69,6 +75,7 @@ public func max(x: T, _ y: T) -> T { @warn_unused_result public func max(x: T, _ y: T, _ z: T, _ rest: T...) -> T { var maxValue = max(max(x, y), z) + // In case `value == maxValue`, we pick `value`. See min(_:_:). for value in rest where value >= maxValue { maxValue = value } From a61839d69699a4e129efdaf1ebb35e1474be05c7 Mon Sep 17 00:00:00 2001 From: Patrick Pijnappel Date: Fri, 18 Dec 2015 09:51:34 +1100 Subject: [PATCH 5/5] [stdlib] Add ordering guarantee to min() and max() docs --- stdlib/public/core/Algorithm.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/stdlib/public/core/Algorithm.swift b/stdlib/public/core/Algorithm.swift index d081342f8264c..a3fe61253d54a 100644 --- a/stdlib/public/core/Algorithm.swift +++ b/stdlib/public/core/Algorithm.swift @@ -44,6 +44,8 @@ public func find< } /// Returns the lesser of `x` and `y`. +/// +/// If `x == y`, returns `x`. @warn_unused_result public func min(x: T, _ y: T) -> T { // In case `x == y` we pick `x`. @@ -54,6 +56,8 @@ public func min(x: T, _ y: T) -> T { } /// Returns the least argument passed. +/// +/// If there are multiple equal least arguments, returns the first one. @warn_unused_result public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { var minValue = min(min(x, y), z) @@ -65,6 +69,8 @@ public func min(x: T, _ y: T, _ z: T, _ rest: T...) -> T { } /// Returns the greater of `x` and `y`. +/// +/// If `x == y`, returns `y`. @warn_unused_result public func max(x: T, _ y: T) -> T { // In case `x == y`, we pick `y`. See min(_:_:). @@ -72,6 +78,8 @@ public func max(x: T, _ y: T) -> T { } /// Returns the greatest argument passed. +/// +/// If there are multiple equal greatest arguments, returns the last one. @warn_unused_result public func max(x: T, _ y: T, _ z: T, _ rest: T...) -> T { var maxValue = max(max(x, y), z)