Introduce new overloads of max and min to fix NaN propagation behavior on JVM.

This commit is contained in:
Ilya Gorbunov
2016-12-28 04:08:05 +03:00
parent 74331f8eaa
commit 0bf2ba8701
11 changed files with 557 additions and 61 deletions
+72
View File
@@ -9803,6 +9803,38 @@ public inline fun CharArray.forEachIndexed(action: (Int, Char) -> Unit): Unit {
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Double>.max(): Double? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Float>.max(): Float? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -9874,8 +9906,10 @@ public fun LongArray.max(): Long? {
public fun FloatArray.max(): Float? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
@@ -9887,8 +9921,10 @@ public fun FloatArray.max(): Float? {
public fun DoubleArray.max(): Double? {
if (isEmpty()) return null
var max = this[0]
if (max.isNaN()) return max
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (max < e) max = e
}
return max
@@ -10186,6 +10222,38 @@ public fun CharArray.maxWith(comparator: Comparator<in Char>): Char? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Double>.min(): Double? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Array<out Float>.min(): Float? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -10257,8 +10325,10 @@ public fun LongArray.min(): Long? {
public fun FloatArray.min(): Float? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
@@ -10270,8 +10340,10 @@ public fun FloatArray.min(): Float? {
public fun DoubleArray.min(): Double? {
if (isEmpty()) return null
var min = this[0]
if (min.isNaN()) return min
for (i in 1..lastIndex) {
val e = this[i]
if (e.isNaN()) return e
if (min > e) min = e
}
return min
@@ -1473,6 +1473,40 @@ public inline fun <T> Iterable<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Double>.max(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Float>.max(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -1520,6 +1554,40 @@ public fun <T> Iterable<T>.maxWith(comparator: Comparator<in T>): T? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Double>.min(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Iterable<Float>.min(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@@ -860,6 +860,40 @@ public inline fun <T> Sequence<T>.forEachIndexed(action: (Int, T) -> Unit): Unit
for (item in this) action(index++, item)
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Double>.max(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Float>.max(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
if (max.isNaN()) return max
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (max < e) max = e
}
return max
}
/**
* Returns the largest element or `null` if there are no elements.
*/
@@ -907,6 +941,40 @@ public fun <T> Sequence<T>.maxWith(comparator: Comparator<in T>): T? {
return max
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Double>.min(): Double? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/
@SinceKotlin("1.1")
public fun Sequence<Float>.min(): Float? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
if (min.isNaN()) return min
while (iterator.hasNext()) {
val e = iterator.next()
if (e.isNaN()) return e
if (min > e) min = e
}
return min
}
/**
* Returns the smallest element or `null` if there are no elements.
*/