standard library: 'max' and 'min' functions reimplemented to avoid unnecessary null checks

(as proposed for minBy/maxBy in https://github.com/JetBrains/kotlin/pull/324)
This commit is contained in:
nik
2013-12-02 23:40:02 +04:00
committed by Evgeny Gerashchenko
parent ba96538a14
commit f439604b57
12 changed files with 218 additions and 128 deletions
+12 -10
View File
@@ -230,11 +230,12 @@ public inline fun <T, R, C: MutableCollection<in R>> Array<out T>.mapTo(result:
* Returns the largest element or null if there are no elements
*/
public fun <T: Comparable<T>> Array<out T>.max() : T? {
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -262,11 +263,12 @@ public inline fun <R: Comparable<R>, T: Any> Array<out T>.maxBy(f: (T) -> R) : T
* Returns the smallest element or null if there are no elements
*/
public fun <T: Comparable<T>> Array<out T>.min() : T? {
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> ByteArray.mapTo(result: C, tra
* Returns the largest element or null if there are no elements
*/
public fun ByteArray.max() : Byte? {
var max: Byte? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> ByteArray.maxBy(f: (Byte) -> R) : Byte? {
* Returns the smallest element or null if there are no elements
*/
public fun ByteArray.min() : Byte? {
var min: Byte? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> DoubleArray.mapTo(result: C, t
* Returns the largest element or null if there are no elements
*/
public fun DoubleArray.max() : Double? {
var max: Double? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> DoubleArray.maxBy(f: (Double) -> R) : Doubl
* Returns the smallest element or null if there are no elements
*/
public fun DoubleArray.min() : Double? {
var min: Double? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> FloatArray.mapTo(result: C, tr
* Returns the largest element or null if there are no elements
*/
public fun FloatArray.max() : Float? {
var max: Float? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> FloatArray.maxBy(f: (Float) -> R) : Float?
* Returns the smallest element or null if there are no elements
*/
public fun FloatArray.min() : Float? {
var min: Float? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> IntArray.mapTo(result: C, tran
* Returns the largest element or null if there are no elements
*/
public fun IntArray.max() : Int? {
var max: Int? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> IntArray.maxBy(f: (Int) -> R) : Int? {
* Returns the smallest element or null if there are no elements
*/
public fun IntArray.min() : Int? {
var min: Int? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
+14 -10
View File
@@ -216,11 +216,13 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.mapTo(result: C
* Returns the largest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterable<T>.max() : T? {
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (max < e) max = e
}
return max
}
@@ -249,11 +251,13 @@ public inline fun <R: Comparable<R>, T: Any> Iterable<T>.maxBy(f: (T) -> R) : T?
* Returns the smallest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterable<T>.min() : T? {
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
val iterator = iterator()
if (!iterator.hasNext()) return null
var min = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -216,11 +216,12 @@ public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.mapTo(result: C
* Returns the largest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterator<T>.max() : T? {
var max: T? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (!hasNext()) return null
var max = next()
while (hasNext()) {
val e = next()
if (max < e) max = e
}
return max
}
@@ -248,11 +249,12 @@ public inline fun <R: Comparable<R>, T: Any> Iterator<T>.maxBy(f: (T) -> R) : T?
* Returns the smallest element or null if there are no elements
*/
public fun <T: Comparable<T>> Iterator<T>.min() : T? {
var min: T? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (!hasNext()) return null
var min = next()
while (hasNext()) {
val e = next()
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> LongArray.mapTo(result: C, tra
* Returns the largest element or null if there are no elements
*/
public fun LongArray.max() : Long? {
var max: Long? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> LongArray.maxBy(f: (Long) -> R) : Long? {
* Returns the smallest element or null if there are no elements
*/
public fun LongArray.min() : Long? {
var min: Long? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
+12 -10
View File
@@ -229,11 +229,12 @@ public inline fun <R, C: MutableCollection<in R>> ShortArray.mapTo(result: C, tr
* Returns the largest element or null if there are no elements
*/
public fun ShortArray.max() : Short? {
var max: Short? = null
for (e in this) {
if (max == null || max!! < e) {
max = e
}
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
@@ -261,11 +262,12 @@ public inline fun <R: Comparable<R>> ShortArray.maxBy(f: (Short) -> R) : Short?
* Returns the smallest element or null if there are no elements
*/
public fun ShortArray.min() : Short? {
var min: Short? = null
for (e in this) {
if (min == null || min!! > e) {
min = e
}
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}