diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index 0101a25914b..247f9c0ebdd 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -14,11 +14,10 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun Array.merge(array: Array, transform: (T, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -27,11 +26,10 @@ public inline fun Array.merge(array: Array, transform: ( * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun BooleanArray.merge(array: Array, transform: (Boolean, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -40,11 +38,10 @@ public inline fun BooleanArray.merge(array: Array, transform: (Boo * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun ByteArray.merge(array: Array, transform: (Byte, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -53,11 +50,10 @@ public inline fun ByteArray.merge(array: Array, transform: (Byte, * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun CharArray.merge(array: Array, transform: (Char, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -66,11 +62,10 @@ public inline fun CharArray.merge(array: Array, transform: (Char, * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun DoubleArray.merge(array: Array, transform: (Double, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -79,11 +74,10 @@ public inline fun DoubleArray.merge(array: Array, transform: (Doub * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun FloatArray.merge(array: Array, transform: (Float, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -92,11 +86,10 @@ public inline fun FloatArray.merge(array: Array, transform: (Float * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun IntArray.merge(array: Array, transform: (Int, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -105,11 +98,10 @@ public inline fun IntArray.merge(array: Array, transform: (Int, R) * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun LongArray.merge(array: Array, transform: (Long, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -118,11 +110,10 @@ public inline fun LongArray.merge(array: Array, transform: (Long, * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun ShortArray.merge(array: Array, transform: (Short, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -131,11 +122,12 @@ public inline fun ShortArray.merge(array: Array, transform: (Short * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun Iterable.merge(array: Array, transform: (T, R) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(collectionSizeOrDefault(10)) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = array.size() + val list = ArrayList(Math.min(collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in this) { + if (i >= arraySize) break + list.add(transform(element, array[i++])) } return list } @@ -144,11 +136,10 @@ public inline fun Iterable.merge(array: Array, transform: (T * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun BooleanArray.merge(array: BooleanArray, transform: (Boolean, Boolean) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -157,11 +148,10 @@ public inline fun BooleanArray.merge(array: BooleanArray, transform: (Boolea * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun ByteArray.merge(array: ByteArray, transform: (Byte, Byte) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -170,11 +160,10 @@ public inline fun ByteArray.merge(array: ByteArray, transform: (Byte, Byte) * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun CharArray.merge(array: CharArray, transform: (Char, Char) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -183,11 +172,10 @@ public inline fun CharArray.merge(array: CharArray, transform: (Char, Char) * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun DoubleArray.merge(array: DoubleArray, transform: (Double, Double) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -196,11 +184,10 @@ public inline fun DoubleArray.merge(array: DoubleArray, transform: (Double, * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun FloatArray.merge(array: FloatArray, transform: (Float, Float) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -209,11 +196,10 @@ public inline fun FloatArray.merge(array: FloatArray, transform: (Float, Flo * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun IntArray.merge(array: IntArray, transform: (Int, Int) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -222,11 +208,10 @@ public inline fun IntArray.merge(array: IntArray, transform: (Int, Int) -> V * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun LongArray.merge(array: LongArray, transform: (Long, Long) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -235,11 +220,10 @@ public inline fun LongArray.merge(array: LongArray, transform: (Long, Long) * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun ShortArray.merge(array: ShortArray, transform: (Short, Short) -> V): List { - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list } @@ -248,11 +232,12 @@ public inline fun ShortArray.merge(array: ShortArray, transform: (Short, Sho * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun Array.merge(other: Iterable, transform: (T, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -261,11 +246,12 @@ public inline fun Array.merge(other: Iterable, transform: (T * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun BooleanArray.merge(other: Iterable, transform: (Boolean, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -274,11 +260,12 @@ public inline fun BooleanArray.merge(other: Iterable, transform: (Bool * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun ByteArray.merge(other: Iterable, transform: (Byte, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -287,11 +274,12 @@ public inline fun ByteArray.merge(other: Iterable, transform: (Byte, R * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun CharArray.merge(other: Iterable, transform: (Char, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -300,11 +288,12 @@ public inline fun CharArray.merge(other: Iterable, transform: (Char, R * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun DoubleArray.merge(other: Iterable, transform: (Double, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -313,11 +302,12 @@ public inline fun DoubleArray.merge(other: Iterable, transform: (Doubl * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun FloatArray.merge(other: Iterable, transform: (Float, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -326,11 +316,12 @@ public inline fun FloatArray.merge(other: Iterable, transform: (Float, * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun IntArray.merge(other: Iterable, transform: (Int, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -339,11 +330,12 @@ public inline fun IntArray.merge(other: Iterable, transform: (Int, R) * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun LongArray.merge(other: Iterable, transform: (Long, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -352,11 +344,12 @@ public inline fun LongArray.merge(other: Iterable, transform: (Long, R * Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. */ public inline fun ShortArray.merge(other: Iterable, transform: (Short, R) -> V): List { - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list } @@ -367,7 +360,7 @@ public inline fun ShortArray.merge(other: Iterable, transform: (Short, public inline fun Iterable.merge(other: Iterable, transform: (T, R) -> V): List { val first = iterator() val second = other.iterator() - val list = ArrayList(collectionSizeOrDefault(10)) + val list = ArrayList(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index e5cab227837..f50e82f4211 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -393,7 +393,7 @@ fun generators(): List { """ val first = iterator() val second = other.iterator() - val list = ArrayList(collectionSizeOrDefault(10)) + val list = ArrayList(Math.min(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -402,11 +402,12 @@ fun generators(): List { } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val first = iterator() - val second = other.iterator() - val list = ArrayList(size()) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = size() + val list = ArrayList(Math.min(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) } return list """ @@ -426,22 +427,22 @@ fun generators(): List { inline(true) body { """ - val first = iterator() - val second = array.iterator() - val list = ArrayList(collectionSizeOrDefault(10)) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val arraySize = array.size() + val list = ArrayList(Math.min(collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in this) { + if (i >= arraySize) break + list.add(transform(element, array[i++])) } return list """ } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list """ @@ -462,11 +463,10 @@ fun generators(): List { inline(true) body() { """ - val first = iterator() - val second = array.iterator() - val list = ArrayList(Math.min(size(), array.size())) - while (first.hasNext() && second.hasNext()) { - list.add(transform(first.next(), second.next())) + val size = Math.min(size(), array.size()) + val list = ArrayList(size) + for (i in 0..size-1) { + list.add(transform(this[i], array[i])) } return list """