diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 338c54bb665..c013faef20b 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -230,11 +230,12 @@ public inline fun > Array.mapTo(result: * Returns the largest element or null if there are no elements */ public fun > Array.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 , T: Any> Array.maxBy(f: (T) -> R) : T * Returns the smallest element or null if there are no elements */ public fun > Array.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 } diff --git a/libraries/stdlib/src/generated/_ByteArrays.kt b/libraries/stdlib/src/generated/_ByteArrays.kt index 543fa827a6a..d6d3643803a 100644 --- a/libraries/stdlib/src/generated/_ByteArrays.kt +++ b/libraries/stdlib/src/generated/_ByteArrays.kt @@ -229,11 +229,12 @@ public inline fun > 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 > 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 } diff --git a/libraries/stdlib/src/generated/_DoubleArrays.kt b/libraries/stdlib/src/generated/_DoubleArrays.kt index 7061c40cc2e..c2fde88c8b5 100644 --- a/libraries/stdlib/src/generated/_DoubleArrays.kt +++ b/libraries/stdlib/src/generated/_DoubleArrays.kt @@ -229,11 +229,12 @@ public inline fun > 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 > 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 } diff --git a/libraries/stdlib/src/generated/_FloatArrays.kt b/libraries/stdlib/src/generated/_FloatArrays.kt index 3ba3b4e4cd5..9f807e07ee7 100644 --- a/libraries/stdlib/src/generated/_FloatArrays.kt +++ b/libraries/stdlib/src/generated/_FloatArrays.kt @@ -229,11 +229,12 @@ public inline fun > 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 > 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 } diff --git a/libraries/stdlib/src/generated/_IntArrays.kt b/libraries/stdlib/src/generated/_IntArrays.kt index bf58f4456f8..fe768a1a227 100644 --- a/libraries/stdlib/src/generated/_IntArrays.kt +++ b/libraries/stdlib/src/generated/_IntArrays.kt @@ -229,11 +229,12 @@ public inline fun > 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 > 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 } diff --git a/libraries/stdlib/src/generated/_Iterables.kt b/libraries/stdlib/src/generated/_Iterables.kt index c3fb27e13fd..a5999f15d1d 100644 --- a/libraries/stdlib/src/generated/_Iterables.kt +++ b/libraries/stdlib/src/generated/_Iterables.kt @@ -216,11 +216,13 @@ public inline fun > Iterable.mapTo(result: C * Returns the largest element or null if there are no elements */ public fun > Iterable.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 , T: Any> Iterable.maxBy(f: (T) -> R) : T? * Returns the smallest element or null if there are no elements */ public fun > Iterable.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 } diff --git a/libraries/stdlib/src/generated/_Iterators.kt b/libraries/stdlib/src/generated/_Iterators.kt index 9cdcacd70eb..f23b5f75f53 100644 --- a/libraries/stdlib/src/generated/_Iterators.kt +++ b/libraries/stdlib/src/generated/_Iterators.kt @@ -216,11 +216,12 @@ public inline fun > Iterator.mapTo(result: C * Returns the largest element or null if there are no elements */ public fun > Iterator.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 , T: Any> Iterator.maxBy(f: (T) -> R) : T? * Returns the smallest element or null if there are no elements */ public fun > Iterator.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 } diff --git a/libraries/stdlib/src/generated/_LongArrays.kt b/libraries/stdlib/src/generated/_LongArrays.kt index 4f5d996d3a9..09a01e56d48 100644 --- a/libraries/stdlib/src/generated/_LongArrays.kt +++ b/libraries/stdlib/src/generated/_LongArrays.kt @@ -229,11 +229,12 @@ public inline fun > 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 > 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 } diff --git a/libraries/stdlib/src/generated/_ShortArrays.kt b/libraries/stdlib/src/generated/_ShortArrays.kt index ebbca31f679..0a868d43543 100644 --- a/libraries/stdlib/src/generated/_ShortArrays.kt +++ b/libraries/stdlib/src/generated/_ShortArrays.kt @@ -229,11 +229,12 @@ public inline fun > 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 > 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 } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt index ed5ccfe92ba..476c4e1a28e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Commons.kt @@ -416,44 +416,6 @@ fun commons(): ArrayList { } } - templates add f("min()") { - doc = "Returns the smallest element or null if there are no elements" - returns("T?") - absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251) - typeParam("T: Comparable") - isInline = false - body { - """ - var min: T? = null - for (e in this) { - if (min == null || min!! > e) { - min = e - } - } - return min - """ - } - } - - templates add f("max()") { - doc = "Returns the largest element or null if there are no elements" - returns("T?") - absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251) - typeParam("T: Comparable") - isInline = false - body { - """ - var max: T? = null - for (e in this) { - if (max == null || max!! < e) { - max = e - } - } - return max - """ - } - } - templates add f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { isInline = false doc = diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt index 589e80691bc..61c0dd11957 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt @@ -146,6 +146,76 @@ fun iterables(): ArrayList { } } + templates add f("min()") { + doc = "Returns the smallest element or null if there are no elements" + returns("T?") + absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251) + typeParam("T: Comparable") + isInline = false + Iterables.body { + """ + 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 + """ + } + listOf(Arrays, PrimitiveArrays).forEach { + it.body { + """ + if (isEmpty()) return null + + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min + """ + } + } + } + + templates add f("max()") { + doc = "Returns the largest element or null if there are no elements" + returns("T?") + absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251) + typeParam("T: Comparable") + isInline = false + Iterables.body { + """ + 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 + """ + } + listOf(Arrays, PrimitiveArrays).forEach { + it.body { + """ + if (isEmpty()) return null + + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max + """ + } + } + } + templates add f("minBy(f: (T) -> R)") { doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements" typeParam("R: Comparable") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt index 34c3e6c39cd..0f0895d1797 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt @@ -132,6 +132,44 @@ fun iterators(): List { } } + templates add f("min()") { + doc = "Returns the smallest element or null if there are no elements" + returns("T?") + typeParam("T: Comparable") + isInline = false + body { + """ + if (!hasNext()) return null + + var min = next() + while (hasNext()) { + val e = next() + if (min > e) min = e + } + return min + """ + } + } + + templates add f("max()") { + doc = "Returns the largest element or null if there are no elements" + returns("T?") + typeParam("T: Comparable") + isInline = false + body { + """ + if (!hasNext()) return null + + var max = next() + while (hasNext()) { + val e = next() + if (max < e) max = e + } + return max + """ + } + } + templates add f("minBy(f: (T) -> R)") { doc = "Returns the first element yielding the smallest value of the given function or null if there are no elements" typeParam("R: Comparable")