From 1fca1cc6079ac297fdaa6a542affeb3de874e0b6 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 5 Feb 2016 16:43:06 +0300 Subject: [PATCH] Use foreach loop instead of directly taking iterator in sum and average. Use indexed loop for arrays in reduce and reduceIndexed. #KT-10579 Fixed --- libraries/stdlib/src/generated/_Arrays.kt | 327 ++++++++---------- .../stdlib/src/generated/_Collections.kt | 62 ++-- libraries/stdlib/src/generated/_Sequences.kt | 60 ++-- libraries/stdlib/src/generated/_Strings.kt | 23 +- .../src/templates/Aggregates.kt | 123 ++++--- .../src/templates/Numeric.kt | 10 +- 6 files changed, 289 insertions(+), 316 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 85bed38ae7d..2febd013e98 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -9959,11 +9959,11 @@ public inline fun CharArray.none(predicate: (Char) -> Boolean): Boolean { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun Array.reduce(operation: (S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -9972,11 +9972,11 @@ public inline fun Array.reduce(operation: (S, T) -> S): S { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte): Byte { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -9985,11 +9985,11 @@ public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte): Byte { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -9998,11 +9998,11 @@ public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun IntArray.reduce(operation: (Int, Int) -> Int): Int { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -10011,11 +10011,11 @@ public inline fun IntArray.reduce(operation: (Int, Int) -> Int): Int { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun LongArray.reduce(operation: (Long, Long) -> Long): Long { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -10024,11 +10024,11 @@ public inline fun LongArray.reduce(operation: (Long, Long) -> Long): Long { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun FloatArray.reduce(operation: (Float, Float) -> Float): Float { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -10037,11 +10037,11 @@ public inline fun FloatArray.reduce(operation: (Float, Float) -> Float): Float { * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double): Double { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -10050,11 +10050,11 @@ public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double): Dou * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean): Boolean { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -10063,11 +10063,11 @@ public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean): * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element. */ public inline fun CharArray.reduce(operation: (Char, Char) -> Char): Char { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -10077,12 +10077,11 @@ public inline fun CharArray.reduce(operation: (Char, Char) -> Char): Char { * to current accumulator value and each element with its index in the original array. */ public inline fun Array.reduceIndexed(operation: (Int, S, T) -> S): S { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator: S = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10092,12 +10091,11 @@ public inline fun Array.reduceIndexed(operation: (Int, S, T) -> * to current accumulator value and each element with its index in the original array. */ public inline fun ByteArray.reduceIndexed(operation: (Int, Byte, Byte) -> Byte): Byte { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10107,12 +10105,11 @@ public inline fun ByteArray.reduceIndexed(operation: (Int, Byte, Byte) -> Byte): * to current accumulator value and each element with its index in the original array. */ public inline fun ShortArray.reduceIndexed(operation: (Int, Short, Short) -> Short): Short { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10122,12 +10119,11 @@ public inline fun ShortArray.reduceIndexed(operation: (Int, Short, Short) -> Sho * to current accumulator value and each element with its index in the original array. */ public inline fun IntArray.reduceIndexed(operation: (Int, Int, Int) -> Int): Int { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10137,12 +10133,11 @@ public inline fun IntArray.reduceIndexed(operation: (Int, Int, Int) -> Int): Int * to current accumulator value and each element with its index in the original array. */ public inline fun LongArray.reduceIndexed(operation: (Int, Long, Long) -> Long): Long { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10152,12 +10147,11 @@ public inline fun LongArray.reduceIndexed(operation: (Int, Long, Long) -> Long): * to current accumulator value and each element with its index in the original array. */ public inline fun FloatArray.reduceIndexed(operation: (Int, Float, Float) -> Float): Float { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10167,12 +10161,11 @@ public inline fun FloatArray.reduceIndexed(operation: (Int, Float, Float) -> Flo * to current accumulator value and each element with its index in the original array. */ public inline fun DoubleArray.reduceIndexed(operation: (Int, Double, Double) -> Double): Double { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10182,12 +10175,11 @@ public inline fun DoubleArray.reduceIndexed(operation: (Int, Double, Double) -> * to current accumulator value and each element with its index in the original array. */ public inline fun BooleanArray.reduceIndexed(operation: (Int, Boolean, Boolean) -> Boolean): Boolean { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10197,12 +10189,11 @@ public inline fun BooleanArray.reduceIndexed(operation: (Int, Boolean, Boolean) * to current accumulator value and each element with its index in the original array. */ public inline fun CharArray.reduceIndexed(operation: (Int, Char, Char) -> Char): Char { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty array can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -10330,7 +10321,7 @@ public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char { */ public inline fun Array.reduceRightIndexed(operation: (Int, T, S) -> S): S { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator: S = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10345,7 +10336,7 @@ public inline fun Array.reduceRightIndexed(operation: (Int, T, */ public inline fun ByteArray.reduceRightIndexed(operation: (Int, Byte, Byte) -> Byte): Byte { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10360,7 +10351,7 @@ public inline fun ByteArray.reduceRightIndexed(operation: (Int, Byte, Byte) -> B */ public inline fun ShortArray.reduceRightIndexed(operation: (Int, Short, Short) -> Short): Short { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10375,7 +10366,7 @@ public inline fun ShortArray.reduceRightIndexed(operation: (Int, Short, Short) - */ public inline fun IntArray.reduceRightIndexed(operation: (Int, Int, Int) -> Int): Int { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10390,7 +10381,7 @@ public inline fun IntArray.reduceRightIndexed(operation: (Int, Int, Int) -> Int) */ public inline fun LongArray.reduceRightIndexed(operation: (Int, Long, Long) -> Long): Long { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10405,7 +10396,7 @@ public inline fun LongArray.reduceRightIndexed(operation: (Int, Long, Long) -> L */ public inline fun FloatArray.reduceRightIndexed(operation: (Int, Float, Float) -> Float): Float { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10420,7 +10411,7 @@ public inline fun FloatArray.reduceRightIndexed(operation: (Int, Float, Float) - */ public inline fun DoubleArray.reduceRightIndexed(operation: (Int, Double, Double) -> Double): Double { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10435,7 +10426,7 @@ public inline fun DoubleArray.reduceRightIndexed(operation: (Int, Double, Double */ public inline fun BooleanArray.reduceRightIndexed(operation: (Int, Boolean, Boolean) -> Boolean): Boolean { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -10450,7 +10441,7 @@ public inline fun BooleanArray.reduceRightIndexed(operation: (Int, Boolean, Bool */ public inline fun CharArray.reduceRightIndexed(operation: (Int, Char, Char) -> Char): Char { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -12876,11 +12867,10 @@ public fun CharArray.toTypedArray(): Array { */ @kotlin.jvm.JvmName("averageOfByte") public fun Array.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12891,11 +12881,10 @@ public fun Array.average(): Double { */ @kotlin.jvm.JvmName("averageOfShort") public fun Array.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12906,11 +12895,10 @@ public fun Array.average(): Double { */ @kotlin.jvm.JvmName("averageOfInt") public fun Array.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12921,11 +12909,10 @@ public fun Array.average(): Double { */ @kotlin.jvm.JvmName("averageOfLong") public fun Array.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12936,11 +12923,10 @@ public fun Array.average(): Double { */ @kotlin.jvm.JvmName("averageOfFloat") public fun Array.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12951,11 +12937,10 @@ public fun Array.average(): Double { */ @kotlin.jvm.JvmName("averageOfDouble") public fun Array.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12965,11 +12950,10 @@ public fun Array.average(): Double { * Returns an average value of elements in the array. */ public fun ByteArray.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12979,11 +12963,10 @@ public fun ByteArray.average(): Double { * Returns an average value of elements in the array. */ public fun ShortArray.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -12993,11 +12976,10 @@ public fun ShortArray.average(): Double { * Returns an average value of elements in the array. */ public fun IntArray.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -13007,11 +12989,10 @@ public fun IntArray.average(): Double { * Returns an average value of elements in the array. */ public fun LongArray.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -13021,11 +13002,10 @@ public fun LongArray.average(): Double { * Returns an average value of elements in the array. */ public fun FloatArray.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -13035,11 +13015,10 @@ public fun FloatArray.average(): Double { * Returns an average value of elements in the array. */ public fun DoubleArray.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -13050,10 +13029,9 @@ public fun DoubleArray.average(): Double { */ @kotlin.jvm.JvmName("sumOfByte") public fun Array.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13063,10 +13041,9 @@ public fun Array.sum(): Int { */ @kotlin.jvm.JvmName("sumOfShort") public fun Array.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13076,10 +13053,9 @@ public fun Array.sum(): Int { */ @kotlin.jvm.JvmName("sumOfInt") public fun Array.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13089,10 +13065,9 @@ public fun Array.sum(): Int { */ @kotlin.jvm.JvmName("sumOfLong") public fun Array.sum(): Long { - val iterator = iterator() var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13102,10 +13077,9 @@ public fun Array.sum(): Long { */ @kotlin.jvm.JvmName("sumOfFloat") public fun Array.sum(): Float { - val iterator = iterator() var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13115,10 +13089,9 @@ public fun Array.sum(): Float { */ @kotlin.jvm.JvmName("sumOfDouble") public fun Array.sum(): Double { - val iterator = iterator() var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13127,10 +13100,9 @@ public fun Array.sum(): Double { * Returns the sum of all elements in the array. */ public fun ByteArray.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13139,10 +13111,9 @@ public fun ByteArray.sum(): Int { * Returns the sum of all elements in the array. */ public fun ShortArray.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13151,10 +13122,9 @@ public fun ShortArray.sum(): Int { * Returns the sum of all elements in the array. */ public fun IntArray.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13163,10 +13133,9 @@ public fun IntArray.sum(): Int { * Returns the sum of all elements in the array. */ public fun LongArray.sum(): Long { - val iterator = iterator() var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13175,10 +13144,9 @@ public fun LongArray.sum(): Long { * Returns the sum of all elements in the array. */ public fun FloatArray.sum(): Float { - val iterator = iterator() var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -13187,10 +13155,9 @@ public fun FloatArray.sum(): Float { * Returns the sum of all elements in the array. */ public fun DoubleArray.sum(): Double { - val iterator = iterator() var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index ebff7bd21f4..9931f24d5eb 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -1531,7 +1531,7 @@ public inline fun List.reduceRight(operation: (T, S) -> S): S { */ public inline fun List.reduceRightIndexed(operation: (Int, T, S) -> S): S { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty list can't be reduced.") var accumulator: S = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) @@ -1883,11 +1883,10 @@ public fun , R> Iterable<*>.filterIsInstanceTo(desti */ @kotlin.jvm.JvmName("averageOfByte") public fun Iterable.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1898,11 +1897,10 @@ public fun Iterable.average(): Double { */ @kotlin.jvm.JvmName("averageOfShort") public fun Iterable.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1913,11 +1911,10 @@ public fun Iterable.average(): Double { */ @kotlin.jvm.JvmName("averageOfInt") public fun Iterable.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1928,11 +1925,10 @@ public fun Iterable.average(): Double { */ @kotlin.jvm.JvmName("averageOfLong") public fun Iterable.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1943,11 +1939,10 @@ public fun Iterable.average(): Double { */ @kotlin.jvm.JvmName("averageOfFloat") public fun Iterable.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1958,11 +1953,10 @@ public fun Iterable.average(): Double { */ @kotlin.jvm.JvmName("averageOfDouble") public fun Iterable.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1973,10 +1967,9 @@ public fun Iterable.average(): Double { */ @kotlin.jvm.JvmName("sumOfByte") public fun Iterable.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1986,10 +1979,9 @@ public fun Iterable.sum(): Int { */ @kotlin.jvm.JvmName("sumOfShort") public fun Iterable.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1999,10 +1991,9 @@ public fun Iterable.sum(): Int { */ @kotlin.jvm.JvmName("sumOfInt") public fun Iterable.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -2012,10 +2003,9 @@ public fun Iterable.sum(): Int { */ @kotlin.jvm.JvmName("sumOfLong") public fun Iterable.sum(): Long { - val iterator = iterator() var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -2025,10 +2015,9 @@ public fun Iterable.sum(): Long { */ @kotlin.jvm.JvmName("sumOfFloat") public fun Iterable.sum(): Float { - val iterator = iterator() var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -2038,10 +2027,9 @@ public fun Iterable.sum(): Float { */ @kotlin.jvm.JvmName("sumOfDouble") public fun Iterable.sum(): Double { - val iterator = iterator() var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index bb316f3e9ee..6f048e9f4fc 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -1187,11 +1187,10 @@ public fun , R> Sequence<*>.filterIsInstanceTo(desti */ @kotlin.jvm.JvmName("averageOfByte") public fun Sequence.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1202,11 +1201,10 @@ public fun Sequence.average(): Double { */ @kotlin.jvm.JvmName("averageOfShort") public fun Sequence.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1217,11 +1215,10 @@ public fun Sequence.average(): Double { */ @kotlin.jvm.JvmName("averageOfInt") public fun Sequence.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1232,11 +1229,10 @@ public fun Sequence.average(): Double { */ @kotlin.jvm.JvmName("averageOfLong") public fun Sequence.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1247,11 +1243,10 @@ public fun Sequence.average(): Double { */ @kotlin.jvm.JvmName("averageOfFloat") public fun Sequence.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1262,11 +1257,10 @@ public fun Sequence.average(): Double { */ @kotlin.jvm.JvmName("averageOfDouble") public fun Sequence.average(): Double { - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count @@ -1277,10 +1271,9 @@ public fun Sequence.average(): Double { */ @kotlin.jvm.JvmName("sumOfByte") public fun Sequence.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1290,10 +1283,9 @@ public fun Sequence.sum(): Int { */ @kotlin.jvm.JvmName("sumOfShort") public fun Sequence.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1303,10 +1295,9 @@ public fun Sequence.sum(): Int { */ @kotlin.jvm.JvmName("sumOfInt") public fun Sequence.sum(): Int { - val iterator = iterator() var sum: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1316,10 +1307,9 @@ public fun Sequence.sum(): Int { */ @kotlin.jvm.JvmName("sumOfLong") public fun Sequence.sum(): Long { - val iterator = iterator() var sum: Long = 0L - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1329,10 +1319,9 @@ public fun Sequence.sum(): Long { */ @kotlin.jvm.JvmName("sumOfFloat") public fun Sequence.sum(): Float { - val iterator = iterator() var sum: Float = 0.0f - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } @@ -1342,10 +1331,9 @@ public fun Sequence.sum(): Float { */ @kotlin.jvm.JvmName("sumOfDouble") public fun Sequence.sum(): Double { - val iterator = iterator() var sum: Double = 0.0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum } diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index 92080c148d1..1a3c469e313 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -964,11 +964,11 @@ public inline fun CharSequence.none(predicate: (Char) -> Boolean): Boolean { * Accumulates value starting with the first character and applying [operation] from left to right to current accumulator value and each character. */ public inline fun CharSequence.reduce(operation: (Char, Char) -> Char): Char { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty char sequence can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) } return accumulator } @@ -978,12 +978,11 @@ public inline fun CharSequence.reduce(operation: (Char, Char) -> Char): Char { * to current accumulator value and each character with its index in the original char sequence. */ public inline fun CharSequence.reduceIndexed(operation: (Int, Char, Char) -> Char): Char { - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + if (isEmpty()) + throw UnsupportedOperationException("Empty char sequence can't be reduced.") + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) } return accumulator } @@ -1007,7 +1006,7 @@ public inline fun CharSequence.reduceRight(operation: (Char, Char) -> Char): Cha */ public inline fun CharSequence.reduceRightIndexed(operation: (Int, Char, Char) -> Char): Char { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(index, get(index), accumulator) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 1a1f5522654..c6ac3dfe0f0 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -436,8 +436,7 @@ fun aggregates(): List { templates add f("reduceIndexed(operation: (Int, T, T) -> T)") { inline(true) - include(CharSequences) - exclude(ArraysOfObjects, Iterables, Sequences) + only(ArraysOfPrimitives, CharSequences) doc { f -> """ @@ -446,18 +445,19 @@ fun aggregates(): List { """ } returns("T") - body { - """ - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + body { f -> + with (DocExtensions) { + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") - var index = 1 - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(index++, accumulator, iterator.next()) + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator + """ } - return accumulator - """ } } @@ -487,6 +487,20 @@ fun aggregates(): List { return accumulator """ } + body(ArraysOfObjects) { f -> + with (DocExtensions) { + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(index, accumulator, this[index]) + } + return accumulator + """ + } + } } templates add f("reduceRightIndexed(operation: (Int, T, T) -> T)") { @@ -500,19 +514,21 @@ fun aggregates(): List { """ } returns("T") - body { - """ - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + body { f -> + with (DocExtensions) { + """ + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") - var accumulator = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index + var accumulator = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + + return accumulator + """ } - - return accumulator - """ } } @@ -529,40 +545,43 @@ fun aggregates(): List { typeParam("S") typeParam("T: S") returns("S") - body { - """ - var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + body { f -> + with (DocExtensions) { + """ + var index = lastIndex + if (index < 0) throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") - var accumulator: S = get(index--) - while (index >= 0) { - accumulator = operation(index, get(index), accumulator) - --index + var accumulator: S = get(index--) + while (index >= 0) { + accumulator = operation(index, get(index), accumulator) + --index + } + + return accumulator + """ } - - return accumulator - """ } } templates add f("reduce(operation: (T, T) -> T)") { inline(true) - include(CharSequences) - exclude(ArraysOfObjects, Iterables, Sequences) + only(ArraysOfPrimitives, CharSequences) doc { f -> "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." } returns("T") - body { - """ - val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + body { f -> + with (DocExtensions) { + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") - var accumulator = iterator.next() - while (iterator.hasNext()) { - accumulator = operation(accumulator, iterator.next()) + var accumulator = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator + """ } - return accumulator - """ } } @@ -586,6 +605,20 @@ fun aggregates(): List { return accumulator """ } + body(ArraysOfObjects) { f -> + with (DocExtensions) { + """ + if (isEmpty()) + throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.") + + var accumulator: S = this[0] + for (index in 1..lastIndex) { + accumulator = operation(accumulator, this[index]) + } + return accumulator + """ + } + } } templates add f("reduceRight(operation: (T, T) -> T)") { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt index c2ea020be3c..2135000cae7 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Numeric.kt @@ -13,10 +13,9 @@ fun numeric(): List { platformName("sumOf") body { """ - val iterator = iterator() var sum: SUM = ZERO - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element } return sum """ @@ -31,11 +30,10 @@ fun numeric(): List { platformName("averageOf") body { """ - val iterator = iterator() var sum: Double = 0.0 var count: Int = 0 - while (iterator.hasNext()) { - sum += iterator.next() + for (element in this) { + sum += element count += 1 } return if (count == 0) 0.0 else sum / count