diff --git a/libraries/stdlib/src/generated/_Generators.kt b/libraries/stdlib/src/generated/_Generators.kt index 899edd8e78c..f458dd2d021 100644 --- a/libraries/stdlib/src/generated/_Generators.kt +++ b/libraries/stdlib/src/generated/_Generators.kt @@ -600,89 +600,9 @@ public inline fun String.partition(predicate: (Char) -> Boolean): Pair Array.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun BooleanArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun ByteArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun CharArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun DoubleArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun FloatArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun IntArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun LongArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) - answer.addAll(array) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [array]. - */ -public fun ShortArray.plus(array: Array): List { - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) +public fun Collection.plus(array: Array): List { + val answer = ArrayList(this.size() + array.size()) + answer.addAll(this) answer.addAll(array) return answer } @@ -691,226 +611,356 @@ public fun ShortArray.plus(array: Array): List { * Returns a list containing all elements of original collection and then all elements of the given [array]. */ public fun Iterable.plus(array: Array): List { - val answer = ArrayList(collectionSizeOrDefault(10) + array.size()) - for (thisElement in this) answer.add(thisElement) + if (this is Collection) return this.plus(array) + val answer = ArrayList() + answer.addAll(this) answer.addAll(array) return answer } /** - * Returns a list containing all elements of original collection and then all elements of the given [collection]. + * Returns a list containing all elements of original collection and then all elements of the given [array]. */ -public fun Array.plus(collection: Iterable): List { - val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) +public fun Sequence.plus(array: Array): Sequence { + return this.plus(array.asList()) +} + +/** + * Returns a list containing all elements of original collection and then all elements of the given [array]. + */ +public fun Set.plus(array: Array): Set { + val result = LinkedHashSet(mapCapacity(this.size() + array.size())) + result.addAll(this) + result.addAll(array) + return result +} + +public fun Array.plus(collection: Collection): Array { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as Array +} + +public fun BooleanArray.plus(collection: Collection): BooleanArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as BooleanArray +} + +public fun ByteArray.plus(collection: Collection): ByteArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as ByteArray +} + +public fun CharArray.plus(collection: Collection): CharArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as CharArray +} + +public fun DoubleArray.plus(collection: Collection): DoubleArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as DoubleArray +} + +public fun FloatArray.plus(collection: Collection): FloatArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as FloatArray +} + +public fun IntArray.plus(collection: Collection): IntArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as IntArray +} + +public fun LongArray.plus(collection: Collection): LongArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as LongArray +} + +public fun ShortArray.plus(collection: Collection): ShortArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as ShortArray +} + +public fun Collection.plus(collection: Collection): List { + val answer = ArrayList(this.size() + collection.size()) + answer.addAll(this) answer.addAll(collection) return answer } /** - * Returns a list containing all elements of original collection and then all elements of the given [collection]. + * Returns a list containing all elements of the original collection and then all elements of the given [collection]. */ -public fun BooleanArray.plus(collection: Iterable): List { - val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) +public fun Collection.plus(collection: Iterable): List { + if (collection is Collection) return this.plus(collection) + val answer = ArrayList(this) answer.addAll(collection) return answer } /** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * 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 = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) - answer.addAll(collection) - return answer -} - -/** - * Returns a list containing all elements of original collection and then all elements of the given [collection]. + * Returns a list containing all elements of the original collection and then all elements of the given [collection]. */ public fun Iterable.plus(collection: Iterable): List { - val answer = ArrayList(collectionSizeOrDefault(10) + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) + if (this is Collection) return this.plus(collection) + val answer = ArrayList(0) + answer.addAll(this) answer.addAll(collection) return answer } /** - * Returns a sequence containing all elements of original sequence and then all elements of the given [collection]. + * Returns a sequence containing all elements of original sequence and then all elements of the given [collection] */ public fun Sequence.plus(collection: Iterable): Sequence { return sequenceOf(this, collection.asSequence()).flatten() } /** - * Returns a list containing all elements of original collection and then the given [element]. + * Returns a list containing all elements of the original collection and then all elements of the given [collection]. */ -public fun Array.plus(element: T): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) +public fun Set.plus(collection: Iterable): Set { + val result = LinkedHashSet(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2)) + result.addAll(this) + result.addAll(collection) + return result +} + +public fun Array.plus(collection: Array): Array { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as Array +} + +public fun BooleanArray.plus(collection: BooleanArray): BooleanArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as BooleanArray +} + +public fun ByteArray.plus(collection: ByteArray): ByteArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as ByteArray +} + +public fun CharArray.plus(collection: CharArray): CharArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as CharArray +} + +public fun DoubleArray.plus(collection: DoubleArray): DoubleArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as DoubleArray +} + +public fun FloatArray.plus(collection: FloatArray): FloatArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as FloatArray +} + +public fun IntArray.plus(collection: IntArray): IntArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as IntArray +} + +public fun LongArray.plus(collection: LongArray): LongArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as LongArray +} + +public fun ShortArray.plus(collection: ShortArray): ShortArray { + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as ShortArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun Array.plus(element: T): Array { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as Array +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun BooleanArray.plus(element: Boolean): BooleanArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as BooleanArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun ByteArray.plus(element: Byte): ByteArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as ByteArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun CharArray.plus(element: Char): CharArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as CharArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun DoubleArray.plus(element: Double): DoubleArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as DoubleArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun FloatArray.plus(element: Float): FloatArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as FloatArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun IntArray.plus(element: Int): IntArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as IntArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun LongArray.plus(element: Long): LongArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as LongArray +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public fun ShortArray.plus(element: Short): ShortArray { + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as ShortArray +} + +/** + * Returns a list containing all elements of the original collection and then the given [element]. + */ +public fun Collection.plus(element: T): List { + val answer = ArrayList(size() + 1) + answer.addAll(this) answer.add(element) return answer } /** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun BooleanArray.plus(element: Boolean): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun ByteArray.plus(element: Byte): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun CharArray.plus(element: Char): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun DoubleArray.plus(element: Double): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun FloatArray.plus(element: Float): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun IntArray.plus(element: Int): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun LongArray.plus(element: Long): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. - */ -public fun ShortArray.plus(element: Short): List { - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) - answer.add(element) - return answer -} - -/** - * Returns a list containing all elements of original collection and then the given [element]. + * Returns a list containing all elements of the original collection and then the given [element]. */ public fun Iterable.plus(element: T): List { - val answer = ArrayList(collectionSizeOrNull()?.let { it + 1 } ?: 10) - for (thisElement in this) answer.add(thisElement) + if (this is Collection) return this.plus(element) + val answer = ArrayList() + answer.addAll(this) answer.add(element) return answer } /** - * Returns a sequence containing all elements of original sequence and then the given [element]. + * Returns a sequence containing all elements of the original sequence and then the given [element]. */ public fun Sequence.plus(element: T): Sequence { return sequenceOf(this, sequenceOf(element)).flatten() } +/** + * Returns a set containing all elements of the original set and then the given [element]. + */ +public fun Set.plus(element: T): Set { + val copyOfSet = LinkedHashSet(mapCapacity(size() + 1)) + copyOfSet.addAll(this) + copyOfSet.add(element) + return copyOfSet +} + /** * Returns a sequence containing all elements of original sequence and then all elements of the given [sequence]. */ diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 81d4e2b0ffb..534d69ad933 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -21,7 +21,7 @@ public fun MutableCollection.addAll(sequence: Sequence) { * Adds all elements of the given [array] to this [MutableCollection]. */ public fun MutableCollection.addAll(array: Array) { - for (item in array) add(item) + addAll(array.asList()) } /** @@ -54,7 +54,7 @@ public fun MutableCollection.removeAll(array: Array) { public fun MutableCollection.retainAll(iterable: Iterable) { when (iterable) { is Collection -> retainAll(iterable) - else -> retainAll(iterable.toSet()) + else -> retainAll(iterable.toHashSet()) } } @@ -62,5 +62,5 @@ public fun MutableCollection.retainAll(iterable: Iterable) { * Retains only elements of the given [array] in this [MutableCollection]. */ public fun MutableCollection.retainAll(array: Array) { - retainAll(array.toSet()) + retainAll(array.toHashSet()) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 915fd8e0775..84db08671e1 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -7,27 +7,51 @@ fun generators(): List { templates add f("plus(element: T)") { exclude(Strings) - doc { "Returns a list containing all elements of original collection and then the given [element]." } + doc { "Returns a list containing all elements of the original collection and then the given [element]." } returns("List") + returns("SELF", ArraysOfObjects, ArraysOfPrimitives, Sets, Sequences) body { """ - val answer = ArrayList(collectionSizeOrNull()?.let { it + 1 } ?: 10) - for (thisElement in this) answer.add(thisElement) + if (this is Collection) return this.plus(element) + val answer = ArrayList() + answer.addAll(this) answer.add(element) return answer """ } - body(ArraysOfObjects, ArraysOfPrimitives) { + body(Collections) { """ - val answer = ArrayList(size()+1) - for (thisElement in this) answer.add(thisElement) + val answer = ArrayList(size() + 1) + answer.addAll(this) answer.add(element) return answer """ } - doc(Sequences) { "Returns a sequence containing all elements of original sequence and then the given [element]." } - returns(Sequences) { "Sequence" } + + doc(ArraysOfObjects, ArraysOfPrimitives) { "Returns an array containing all elements of the original array and then the given [element]." } + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val answer = this.copyOf(size() + 1) + answer[size()] = element + return answer as SELF + """ + } + + // TODO: use build scope function when available + // TODO: use immutable sets when available + // TODO: precalculate size + doc(Sets) { "Returns a set containing all elements of the original set and then the given [element]." } + body(Sets) { + """ + val copyOfSet = LinkedHashSet(mapCapacity(size() + 1)) + copyOfSet.addAll(this) + copyOfSet.add(element) + return copyOfSet + """ + } + + doc(Sequences) { "Returns a sequence containing all elements of the original sequence and then the given [element]." } body(Sequences) { """ return sequenceOf(this, sequenceOf(element)).flatten() @@ -36,57 +60,120 @@ fun generators(): List { } templates add f("plus(collection: Iterable)") { - exclude(Strings, Sequences) - doc { "Returns a list containing all elements of original collection and then all elements of the given [collection]." } + only(Iterables, Collections, Sets, Sequences) + doc { "Returns a list containing all elements of the original collection and then all elements of the given [collection]." } returns("List") + returns("SELF", ArraysOfObjects, ArraysOfPrimitives, Sets, Sequences) body { """ - val answer = ArrayList(collectionSizeOrDefault(10) + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) + if (this is Collection) return this.plus(collection) + val answer = ArrayList(0) + answer.addAll(this) + answer.addAll(collection) + return answer + """ + } + body(Collections) { + """ + if (collection is Collection) return this.plus(collection) + val answer = ArrayList(this) answer.addAll(collection) return answer """ } - body(ArraysOfObjects, ArraysOfPrimitives) { + // TODO: try to precalculate size + // TODO: use immutable set builder when available + body(Sets) { """ - val answer = ArrayList(size() + collection.collectionSizeOrDefault(10)) - for (thisElement in this) answer.add(thisElement) + val result = LinkedHashSet(mapCapacity(collection.collectionSizeOrNull()?.let { this.size() + it } ?: this.size() * 2)) + result.addAll(this) + result.addAll(collection) + return result + """ + } + + doc(Sequences) { "Returns a sequence containing all elements of original sequence and then all elements of the given [collection]" } + body(Sequences) { + """ + return sequenceOf(this, collection.asSequence()).flatten() + """ + } + } + + templates add f("plus(collection: Collection)") { + only(Collections, ArraysOfPrimitives, ArraysOfObjects) + returns("List") + returns("SELF", ArraysOfObjects, ArraysOfPrimitives) + body(Collections) { + """ + val answer = ArrayList(this.size() + collection.size()) + answer.addAll(this) answer.addAll(collection) return answer """ } + // ? + body(ArraysOfObjects, ArraysOfPrimitives) { + """ + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as SELF + """ + } } templates add f("plus(array: Array)") { - exclude(Strings, Sequences) + only(Iterables, Collections, Sets, Sequences) doc { "Returns a list containing all elements of original collection and then all elements of the given [array]." } returns("List") + returns("SELF", Sets, Sequences) body { """ - val answer = ArrayList(collectionSizeOrDefault(10) + array.size()) - for (thisElement in this) answer.add(thisElement) + if (this is Collection) return this.plus(array) + val answer = ArrayList() + answer.addAll(this) answer.addAll(array) return answer """ } - body(ArraysOfObjects, ArraysOfPrimitives) { + body(Collections) { """ - val answer = ArrayList(size() + array.size()) - for (thisElement in this) answer.add(thisElement) + val answer = ArrayList(this.size() + array.size()) + answer.addAll(this) answer.addAll(array) return answer """ } + body(Sets) { + """ + val result = LinkedHashSet(mapCapacity(this.size() + array.size())) + result.addAll(this) + result.addAll(array) + return result + """ + } + body(Sequences) { + """ + return this.plus(array.asList()) + """ + } } - templates add f("plus(collection: Iterable)") { - only(Sequences) - doc { "Returns a sequence containing all elements of original sequence and then all elements of the given [collection]." } - returns("Sequence") + templates add f("plus(collection: SELF)") { + only(ArraysOfObjects, ArraysOfPrimitives) + returns("SELF") body { """ - return sequenceOf(this, collection.asSequence()).flatten() + val thisSize = size() + val answer = this.copyOf(thisSize + collection.size()) + collection.forEachIndexed { i, element -> + answer[thisSize + i] = element + } + return answer as SELF """ } }