diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index 96e64e1f3b8..8da43c6b394 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -15,7 +15,7 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col public inline fun Array.merge(array: Array, transform: (T, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -28,7 +28,7 @@ public inline fun Array.merge(array: Array, transform: ( public inline fun BooleanArray.merge(array: Array, transform: (Boolean, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -41,7 +41,7 @@ public inline fun BooleanArray.merge(array: Array, transform: (Boo public inline fun ByteArray.merge(array: Array, transform: (Byte, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -54,7 +54,7 @@ public inline fun ByteArray.merge(array: Array, transform: (Byte, public inline fun CharArray.merge(array: Array, transform: (Char, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -67,7 +67,7 @@ public inline fun CharArray.merge(array: Array, transform: (Char, public inline fun DoubleArray.merge(array: Array, transform: (Double, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -80,7 +80,7 @@ public inline fun DoubleArray.merge(array: Array, transform: (Doub public inline fun FloatArray.merge(array: Array, transform: (Float, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -93,7 +93,7 @@ public inline fun FloatArray.merge(array: Array, transform: (Float public inline fun IntArray.merge(array: Array, transform: (Int, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -106,7 +106,7 @@ public inline fun IntArray.merge(array: Array, transform: (Int, R) public inline fun LongArray.merge(array: Array, transform: (Long, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -119,7 +119,7 @@ public inline fun LongArray.merge(array: Array, transform: (Long, public inline fun ShortArray.merge(array: Array, transform: (Short, R) -> V): List { val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -139,6 +139,110 @@ public inline fun Iterable.merge(array: Array, transform: (T return list } +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + +/** + * 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())) + } + return list +} + /** * Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection. */ @@ -525,7 +629,8 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair Array.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -534,7 +639,8 @@ public fun Array.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun BooleanArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -543,7 +649,8 @@ public fun BooleanArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun ByteArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -552,7 +659,8 @@ public fun ByteArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun CharArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -561,7 +669,8 @@ public fun CharArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun DoubleArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -570,7 +679,8 @@ public fun DoubleArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun FloatArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -579,7 +689,8 @@ public fun FloatArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun IntArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -588,7 +699,8 @@ public fun IntArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun LongArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -597,7 +709,8 @@ public fun LongArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun ShortArray.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -606,7 +719,8 @@ public fun ShortArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun Iterable.plus(array: Array): List { - val answer = toArrayList() + val answer = ArrayList(collectionSizeOrDefault(10) + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer } @@ -615,7 +729,8 @@ public fun Iterable.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun Array.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -624,7 +739,8 @@ public fun Array.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun BooleanArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -633,7 +749,8 @@ public fun BooleanArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun ByteArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -642,7 +759,8 @@ public fun ByteArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun CharArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -651,7 +769,8 @@ public fun CharArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun DoubleArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -660,7 +779,8 @@ public fun DoubleArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun FloatArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -669,7 +789,8 @@ public fun FloatArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun IntArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -678,7 +799,8 @@ public fun IntArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun LongArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -687,7 +809,8 @@ public fun LongArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun ShortArray.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -696,7 +819,8 @@ public fun ShortArray.plus(collection: Iterable): List { * Returns a list containing all elements of original collection and then all elements of the given *collection* */ public fun Iterable.plus(collection: Iterable): List { - val answer = toArrayList() + val answer = ArrayList(collectionSizeOrDefault(10) + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer } @@ -721,7 +845,8 @@ public fun Stream.plus(collection: Iterable): Stream { * Returns a list containing all elements of original collection and then the given element */ public fun Array.plus(element: T): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -730,7 +855,8 @@ public fun Array.plus(element: T): List { * Returns a list containing all elements of original collection and then the given element */ public fun BooleanArray.plus(element: Boolean): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -739,7 +865,8 @@ public fun BooleanArray.plus(element: Boolean): List { * Returns a list containing all elements of original collection and then the given element */ public fun ByteArray.plus(element: Byte): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -748,7 +875,8 @@ public fun ByteArray.plus(element: Byte): List { * Returns a list containing all elements of original collection and then the given element */ public fun CharArray.plus(element: Char): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -757,7 +885,8 @@ public fun CharArray.plus(element: Char): List { * Returns a list containing all elements of original collection and then the given element */ public fun DoubleArray.plus(element: Double): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -766,7 +895,8 @@ public fun DoubleArray.plus(element: Double): List { * Returns a list containing all elements of original collection and then the given element */ public fun FloatArray.plus(element: Float): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -775,7 +905,8 @@ public fun FloatArray.plus(element: Float): List { * Returns a list containing all elements of original collection and then the given element */ public fun IntArray.plus(element: Int): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -784,7 +915,8 @@ public fun IntArray.plus(element: Int): List { * Returns a list containing all elements of original collection and then the given element */ public fun LongArray.plus(element: Long): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -793,7 +925,8 @@ public fun LongArray.plus(element: Long): List { * Returns a list containing all elements of original collection and then the given element */ public fun ShortArray.plus(element: Short): List { - val answer = toArrayList() + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -802,7 +935,8 @@ public fun ShortArray.plus(element: Short): List { * Returns a list containing all elements of original collection and then the given element */ public fun Iterable.plus(element: T): List { - val answer = toArrayList() + val answer = ArrayList(collectionSizeOrNull()?.let { it + 1 } ?: 10) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer } @@ -909,6 +1043,62 @@ public fun Iterable.zip(array: Array): List> { return merge(array) { t1, t2 -> t1 to t2 } } +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun BooleanArray.zip(array: BooleanArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun ByteArray.zip(array: ByteArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun CharArray.zip(array: CharArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun DoubleArray.zip(array: DoubleArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun FloatArray.zip(array: FloatArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun IntArray.zip(array: IntArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun LongArray.zip(array: LongArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + */ +public fun ShortArray.zip(array: ShortArray): List> { + return merge(array) { t1, t2 -> t1 to t2 } +} + /** * Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. */ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index c5781c742d7..83786c50b03 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -11,7 +11,16 @@ fun generators(): List { returns("List") body { """ - val answer = toArrayList() + val answer = ArrayList(collectionSizeOrNull()?.let { it + 1 } ?: 10) + for (thisElement in this) answer.add(thisElement) + answer.add(element) + return answer + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val answer = ArrayList(size()+1) + for (thisElement in this) answer.add(thisElement) answer.add(element) return answer """ @@ -32,7 +41,17 @@ fun generators(): List { returns("List") body { """ - val answer = toArrayList() + val answer = ArrayList(collectionSizeOrDefault(10) + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) + answer.addAll(collection) + return answer + """ + } + + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) + for (thisElement in this) answer.add(thisElement) answer.addAll(collection) return answer """ @@ -45,7 +64,16 @@ fun generators(): List { returns("List") body { """ - val answer = toArrayList() + val answer = ArrayList(collectionSizeOrDefault(10) + array.size()) + for (thisElement in this) answer.add(thisElement) + answer.addAll(array) + return answer + """ + } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val answer = ArrayList(size() + array.size()) + for (thisElement in this) answer.add(thisElement) answer.addAll(array) return answer """ @@ -179,7 +207,7 @@ fun generators(): List { """ val first = iterator() val second = array.iterator() - val list = ArrayList(size()) + val list = ArrayList(Math.min(size(), array.size())) while (first.hasNext() && second.hasNext()) { list.add(transform(first.next(), second.next())) } @@ -190,6 +218,30 @@ fun generators(): List { } + templates add f("merge(array: SELF, transform: (T, T) -> V)") { + only(ArraysOfPrimitives) + doc { + """ + Returns a list of values built from elements of both collections with same indexes using provided *transform*. List has length of shortest collection. + """ + } + typeParam("V") + returns("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())) + } + return list + """ + } + } + + templates add f("merge(sequence: Sequence, transform: (T, R) -> V)") { only(Sequences) doc { @@ -261,6 +313,21 @@ fun generators(): List { } } + templates add f("zip(array: SELF)") { + only(ArraysOfPrimitives) + doc { + """ + Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. + """ + } + returns("List>") + body { + """ + return merge(array) { t1, t2 -> t1 to t2 } + """ + } + } + templates add f("zip(sequence: Sequence)") { only(Sequences) doc {