diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index f2ec2c3a134..78c74edbf75 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -11043,6 +11043,8 @@ public fun CharArray.max(): Char? { public inline fun > Array.maxBy(selector: (T) -> R): T? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11063,6 +11065,8 @@ public inline fun > Array.maxBy(selector: (T) -> R): public inline fun > ByteArray.maxBy(selector: (Byte) -> R): Byte? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11083,6 +11087,8 @@ public inline fun > ByteArray.maxBy(selector: (Byte) -> R): By public inline fun > ShortArray.maxBy(selector: (Short) -> R): Short? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11103,6 +11109,8 @@ public inline fun > ShortArray.maxBy(selector: (Short) -> R): public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11123,6 +11131,8 @@ public inline fun > IntArray.maxBy(selector: (Int) -> R): Int? public inline fun > LongArray.maxBy(selector: (Long) -> R): Long? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11143,6 +11153,8 @@ public inline fun > LongArray.maxBy(selector: (Long) -> R): Lo public inline fun > FloatArray.maxBy(selector: (Float) -> R): Float? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11163,6 +11175,8 @@ public inline fun > FloatArray.maxBy(selector: (Float) -> R): public inline fun > DoubleArray.maxBy(selector: (Double) -> R): Double? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11183,6 +11197,8 @@ public inline fun > DoubleArray.maxBy(selector: (Double) -> R) public inline fun > BooleanArray.maxBy(selector: (Boolean) -> R): Boolean? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11203,6 +11219,8 @@ public inline fun > BooleanArray.maxBy(selector: (Boolean) -> public inline fun > CharArray.maxBy(selector: (Char) -> R): Char? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -11488,6 +11506,8 @@ public fun CharArray.min(): Char? { public inline fun > Array.minBy(selector: (T) -> R): T? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11508,6 +11528,8 @@ public inline fun > Array.minBy(selector: (T) -> R): public inline fun > ByteArray.minBy(selector: (Byte) -> R): Byte? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11528,6 +11550,8 @@ public inline fun > ByteArray.minBy(selector: (Byte) -> R): By public inline fun > ShortArray.minBy(selector: (Short) -> R): Short? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11548,6 +11572,8 @@ public inline fun > ShortArray.minBy(selector: (Short) -> R): public inline fun > IntArray.minBy(selector: (Int) -> R): Int? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11568,6 +11594,8 @@ public inline fun > IntArray.minBy(selector: (Int) -> R): Int? public inline fun > LongArray.minBy(selector: (Long) -> R): Long? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11588,6 +11616,8 @@ public inline fun > LongArray.minBy(selector: (Long) -> R): Lo public inline fun > FloatArray.minBy(selector: (Float) -> R): Float? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11608,6 +11638,8 @@ public inline fun > FloatArray.minBy(selector: (Float) -> R): public inline fun > DoubleArray.minBy(selector: (Double) -> R): Double? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11628,6 +11660,8 @@ public inline fun > DoubleArray.minBy(selector: (Double) -> R) public inline fun > BooleanArray.minBy(selector: (Boolean) -> R): Boolean? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -11648,6 +11682,8 @@ public inline fun > BooleanArray.minBy(selector: (Boolean) -> public inline fun > CharArray.minBy(selector: (Char) -> R): Char? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] diff --git a/libraries/stdlib/common/src/generated/_Collections.kt b/libraries/stdlib/common/src/generated/_Collections.kt index f35965dc90f..e415d680bf8 100644 --- a/libraries/stdlib/common/src/generated/_Collections.kt +++ b/libraries/stdlib/common/src/generated/_Collections.kt @@ -1653,15 +1653,16 @@ public inline fun > Iterable.maxBy(selector: (T) -> R): val iterator = iterator() if (!iterator.hasNext()) return null var maxElem = iterator.next() + if (!iterator.hasNext()) return maxElem var maxValue = selector(maxElem) - while (iterator.hasNext()) { + do { val e = iterator.next() val v = selector(e) if (maxValue < v) { maxElem = e maxValue = v } - } + } while (iterator.hasNext()) return maxElem } @@ -1740,15 +1741,16 @@ public inline fun > Iterable.minBy(selector: (T) -> R): val iterator = iterator() if (!iterator.hasNext()) return null var minElem = iterator.next() + if (!iterator.hasNext()) return minElem var minValue = selector(minElem) - while (iterator.hasNext()) { + do { val e = iterator.next() val v = selector(e) if (minValue > v) { minElem = e minValue = v } - } + } while (iterator.hasNext()) return minElem } diff --git a/libraries/stdlib/common/src/generated/_Sequences.kt b/libraries/stdlib/common/src/generated/_Sequences.kt index 5e630f59d5b..5658da4c1cd 100644 --- a/libraries/stdlib/common/src/generated/_Sequences.kt +++ b/libraries/stdlib/common/src/generated/_Sequences.kt @@ -1162,15 +1162,16 @@ public inline fun > Sequence.maxBy(selector: (T) -> R): val iterator = iterator() if (!iterator.hasNext()) return null var maxElem = iterator.next() + if (!iterator.hasNext()) return maxElem var maxValue = selector(maxElem) - while (iterator.hasNext()) { + do { val e = iterator.next() val v = selector(e) if (maxValue < v) { maxElem = e maxValue = v } - } + } while (iterator.hasNext()) return maxElem } @@ -1259,15 +1260,16 @@ public inline fun > Sequence.minBy(selector: (T) -> R): val iterator = iterator() if (!iterator.hasNext()) return null var minElem = iterator.next() + if (!iterator.hasNext()) return minElem var minValue = selector(minElem) - while (iterator.hasNext()) { + do { val e = iterator.next() val v = selector(e) if (minValue > v) { minElem = e minValue = v } - } + } while (iterator.hasNext()) return minElem } diff --git a/libraries/stdlib/common/src/generated/_Strings.kt b/libraries/stdlib/common/src/generated/_Strings.kt index ff4dd113b6d..36b6cdbf903 100644 --- a/libraries/stdlib/common/src/generated/_Strings.kt +++ b/libraries/stdlib/common/src/generated/_Strings.kt @@ -1045,6 +1045,8 @@ public fun CharSequence.max(): Char? { public inline fun > CharSequence.maxBy(selector: (Char) -> R): Char? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -1091,6 +1093,8 @@ public fun CharSequence.min(): Char? { public inline fun > CharSequence.minBy(selector: (Char) -> R): Char? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 50c273ac525..aebd2efb0ec 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -4859,6 +4859,8 @@ public fun UShortArray.max(): UShort? { public inline fun > UIntArray.maxBy(selector: (UInt) -> R): UInt? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -4882,6 +4884,8 @@ public inline fun > UIntArray.maxBy(selector: (UInt) -> R): UI public inline fun > ULongArray.maxBy(selector: (ULong) -> R): ULong? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -4905,6 +4909,8 @@ public inline fun > ULongArray.maxBy(selector: (ULong) -> R): public inline fun > UByteArray.maxBy(selector: (UByte) -> R): UByte? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -4928,6 +4934,8 @@ public inline fun > UByteArray.maxBy(selector: (UByte) -> R): public inline fun > UShortArray.maxBy(selector: (UShort) -> R): UShort? { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i] @@ -5071,6 +5079,8 @@ public fun UShortArray.min(): UShort? { public inline fun > UIntArray.minBy(selector: (UInt) -> R): UInt? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -5094,6 +5104,8 @@ public inline fun > UIntArray.minBy(selector: (UInt) -> R): UI public inline fun > ULongArray.minBy(selector: (ULong) -> R): ULong? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -5117,6 +5129,8 @@ public inline fun > ULongArray.minBy(selector: (ULong) -> R): public inline fun > UByteArray.minBy(selector: (UByte) -> R): UByte? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -5140,6 +5154,8 @@ public inline fun > UByteArray.minBy(selector: (UByte) -> R): public inline fun > UShortArray.minBy(selector: (UShort) -> R): UShort? { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 9e02d23e6b4..3a4be9304bd 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -346,15 +346,16 @@ object Aggregates : TemplateGroupBase() { if (!iterator.hasNext()) return null var minElem = iterator.next() + if (!iterator.hasNext()) return minElem var minValue = selector(minElem) - while (iterator.hasNext()) { + do { val e = iterator.next() val v = selector(e) if (minValue > v) { minElem = e minValue = v } - } + } while (iterator.hasNext()) return minElem """ } @@ -363,6 +364,8 @@ object Aggregates : TemplateGroupBase() { if (isEmpty()) return null var minElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return minElem var minValue = selector(minElem) for (i in 1..lastIndex) { val e = this[i] @@ -430,15 +433,16 @@ object Aggregates : TemplateGroupBase() { if (!iterator.hasNext()) return null var maxElem = iterator.next() + if (!iterator.hasNext()) return maxElem var maxValue = selector(maxElem) - while (iterator.hasNext()) { + do { val e = iterator.next() val v = selector(e) if (maxValue < v) { maxElem = e maxValue = v } - } + } while (iterator.hasNext()) return maxElem """ } @@ -447,6 +451,8 @@ object Aggregates : TemplateGroupBase() { if (isEmpty()) return null var maxElem = this[0] + val lastIndex = this.lastIndex + if (lastIndex == 0) return maxElem var maxValue = selector(maxElem) for (i in 1..lastIndex) { val e = this[i]