From 32b68ae1d501750507e4378273dc322912bafbd5 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 14 Nov 2017 08:01:33 +0300 Subject: [PATCH] Rearrange platform specialized functions Place them according to their operation to ArrayOps and Filtering. --- .../src/core/generated/_ArraysJs.kt | 1370 +++++------ .../stdlib/common/src/generated/_Arrays.kt | 810 +++---- libraries/stdlib/src/generated/_Arrays.kt | 2122 ++++++++--------- .../stdlib/src/generated/_Collections.kt | 32 +- libraries/stdlib/src/generated/_Sequences.kt | 38 +- .../src/generators/GenerateStandardLib.kt | 5 +- .../kotlin-stdlib-gen/src/templates/Arrays.kt | 466 ++++ .../src/templates/Filtering.kt | 45 + .../src/templates/SpecialJVM.kt | 536 ----- 9 files changed, 2700 insertions(+), 2724 deletions(-) delete mode 100644 libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt index 08df7d86716..c3c860aab65 100644 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -5159,6 +5159,83 @@ public fun CharArray.sortedWith(comparator: Comparator): List { return toTypedArray().apply { sortWith(comparator) }.asList() } +/** + * Returns a [List] that wraps the original array. + */ +public fun Array.asList(): List { + return ArrayList(this.unsafeCast>()) +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +@kotlin.internal.InlineOnly +public inline fun BooleanArray.asList(): List { + return this.unsafeCast>().asList() +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun CharArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Char): Boolean = this@asList.contains(element) + override fun get(index: Int): Char = this@asList[index] + override fun indexOf(element: Char): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Char): Int = this@asList.lastIndexOf(element) + } +} + /** * Returns `true` if the two specified arrays are *deeply* equal to one another, * i.e. contain the same number of the same elements in the same order. @@ -5479,6 +5556,207 @@ public fun CharArray.contentToString(): String { definedExternally } +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.copyOf(): Array { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun ByteArray.copyOf(): ByteArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun ShortArray.copyOf(): ShortArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun IntArray.copyOf(): IntArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +public fun LongArray.copyOf(): LongArray { + return withType("LongArray", this.asDynamic().slice()) +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun FloatArray.copyOf(): FloatArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun DoubleArray.copyOf(): DoubleArray { + return this.asDynamic().slice() +} + +/** + * Returns new array which is a copy of the original array. + */ +public fun BooleanArray.copyOf(): BooleanArray { + return withType("BooleanArray", this.asDynamic().slice()) +} + +/** + * Returns new array which is a copy of the original array. + */ +public fun CharArray.copyOf(): CharArray { + return withType("CharArray", this.asDynamic().slice()) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun ByteArray.copyOf(newSize: Int): ByteArray { + return fillFrom(this, ByteArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun ShortArray.copyOf(newSize: Int): ShortArray { + return fillFrom(this, ShortArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun IntArray.copyOf(newSize: Int): IntArray { + return fillFrom(this, IntArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun LongArray.copyOf(newSize: Int): LongArray { + return withType("LongArray", arrayCopyResize(this, newSize, 0L)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun FloatArray.copyOf(newSize: Int): FloatArray { + return fillFrom(this, FloatArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun DoubleArray.copyOf(newSize: Int): DoubleArray { + return fillFrom(this, DoubleArray(newSize)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun BooleanArray.copyOf(newSize: Int): BooleanArray { + return withType("BooleanArray", arrayCopyResize(this, newSize, false)) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun CharArray.copyOf(newSize: Int): CharArray { + return withType("CharArray", fillFrom(this, CharArray(newSize))) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public fun Array.copyOf(newSize: Int): Array { + return arrayCopyResize(this, newSize, null) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +public fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { + return withType("LongArray", this.asDynamic().slice(fromIndex, toIndex)) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { + return this.asDynamic().slice(fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +public fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { + return withType("BooleanArray", this.asDynamic().slice(fromIndex, toIndex)) +} + +/** + * Returns new array which is a copy of range of original array. + */ +public fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { + return withType("CharArray", this.asDynamic().slice(fromIndex, toIndex)) +} + /** * Returns the range of valid indices for the array. */ @@ -5731,6 +6009,357 @@ public val BooleanArray.lastIndex: Int public val CharArray.lastIndex: Int get() = size - 1 +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.plus(element: T): Array { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.plus(element: Byte): ByteArray { + return plus(byteArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.plus(element: Short): ShortArray { + return plus(shortArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.plus(element: Int): IntArray { + return plus(intArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.plus(element: Long): LongArray { + return plus(longArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.plus(element: Float): FloatArray { + return plus(floatArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.plus(element: Double): DoubleArray { + return plus(doubleArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { + return plus(booleanArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.plus(element: Char): CharArray { + return plus(charArrayOf(element)) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun Array.plus(elements: Collection): Array { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun ByteArray.plus(elements: Collection): ByteArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun ShortArray.plus(elements: Collection): ShortArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun IntArray.plus(elements: Collection): IntArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun LongArray.plus(elements: Collection): LongArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun FloatArray.plus(elements: Collection): FloatArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun DoubleArray.plus(elements: Collection): DoubleArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun BooleanArray.plus(elements: Collection): BooleanArray { + return arrayPlusCollection(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun CharArray.plus(elements: Collection): CharArray { + return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun Array.plus(elements: Array): Array { + return this.asDynamic().concat(elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun IntArray.plus(elements: IntArray): IntArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun LongArray.plus(elements: LongArray): LongArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +@Suppress("NOTHING_TO_INLINE") +public inline operator fun CharArray.plus(elements: CharArray): CharArray { + return primitiveArrayConcat(this, elements) +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@Suppress("NOTHING_TO_INLINE") +public inline fun Array.plusElement(element: T): Array { + return this.asDynamic().concat(arrayOf(element)) +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun IntArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +public fun LongArray.sort(): Unit { + if (size > 1) + sort { a: Long, b: Long -> a.compareTo(b) } +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun ByteArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun ShortArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun DoubleArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun FloatArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place. + */ +@library("primitiveArraySort") +public fun CharArray.sort(): Unit { + definedExternally +} + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +public fun > Array.sort(): Unit { + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun Array.sort(noinline comparison: (a: T, b: T) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparison] function. + */ +@kotlin.internal.InlineOnly +public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int): Unit { + asDynamic().sort(comparison) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) + sort { a, b -> comparator.compare(a, b) } +} + /** * Returns an array of Boolean containing all of the elements of this generic array. */ @@ -5811,6 +6440,62 @@ public fun Array.toShortArray(): ShortArray { return result } +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + return js("[]").slice.call(this) +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + return Array(size, { i -> this[i] }) +} + /** * Returns a [Map] containing key-value pairs provided by [transform] function * applied to elements of the given array. @@ -12928,688 +13613,3 @@ public fun DoubleArray.sum(): Double { return sum } -/** - * Returns a [List] that wraps the original array. - */ -public fun Array.asList(): List { - return ArrayList(this.unsafeCast>()) -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.asList(): List { - return this.unsafeCast>().asList() -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun CharArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Char): Boolean = this@asList.contains(element) - override fun get(index: Int): Char = this@asList[index] - override fun indexOf(element: Char): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Char): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.copyOf(): Array { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.copyOf(): ByteArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.copyOf(): ShortArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.copyOf(): IntArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun LongArray.copyOf(): LongArray { - return withType("LongArray", this.asDynamic().slice()) -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.copyOf(): FloatArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.copyOf(): DoubleArray { - return this.asDynamic().slice() -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun BooleanArray.copyOf(): BooleanArray { - return withType("BooleanArray", this.asDynamic().slice()) -} - -/** - * Returns new array which is a copy of the original array. - */ -public fun CharArray.copyOf(): CharArray { - return withType("CharArray", this.asDynamic().slice()) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun ByteArray.copyOf(newSize: Int): ByteArray { - return fillFrom(this, ByteArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun ShortArray.copyOf(newSize: Int): ShortArray { - return fillFrom(this, ShortArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun IntArray.copyOf(newSize: Int): IntArray { - return fillFrom(this, IntArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun LongArray.copyOf(newSize: Int): LongArray { - return withType("LongArray", arrayCopyResize(this, newSize, 0L)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun FloatArray.copyOf(newSize: Int): FloatArray { - return fillFrom(this, FloatArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun DoubleArray.copyOf(newSize: Int): DoubleArray { - return fillFrom(this, DoubleArray(newSize)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun BooleanArray.copyOf(newSize: Int): BooleanArray { - return withType("BooleanArray", arrayCopyResize(this, newSize, false)) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun CharArray.copyOf(newSize: Int): CharArray { - return withType("CharArray", fillFrom(this, CharArray(newSize))) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public fun Array.copyOf(newSize: Int): Array { - return arrayCopyResize(this, newSize, null) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { - return withType("LongArray", this.asDynamic().slice(fromIndex, toIndex)) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { - return this.asDynamic().slice(fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { - return withType("BooleanArray", this.asDynamic().slice(fromIndex, toIndex)) -} - -/** - * Returns new array which is a copy of range of original array. - */ -public fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { - return withType("CharArray", this.asDynamic().slice(fromIndex, toIndex)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.plus(element: T): Array { - return this.asDynamic().concat(arrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.plus(element: Byte): ByteArray { - return plus(byteArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.plus(element: Short): ShortArray { - return plus(shortArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.plus(element: Int): IntArray { - return plus(intArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.plus(element: Long): LongArray { - return plus(longArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.plus(element: Float): FloatArray { - return plus(floatArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.plus(element: Double): DoubleArray { - return plus(doubleArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray { - return plus(booleanArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.plus(element: Char): CharArray { - return plus(charArrayOf(element)) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun Array.plus(elements: Collection): Array { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ByteArray.plus(elements: Collection): ByteArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ShortArray.plus(elements: Collection): ShortArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun IntArray.plus(elements: Collection): IntArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun LongArray.plus(elements: Collection): LongArray { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun FloatArray.plus(elements: Collection): FloatArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun DoubleArray.plus(elements: Collection): DoubleArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun BooleanArray.plus(elements: Collection): BooleanArray { - return arrayPlusCollection(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun CharArray.plus(elements: Collection): CharArray { - return fillFromCollection(this.copyOf(size + elements.size), this.size, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun Array.plus(elements: Array): Array { - return this.asDynamic().concat(elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun IntArray.plus(elements: IntArray): IntArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun LongArray.plus(elements: LongArray): LongArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -@Suppress("NOTHING_TO_INLINE") -public inline operator fun CharArray.plus(elements: CharArray): CharArray { - return primitiveArrayConcat(this, elements) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@Suppress("NOTHING_TO_INLINE") -public inline fun Array.plusElement(element: T): Array { - return this.asDynamic().concat(arrayOf(element)) -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun IntArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -public fun LongArray.sort(): Unit { - if (size > 1) - sort { a: Long, b: Long -> a.compareTo(b) } -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ByteArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun ShortArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun DoubleArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun FloatArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place. - */ -@library("primitiveArraySort") -public fun CharArray.sort(): Unit { - definedExternally -} - -/** - * Sorts the array in-place according to the natural order of its elements. - */ -public fun > Array.sort(): Unit { - if (size > 1) - sort { a: T, b: T -> a.compareTo(b) } -} - -/** - * Sorts the array in-place according to the order specified by the given [comparator]. - */ -public fun Array.sortWith(comparator: Comparator): Unit { - if (size > 1) - sort { a, b -> comparator.compare(a, b) } -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ByteArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ShortArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun IntArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun LongArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun FloatArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun DoubleArray.toTypedArray(): Array { - return js("[]").slice.call(this) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun BooleanArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun CharArray.toTypedArray(): Array { - return Array(size, { i -> this[i] }) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun Array.sort(noinline comparison: (a: T, b: T) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int): Unit { - asDynamic().sort(comparison) -} - -/** - * Sorts the array in-place according to the order specified by the given [comparison] function. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int): Unit { - asDynamic().sort(comparison) -} - diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 594dfdcb4be..1e2f168fa30 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -2801,6 +2801,51 @@ public expect fun BooleanArray.sortedWith(comparator: Comparator): L */ public expect fun CharArray.sortedWith(comparator: Comparator): List +/** + * Returns a [List] that wraps the original array. + */ +public expect fun Array.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun ByteArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun ShortArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun IntArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun LongArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun FloatArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun DoubleArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun BooleanArray.asList(): List + +/** + * Returns a [List] that wraps the original array. + */ +public expect fun CharArray.asList(): List + /** * Returns `true` if the two specified arrays are *deeply* equal to one another, * i.e. contain the same number of the same elements in the same order. @@ -3021,6 +3066,141 @@ public expect fun BooleanArray.contentToString(): String @SinceKotlin("1.1") public expect fun CharArray.contentToString(): String +/** + * Returns new array which is a copy of the original array. + */ +public expect fun Array.copyOf(): Array + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun ByteArray.copyOf(): ByteArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun ShortArray.copyOf(): ShortArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun IntArray.copyOf(): IntArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun LongArray.copyOf(): LongArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun FloatArray.copyOf(): FloatArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun DoubleArray.copyOf(): DoubleArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun BooleanArray.copyOf(): BooleanArray + +/** + * Returns new array which is a copy of the original array. + */ +public expect fun CharArray.copyOf(): CharArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun ByteArray.copyOf(newSize: Int): ByteArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun ShortArray.copyOf(newSize: Int): ShortArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun IntArray.copyOf(newSize: Int): IntArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun LongArray.copyOf(newSize: Int): LongArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun FloatArray.copyOf(newSize: Int): FloatArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun DoubleArray.copyOf(newSize: Int): DoubleArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun BooleanArray.copyOf(newSize: Int): BooleanArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun CharArray.copyOf(newSize: Int): CharArray + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +public expect fun Array.copyOf(newSize: Int): Array + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray + +/** + * Returns new array which is a copy of range of original array. + */ +public expect fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray + /** * Returns the range of valid indices for the array. */ @@ -3219,6 +3399,191 @@ public expect val BooleanArray.lastIndex: Int */ public expect val CharArray.lastIndex: Int +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun Array.plus(element: T): Array + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun ByteArray.plus(element: Byte): ByteArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun ShortArray.plus(element: Short): ShortArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun IntArray.plus(element: Int): IntArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun LongArray.plus(element: Long): LongArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun FloatArray.plus(element: Float): FloatArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun DoubleArray.plus(element: Double): DoubleArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun BooleanArray.plus(element: Boolean): BooleanArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect operator fun CharArray.plus(element: Char): CharArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun Array.plus(elements: Collection): Array + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun ByteArray.plus(elements: Collection): ByteArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun ShortArray.plus(elements: Collection): ShortArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun IntArray.plus(elements: Collection): IntArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun LongArray.plus(elements: Collection): LongArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun FloatArray.plus(elements: Collection): FloatArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun DoubleArray.plus(elements: Collection): DoubleArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun BooleanArray.plus(elements: Collection): BooleanArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public expect operator fun CharArray.plus(elements: Collection): CharArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun Array.plus(elements: Array): Array + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun ByteArray.plus(elements: ByteArray): ByteArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun ShortArray.plus(elements: ShortArray): ShortArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun IntArray.plus(elements: IntArray): IntArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun LongArray.plus(elements: LongArray): LongArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun FloatArray.plus(elements: FloatArray): FloatArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public expect operator fun CharArray.plus(elements: CharArray): CharArray + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public expect fun Array.plusElement(element: T): Array + +/** + * Sorts the array in-place. + */ +public expect fun IntArray.sort(): Unit + +/** + * Sorts the array in-place. + */ +public expect fun LongArray.sort(): Unit + +/** + * Sorts the array in-place. + */ +public expect fun ByteArray.sort(): Unit + +/** + * Sorts the array in-place. + */ +public expect fun ShortArray.sort(): Unit + +/** + * Sorts the array in-place. + */ +public expect fun DoubleArray.sort(): Unit + +/** + * Sorts the array in-place. + */ +public expect fun FloatArray.sort(): Unit + +/** + * Sorts the array in-place. + */ +public expect fun CharArray.sort(): Unit + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +public expect fun > Array.sort(): Unit + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public expect fun Array.sortWith(comparator: Comparator): Unit + /** * Returns an array of Boolean containing all of the elements of this generic array. */ @@ -3259,6 +3624,46 @@ public expect fun Array.toLongArray(): LongArray */ public expect fun Array.toShortArray(): ShortArray +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun ByteArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun ShortArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun IntArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun LongArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun FloatArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun DoubleArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun BooleanArray.toTypedArray(): Array + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public expect fun CharArray.toTypedArray(): Array + /** * Returns a [Map] containing key-value pairs provided by [transform] function * applied to elements of the given array. @@ -7356,408 +7761,3 @@ public expect fun FloatArray.sum(): Float */ public expect fun DoubleArray.sum(): Double -/** - * Returns a [List] that wraps the original array. - */ -public expect fun Array.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun ByteArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun ShortArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun IntArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun LongArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun FloatArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun DoubleArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun BooleanArray.asList(): List - -/** - * Returns a [List] that wraps the original array. - */ -public expect fun CharArray.asList(): List - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun Array.copyOf(): Array - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun ByteArray.copyOf(): ByteArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun ShortArray.copyOf(): ShortArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun IntArray.copyOf(): IntArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun LongArray.copyOf(): LongArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun FloatArray.copyOf(): FloatArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun DoubleArray.copyOf(): DoubleArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun BooleanArray.copyOf(): BooleanArray - -/** - * Returns new array which is a copy of the original array. - */ -public expect fun CharArray.copyOf(): CharArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun ByteArray.copyOf(newSize: Int): ByteArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun ShortArray.copyOf(newSize: Int): ShortArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun IntArray.copyOf(newSize: Int): IntArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun LongArray.copyOf(newSize: Int): LongArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun FloatArray.copyOf(newSize: Int): FloatArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun DoubleArray.copyOf(newSize: Int): DoubleArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun BooleanArray.copyOf(newSize: Int): BooleanArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun CharArray.copyOf(newSize: Int): CharArray - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -public expect fun Array.copyOf(newSize: Int): Array - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray - -/** - * Returns new array which is a copy of range of original array. - */ -public expect fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun Array.plus(element: T): Array - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun ByteArray.plus(element: Byte): ByteArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun ShortArray.plus(element: Short): ShortArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun IntArray.plus(element: Int): IntArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun LongArray.plus(element: Long): LongArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun FloatArray.plus(element: Float): FloatArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun DoubleArray.plus(element: Double): DoubleArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun BooleanArray.plus(element: Boolean): BooleanArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect operator fun CharArray.plus(element: Char): CharArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun Array.plus(elements: Collection): Array - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun ByteArray.plus(elements: Collection): ByteArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun ShortArray.plus(elements: Collection): ShortArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun IntArray.plus(elements: Collection): IntArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun LongArray.plus(elements: Collection): LongArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun FloatArray.plus(elements: Collection): FloatArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun DoubleArray.plus(elements: Collection): DoubleArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun BooleanArray.plus(elements: Collection): BooleanArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public expect operator fun CharArray.plus(elements: Collection): CharArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun Array.plus(elements: Array): Array - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun ByteArray.plus(elements: ByteArray): ByteArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun ShortArray.plus(elements: ShortArray): ShortArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun IntArray.plus(elements: IntArray): IntArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun LongArray.plus(elements: LongArray): LongArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun FloatArray.plus(elements: FloatArray): FloatArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public expect operator fun CharArray.plus(elements: CharArray): CharArray - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public expect fun Array.plusElement(element: T): Array - -/** - * Sorts the array in-place. - */ -public expect fun IntArray.sort(): Unit - -/** - * Sorts the array in-place. - */ -public expect fun LongArray.sort(): Unit - -/** - * Sorts the array in-place. - */ -public expect fun ByteArray.sort(): Unit - -/** - * Sorts the array in-place. - */ -public expect fun ShortArray.sort(): Unit - -/** - * Sorts the array in-place. - */ -public expect fun DoubleArray.sort(): Unit - -/** - * Sorts the array in-place. - */ -public expect fun FloatArray.sort(): Unit - -/** - * Sorts the array in-place. - */ -public expect fun CharArray.sort(): Unit - -/** - * Sorts the array in-place according to the natural order of its elements. - */ -public expect fun > Array.sort(): Unit - -/** - * Sorts the array in-place according to the order specified by the given [comparator]. - */ -public expect fun Array.sortWith(comparator: Comparator): Unit - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun ByteArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun ShortArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun IntArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun LongArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun FloatArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun DoubleArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun BooleanArray.toTypedArray(): Array - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public expect fun CharArray.toTypedArray(): Array - diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index f52ba7e04dd..075c398dd22 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -3175,6 +3175,13 @@ public inline fun Array<*>.filterIsInstance(): List<@kotlin.internal return filterIsInstanceTo(ArrayList()) } +/** + * Returns a list containing all elements that are instances of specified class. + */ +public fun Array<*>.filterIsInstance(klass: Class): List { + return filterIsInstanceTo(ArrayList(), klass) +} + /** * Appends all elements that are instances of specified type parameter R to the given [destination]. */ @@ -3183,6 +3190,15 @@ public inline fun > Array<*>.filterIsInst return destination } +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + /** * Returns a list containing all elements not matching the given [predicate]. */ @@ -5157,6 +5173,260 @@ public fun CharArray.sortedWith(comparator: Comparator): List { return toTypedArray().apply { sortWith(comparator) }.asList() } +/** + * Returns a [List] that wraps the original array. + */ +public fun Array.asList(): List { + return ArraysUtilJVM.asList(this) +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun ByteArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Byte): Boolean = this@asList.contains(element) + override fun get(index: Int): Byte = this@asList[index] + override fun indexOf(element: Byte): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Byte): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun ShortArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Short): Boolean = this@asList.contains(element) + override fun get(index: Int): Short = this@asList[index] + override fun indexOf(element: Short): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Short): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun IntArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Int): Boolean = this@asList.contains(element) + override fun get(index: Int): Int = this@asList[index] + override fun indexOf(element: Int): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Int): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun LongArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Long): Boolean = this@asList.contains(element) + override fun get(index: Int): Long = this@asList[index] + override fun indexOf(element: Long): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Long): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun FloatArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Float): Boolean = this@asList.contains(element) + override fun get(index: Int): Float = this@asList[index] + override fun indexOf(element: Float): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Float): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun DoubleArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Double): Boolean = this@asList.contains(element) + override fun get(index: Int): Double = this@asList[index] + override fun indexOf(element: Double): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Double): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun BooleanArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Boolean): Boolean = this@asList.contains(element) + override fun get(index: Int): Boolean = this@asList[index] + override fun indexOf(element: Boolean): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Boolean): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Returns a [List] that wraps the original array. + */ +public fun CharArray.asList(): List { + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: Char): Boolean = this@asList.contains(element) + override fun get(index: Int): Char = this@asList[index] + override fun indexOf(element: Char): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: Char): Int = this@asList.lastIndexOf(element) + } +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted according to the specified [comparator]. + */ +public fun Array.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element, comparator) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + +/** + * Searches the array or the range of the array for the provided [element] using the binary search algorithm. + * The array is expected to be sorted, otherwise the result is undefined. + * + * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + * + * @return the index of the element, if it is contained in the array within the specified range; + * otherwise, the inverted insertion point `(-insertion point - 1)`. + * The insertion point is defined as the index at which the element should be inserted, + * so that the array (or the specified subrange of array) still remains sorted. + */ +public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int { + return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) +} + /** * Returns `true` if the two specified arrays are *deeply* equal to one another, * i.e. contain the same number of the same elements in the same order. @@ -5467,6 +5737,285 @@ public inline fun CharArray.contentToString(): String { return java.util.Arrays.toString(this) } +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun Array.copyOf(): Array { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.copyOf(): ByteArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.copyOf(): ShortArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.copyOf(): IntArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.copyOf(): LongArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.copyOf(): FloatArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.copyOf(): DoubleArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun BooleanArray.copyOf(): BooleanArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array. + */ +@kotlin.internal.InlineOnly +public inline fun CharArray.copyOf(): CharArray { + return java.util.Arrays.copyOf(this, size) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.copyOf(newSize: Int): ByteArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.copyOf(newSize: Int): ShortArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.copyOf(newSize: Int): IntArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.copyOf(newSize: Int): LongArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.copyOf(newSize: Int): FloatArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.copyOf(newSize: Int): DoubleArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun BooleanArray.copyOf(newSize: Int): BooleanArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun CharArray.copyOf(newSize: Int): CharArray { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of the original array, resized to the given [newSize]. + */ +@kotlin.internal.InlineOnly +public inline fun Array.copyOf(newSize: Int): Array { + return java.util.Arrays.copyOf(this, newSize) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Returns new array which is a copy of range of original array. + */ +@kotlin.internal.InlineOnly +public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { + return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) +} + +/** + * Fills original array with the provided value. + */ +public fun Array.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + +/** + * Fills original array with the provided value. + */ +public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.fill(this, fromIndex, toIndex, element) +} + /** * Returns the range of valid indices for the array. */ @@ -5719,6 +6268,430 @@ public val BooleanArray.lastIndex: Int public val CharArray.lastIndex: Int get() = size - 1 +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun Array.plus(element: T): Array { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun ByteArray.plus(element: Byte): ByteArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun ShortArray.plus(element: Short): ShortArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun IntArray.plus(element: Int): IntArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun LongArray.plus(element: Long): LongArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun FloatArray.plus(element: Float): FloatArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun DoubleArray.plus(element: Double): DoubleArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun BooleanArray.plus(element: Boolean): BooleanArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +public operator fun CharArray.plus(element: Char): CharArray { + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun Array.plus(elements: Collection): Array { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun ByteArray.plus(elements: Collection): ByteArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun ShortArray.plus(elements: Collection): ShortArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun IntArray.plus(elements: Collection): IntArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun LongArray.plus(elements: Collection): LongArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun FloatArray.plus(elements: Collection): FloatArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun DoubleArray.plus(elements: Collection): DoubleArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun BooleanArray.plus(elements: Collection): BooleanArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. + */ +public operator fun CharArray.plus(elements: Collection): CharArray { + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun Array.plus(elements: Array): Array { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun ByteArray.plus(elements: ByteArray): ByteArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun ShortArray.plus(elements: ShortArray): ShortArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun IntArray.plus(elements: IntArray): IntArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun LongArray.plus(elements: LongArray): LongArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun FloatArray.plus(elements: FloatArray): FloatArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then all elements of the given [elements] array. + */ +public operator fun CharArray.plus(elements: CharArray): CharArray { + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result +} + +/** + * Returns an array containing all elements of the original array and then the given [element]. + */ +@kotlin.internal.InlineOnly +public inline fun Array.plusElement(element: T): Array { + return plus(element) +} + +/** + * Sorts the array in-place. + */ +public fun IntArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun LongArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun ByteArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun ShortArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun DoubleArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun FloatArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place. + */ +public fun CharArray.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts the array in-place according to the natural order of its elements. + */ +@kotlin.internal.InlineOnly +public inline fun > Array.sort(): Unit { + @Suppress("UNCHECKED_CAST") + (this as Array).sort() +} + +/** + * Sorts the array in-place according to the natural order of its elements. + * + * @throws ClassCastException if any element of the array is not [Comparable]. + */ +public fun Array.sort(): Unit { + if (size > 1) java.util.Arrays.sort(this) +} + +/** + * Sorts a range in the array in-place. + */ +public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts a range in the array in-place. + */ +public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex) +} + +/** + * Sorts the array in-place according to the order specified by the given [comparator]. + */ +public fun Array.sortWith(comparator: Comparator): Unit { + if (size > 1) java.util.Arrays.sort(this, comparator) +} + +/** + * Sorts a range in the array in-place with the given [comparator]. + */ +public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { + java.util.Arrays.sort(this, fromIndex, toIndex, comparator) +} + /** * Returns an array of Boolean containing all of the elements of this generic array. */ @@ -5799,6 +6772,94 @@ public fun Array.toShortArray(): ShortArray { return result } +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + /** * Returns a [Map] containing key-value pairs provided by [transform] function * applied to elements of the given array. @@ -12988,1064 +14049,3 @@ public fun DoubleArray.sum(): Double { return sum } -/** - * Returns a [List] that wraps the original array. - */ -public fun Array.asList(): List { - return ArraysUtilJVM.asList(this) -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun ByteArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Byte): Boolean = this@asList.contains(element) - override fun get(index: Int): Byte = this@asList[index] - override fun indexOf(element: Byte): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Byte): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun ShortArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Short): Boolean = this@asList.contains(element) - override fun get(index: Int): Short = this@asList[index] - override fun indexOf(element: Short): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Short): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun IntArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Int): Boolean = this@asList.contains(element) - override fun get(index: Int): Int = this@asList[index] - override fun indexOf(element: Int): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Int): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun LongArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Long): Boolean = this@asList.contains(element) - override fun get(index: Int): Long = this@asList[index] - override fun indexOf(element: Long): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Long): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun FloatArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Float): Boolean = this@asList.contains(element) - override fun get(index: Int): Float = this@asList[index] - override fun indexOf(element: Float): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Float): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun DoubleArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Double): Boolean = this@asList.contains(element) - override fun get(index: Int): Double = this@asList[index] - override fun indexOf(element: Double): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Double): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun BooleanArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Boolean): Boolean = this@asList.contains(element) - override fun get(index: Int): Boolean = this@asList[index] - override fun indexOf(element: Boolean): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Boolean): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns a [List] that wraps the original array. - */ -public fun CharArray.asList(): List { - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: Char): Boolean = this@asList.contains(element) - override fun get(index: Int): Char = this@asList[index] - override fun indexOf(element: Char): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: Char): Int = this@asList.lastIndexOf(element) - } -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.copyOf(): Array { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.copyOf(): ByteArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.copyOf(): ShortArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.copyOf(): IntArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.copyOf(): LongArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.copyOf(): FloatArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.copyOf(): DoubleArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.copyOf(): BooleanArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.copyOf(): CharArray { - return java.util.Arrays.copyOf(this, size) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.copyOf(newSize: Int): ByteArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.copyOf(newSize: Int): ShortArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.copyOf(newSize: Int): IntArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.copyOf(newSize: Int): LongArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.copyOf(newSize: Int): FloatArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.copyOf(newSize: Int): DoubleArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.copyOf(newSize: Int): BooleanArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.copyOf(newSize: Int): CharArray { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of the original array, resized to the given [newSize]. - */ -@kotlin.internal.InlineOnly -public inline fun Array.copyOf(newSize: Int): Array { - return java.util.Arrays.copyOf(this, newSize) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun ByteArray.copyOfRange(fromIndex: Int, toIndex: Int): ByteArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun ShortArray.copyOfRange(fromIndex: Int, toIndex: Int): ShortArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun IntArray.copyOfRange(fromIndex: Int, toIndex: Int): IntArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun LongArray.copyOfRange(fromIndex: Int, toIndex: Int): LongArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun FloatArray.copyOfRange(fromIndex: Int, toIndex: Int): FloatArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun DoubleArray.copyOfRange(fromIndex: Int, toIndex: Int): DoubleArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun BooleanArray.copyOfRange(fromIndex: Int, toIndex: Int): BooleanArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns new array which is a copy of range of original array. - */ -@kotlin.internal.InlineOnly -public inline fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray { - return java.util.Arrays.copyOfRange(this, fromIndex, toIndex) -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun Array.plus(element: T): Array { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun ByteArray.plus(element: Byte): ByteArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun ShortArray.plus(element: Short): ShortArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun IntArray.plus(element: Int): IntArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun LongArray.plus(element: Long): LongArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun FloatArray.plus(element: Float): FloatArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun DoubleArray.plus(element: Double): DoubleArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun BooleanArray.plus(element: Boolean): BooleanArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -public operator fun CharArray.plus(element: Char): CharArray { - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun Array.plus(elements: Collection): Array { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ByteArray.plus(elements: Collection): ByteArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun ShortArray.plus(elements: Collection): ShortArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun IntArray.plus(elements: Collection): IntArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun LongArray.plus(elements: Collection): LongArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun FloatArray.plus(elements: Collection): FloatArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun DoubleArray.plus(elements: Collection): DoubleArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun BooleanArray.plus(elements: Collection): BooleanArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] collection. - */ -public operator fun CharArray.plus(elements: Collection): CharArray { - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun Array.plus(elements: Array): Array { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun ByteArray.plus(elements: ByteArray): ByteArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun ShortArray.plus(elements: ShortArray): ShortArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun IntArray.plus(elements: IntArray): IntArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun LongArray.plus(elements: LongArray): LongArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun FloatArray.plus(elements: FloatArray): FloatArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then all elements of the given [elements] array. - */ -public operator fun CharArray.plus(elements: CharArray): CharArray { - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result -} - -/** - * Returns an array containing all elements of the original array and then the given [element]. - */ -@kotlin.internal.InlineOnly -public inline fun Array.plusElement(element: T): Array { - return plus(element) -} - -/** - * Sorts the array in-place. - */ -public fun IntArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun LongArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun ByteArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun ShortArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun DoubleArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun FloatArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place. - */ -public fun CharArray.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts the array in-place according to the natural order of its elements. - */ -@kotlin.internal.InlineOnly -public inline fun > Array.sort(): Unit { - @Suppress("UNCHECKED_CAST") - (this as Array).sort() -} - -/** - * Sorts the array in-place according to the order specified by the given [comparator]. - */ -public fun Array.sortWith(comparator: Comparator): Unit { - if (size > 1) java.util.Arrays.sort(this, comparator) -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ByteArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ShortArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun IntArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun LongArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun FloatArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun DoubleArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun BooleanArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun CharArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted according to the specified [comparator]. - */ -public fun Array.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element, comparator) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Searches the array or the range of the array for the provided [element] using the binary search algorithm. - * The array is expected to be sorted, otherwise the result is undefined. - * - * If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - * - * @return the index of the element, if it is contained in the array within the specified range; - * otherwise, the inverted insertion point `(-insertion point - 1)`. - * The insertion point is defined as the index at which the element should be inserted, - * so that the array (or the specified subrange of array) still remains sorted. - */ -public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int { - return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun Array.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Fills original array with the provided value. - */ -public fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.fill(this, fromIndex, toIndex, element) -} - -/** - * Returns a list containing all elements that are instances of specified class. - */ -public fun Array<*>.filterIsInstance(klass: Class): List { - return filterIsInstanceTo(ArrayList(), klass) -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - -/** - * Sorts the array in-place according to the natural order of its elements. - * - * @throws ClassCastException if any element of the array is not [Comparable]. - */ -public fun Array.sort(): Unit { - if (size > 1) java.util.Arrays.sort(this) -} - -/** - * Sorts a range in the array in-place. - */ -public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place. - */ -public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex) -} - -/** - * Sorts a range in the array in-place with the given [comparator]. - */ -public fun Array.sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Unit { - java.util.Arrays.sort(this, fromIndex, toIndex, comparator) -} - diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 45edfb77084..69adf28e996 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -644,6 +644,13 @@ public inline fun Iterable<*>.filterIsInstance(): List<@kotlin.inter return filterIsInstanceTo(ArrayList()) } +/** + * Returns a list containing all elements that are instances of specified class. + */ +public fun Iterable<*>.filterIsInstance(klass: Class): List { + return filterIsInstanceTo(ArrayList(), klass) +} + /** * Appends all elements that are instances of specified type parameter R to the given [destination]. */ @@ -652,6 +659,15 @@ public inline fun > Iterable<*>.filterIsI return destination } +/** + * Appends all elements that are instances of specified class to the given [destination]. + */ +public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + /** * Returns a list containing all elements not matching the given [predicate]. */ @@ -2353,19 +2369,3 @@ public fun Iterable.sum(): Double { return sum } -/** - * Returns a list containing all elements that are instances of specified class. - */ -public fun Iterable<*>.filterIsInstance(klass: Class): List { - return filterIsInstanceTo(ArrayList(), klass) -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index b4435f10c23..1ed267b4877 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -397,6 +397,16 @@ public inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.i return filter { it is R } as Sequence } +/** + * Returns a sequence containing all elements that are instances of specified class. + * + * The operation is _intermediate_ and _stateless_. + */ +public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { + @Suppress("UNCHECKED_CAST") + return filter { klass.isInstance(it) } as Sequence +} + /** * Appends all elements that are instances of specified type parameter R to the given [destination]. * @@ -407,6 +417,17 @@ public inline fun > Sequence<*>.filterIsI return destination } +/** + * Appends all elements that are instances of specified class to the given [destination]. + * + * The operation is _terminal_. + */ +public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination +} + /** * Returns a sequence containing all elements not matching the given [predicate]. * @@ -1851,20 +1872,3 @@ public fun Sequence.sum(): Double { return sum } -/** - * Returns a sequence containing all elements that are instances of specified class. - */ -public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { - @Suppress("UNCHECKED_CAST") - return filter { klass.isInstance(it) } as Sequence -} - -/** - * Appends all elements that are instances of specified class to the given [destination]. - */ -public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination -} - diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index e5f9c873d98..26f7f4b17d2 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -26,10 +26,7 @@ fun main(args: Array) { SequenceOps, RangeOps, Numeric, - ComparableOps, - CommonArrays, - PlatformSpecialized, - PlatformSpecializedJS + ComparableOps ) require(args.size == 1) { "Expecting Kotlin project home path as an argument" } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 19286f7ca2e..41883b718ca 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -235,4 +235,470 @@ object ArrayOps : TemplateGroupBase() { } } } + + val f_plusElement = fn("plusElement(element: T)") { + include(InvariantArraysOfObjects) + } builder { + returns("SELF") + doc { "Returns an array containing all elements of the original array and then the given [element]." } + + on(Platform.JVM) { + inlineOnly() + body { "return plus(element)" } + } + on(Platform.JS) { + family = ArraysOfObjects + inline(suppressWarning = true) + returns("Array") + body { + """ + return this.asDynamic().concat(arrayOf(element)) + """ + } + } + } + + val f_plus = fn("plus(element: T)") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) + } builderWith { primitive -> + doc { "Returns an array containing all elements of the original array and then the given [element]." } + operator() + returns("SELF") + + on(Platform.JVM) { + body { + """ + val index = size + val result = java.util.Arrays.copyOf(this, index + 1) + result[index] = element + return result + """ + } + } + + on(Platform.JS) { + inline(suppressWarning = true) + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + returns("Array") + } + + body { + if (primitive == null) + "return this.asDynamic().concat(arrayOf(element))" + else + "return plus(${primitive.name.toLowerCase()}ArrayOf(element))" + } + } + } + + + val f_plus_collection = fn("plus(elements: Collection)") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) + } builder { + operator() + returns("SELF") + doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." } + on(Platform.JVM) { + body { + """ + var index = size + val result = java.util.Arrays.copyOf(this, index + elements.size) + for (element in elements) result[index++] = element + return result + """ + } + } + on(Platform.JS) { + // TODO: inline arrayPlusCollection when @PublishedAPI is available +// inline(Platform.JS, Inline.Yes) +// annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""") + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + returns("Array") + } + when (primitive) { + null, PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return arrayPlusCollection(this, elements)" } + else -> + body { "return fillFromCollection(this.copyOf(size + elements.size), this.size, elements)" } + } + } + } + + val f_plus_array = fn("plus(elements: SELF)") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) + } builder { + operator(true) + doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." } + returns("SELF") + specialFor(InvariantArraysOfObjects) { + signature("plus(elements: Array)", notForSorting = true) + } + + on(Platform.JVM) { + body { + """ + val thisSize = size + val arraySize = elements.size + val result = java.util.Arrays.copyOf(this, thisSize + arraySize) + System.arraycopy(elements, 0, result, thisSize, arraySize) + return result + """ + } + + } + on(Platform.JS) { + inline(suppressWarning = true) + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + returns("Array") + body { """return this.asDynamic().concat(elements)""" } + } + specialFor(ArraysOfPrimitives) { + body { """return primitiveArrayConcat(this, elements)""" } + } + } + } + + + val f_copyOfRange = fn("copyOfRange(fromIndex: Int, toIndex: Int)") { + include(InvariantArraysOfObjects, ArraysOfPrimitives) + } builderWith { primitive -> + doc { "Returns new array which is a copy of range of original array." } + returns("SELF") + + on(Platform.JS) { + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + returns("Array") + } + when(primitive) { + PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return withType(\"${primitive}Array\", this.asDynamic().slice(fromIndex, toIndex))" } + else -> { + inline(suppressWarning = true) + body { "return this.asDynamic().slice(fromIndex, toIndex)" } + } + } + } + on(Platform.JVM) { + inlineOnly() + body { "return java.util.Arrays.copyOfRange(this, fromIndex, toIndex)" } + } + } + + val f_copyOf = fn("copyOf()") { + include(InvariantArraysOfObjects) + include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives) + } builder { + doc { "Returns new array which is a copy of the original array." } + returns("SELF") + on(Platform.JVM) { + inlineOnly() + body { "return java.util.Arrays.copyOf(this, size)" } + } + on(Platform.JS) { + specialFor(InvariantArraysOfObjects) { + family = ArraysOfObjects + returns("Array") + } + when (primitive) { + null -> { + inline(suppressWarning = true) + body { "return this.asDynamic().slice()" } + } + PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return withType(\"${primitive}Array\", this.asDynamic().slice())" } + else -> { + inline(suppressWarning = true) + body { "return this.asDynamic().slice()" } + } + } + } + } + + val f_copyOf_newSize = fn("copyOf(newSize: Int)") { + include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives) + include(InvariantArraysOfObjects) + } builder { + doc { "Returns new array which is a copy of the original array, resized to the given [newSize]." } + specialFor(ArraysOfPrimitives) { + returns("SELF") + on(Platform.JS) { + when (primitive!!) { + PrimitiveType.Boolean -> + body { "return withType(\"BooleanArray\", arrayCopyResize(this, newSize, false))" } + PrimitiveType.Char -> + body { "return withType(\"CharArray\", fillFrom(this, ${primitive}Array(newSize)))" } + PrimitiveType.Long -> + body { "return withType(\"LongArray\", arrayCopyResize(this, newSize, ZERO))" } + else -> + body { "return fillFrom(this, ${primitive}Array(newSize))" } + } + } + + } + specialFor(InvariantArraysOfObjects) { + returns("Array") + on(Platform.JS) { + family = ArraysOfObjects + body { "return arrayCopyResize(this, newSize, null)" } + } + } + on(Platform.JVM) { + inlineOnly() + body { + "return java.util.Arrays.copyOf(this, newSize)" + } + } + } + + val f_sort = fn("sort()") { + include(ArraysOfPrimitives, PrimitiveType.numericPrimitives + PrimitiveType.Char) + include(ArraysOfObjects) + } builder { + typeParam("T: Comparable") + doc { "Sorts the array in-place according to the natural order of its elements." } + specialFor(ArraysOfPrimitives) { + doc { "Sorts the array in-place." } + } + + returns("Unit") + on(Platform.JS) { + body { + """ + if (size > 1) + sort { a: T, b: T -> a.compareTo(b) } + """ + } + specialFor(ArraysOfPrimitives) { + if (primitive != PrimitiveType.Long) { + annotation("""@library("primitiveArraySort")""") + body { "definedExternally" } + } + } + } + on(Platform.JVM) { + specialFor(ArraysOfObjects) { + inlineOnly() + body { + """ + @Suppress("UNCHECKED_CAST") + (this as Array).sort() + """ + } + } + specialFor(ArraysOfPrimitives) { + body { + "if (size > 1) java.util.Arrays.sort(this)" + } + } + } + } + + val f_sortWith = fn("sortWith(comparator: Comparator)") { + include(ArraysOfObjects) + } builder { + doc { "Sorts the array in-place according to the order specified by the given [comparator]." } + returns("Unit") + on(Platform.JVM) { + body { + "if (size > 1) java.util.Arrays.sort(this, comparator)" + } + } + on(Platform.JS) { + body { + """ + if (size > 1) + sort { a, b -> comparator.compare(a, b) } + """ + } + } + } + + val f_sort_comparison = fn("sort(noinline comparison: (a: T, b: T) -> Int)") { + platforms(Platform.JS) + include(ArraysOfObjects, ArraysOfPrimitives) + exclude(PrimitiveType.Boolean) + } builder { + inlineOnly() + returns("Unit") + doc { "Sorts the array in-place according to the order specified by the given [comparison] function." } + body { "asDynamic().sort(comparison)" } + } + + val f_sort_objects = fn("sort()") { + // left with more generic signature for JVM only + platforms(Platform.JVM) + include(ArraysOfObjects) + } builder { + doc { + """ + Sorts the array in-place according to the natural order of its elements. + + @throws ClassCastException if any element of the array is not [Comparable]. + """ + } + returns("Unit") + body { + "if (size > 1) java.util.Arrays.sort(this)" + } + } + + val f_sort_range = fn("sort(fromIndex: Int = 0, toIndex: Int = size)") { + platforms(Platform.JVM) + include(ArraysOfObjects, ArraysOfPrimitives) + exclude(PrimitiveType.Boolean) + } builder { + doc { "Sorts a range in the array in-place." } + returns("Unit") + body { + "java.util.Arrays.sort(this, fromIndex, toIndex)" + } + } + + val f_sortWith_range = fn("sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size)") { + platforms(Platform.JVM) + include(ArraysOfObjects) + } builder { + doc { "Sorts a range in the array in-place with the given [comparator]." } + returns("Unit") + body { + "java.util.Arrays.sort(this, fromIndex, toIndex, comparator)" + } + } + + + + val f_asList = fn("asList()") { + include(ArraysOfObjects, ArraysOfPrimitives) + } builder { + doc { "Returns a [List] that wraps the original array." } + returns("List") + on(Platform.JVM) { + body { """return ArraysUtilJVM.asList(this)""" } + } + on(Platform.JS) { + body { """return ArrayList(this.unsafeCast>())""" } + } + + specialFor(ArraysOfPrimitives) { + val objectLiteralImpl = """ + return object : AbstractList(), RandomAccess { + override val size: Int get() = this@asList.size + override fun isEmpty(): Boolean = this@asList.isEmpty() + override fun contains(element: T): Boolean = this@asList.contains(element) + override fun get(index: Int): T = this@asList[index] + override fun indexOf(element: T): Int = this@asList.indexOf(element) + override fun lastIndexOf(element: T): Int = this@asList.lastIndexOf(element) + } + """ + on(Platform.JVM) { + body { objectLiteralImpl } + } + on(Platform.JS) { + if (primitive == PrimitiveType.Char) { + body { objectLiteralImpl } + } + else { + inlineOnly() + body { "return this.unsafeCast>().asList()" } + } + } + } + } + + val f_toTypedArray = fn("toTypedArray()") { + include(ArraysOfPrimitives) + } builder { + returns("Array") + doc { + """ + Returns a *typed* object array containing all of the elements of this primitive array. + """ + } + on(Platform.JVM) { + body { + """ + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array + """ + } + } + on(Platform.JS) { + when (primitive) { + PrimitiveType.Char -> + body { "return Array(size, { i -> this[i] })" } + PrimitiveType.Boolean, PrimitiveType.Long -> + body { "return copyOf().unsafeCast>()" } + else -> + body { "return js(\"[]\").slice.call(this)" } + } + + } + } + + val f_fill = fn("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") { + platforms(Platform.JVM) + include(InvariantArraysOfObjects, ArraysOfPrimitives) + } builder { + doc { "Fills original array with the provided value." } + returns("Unit") + body { + """ + java.util.Arrays.fill(this, fromIndex, toIndex, element) + """ + } + } + + val f_binarySearch = fn("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") { + platforms(Platform.JVM) + include(ArraysOfObjects, ArraysOfPrimitives) + exclude(PrimitiveType.Boolean) + } builder { + doc { + """ + Searches the array or the range of the array for the provided [element] using the binary search algorithm. + The array is expected to be sorted, otherwise the result is undefined. + + If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + + @return the index of the element, if it is contained in the array within the specified range; + otherwise, the inverted insertion point `(-insertion point - 1)`. + The insertion point is defined as the index at which the element should be inserted, + so that the array (or the specified subrange of array) still remains sorted. + """ + } + returns("Int") + body { + "return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element)" + } + } + + val f_binarySearch_comparator = fn("binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size)") { + platforms(Platform.JVM) + include(ArraysOfObjects) + } builder { + doc { + """ + Searches the array or the range of the array for the provided [element] using the binary search algorithm. + The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. + + If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. + + @return the index of the element, if it is contained in the array within the specified range; + otherwise, the inverted insertion point `(-insertion point - 1)`. + The insertion point is defined as the index at which the element should be inserted, + so that the array (or the specified subrange of array) still remains sorted according to the specified [comparator]. + """ + } + returns("Int") + body { + "return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element, comparator)" + } + } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index 269dc7f0ef8..3fcc5f99565 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -755,6 +755,51 @@ object Filtering : TemplateGroupBase() { } } + val f_filterIsInstanceTo_class = fn("filterIsInstanceTo(destination: C, klass: Class)") { + platforms(Platform.JVM) + include(Iterables, ArraysOfObjects, Sequences) + } builder { + doc { "Appends all elements that are instances of specified class to the given [destination]." } + receiverAsterisk = true + typeParam("C : MutableCollection") + typeParam("R") + returns("C") + body { + """ + @Suppress("UNCHECKED_CAST") + for (element in this) if (klass.isInstance(element)) destination.add(element as R) + return destination + """ + } + } + + val f_filterIsInstance_class = fn("filterIsInstance(klass: Class)") { + platforms(Platform.JVM) + include(Iterables, ArraysOfObjects, Sequences) + } builder { + doc { "Returns a list containing all elements that are instances of specified class." } + receiverAsterisk= true + typeParam("R") + returns("List") + body { + """ + return filterIsInstanceTo(ArrayList(), klass) + """ + } + + specialFor(Sequences) { + doc { "Returns a sequence containing all elements that are instances of specified class." } + returns("Sequence") + } + body(Sequences) { + """ + @Suppress("UNCHECKED_CAST") + return filter { klass.isInstance(it) } as Sequence + """ + } + } + + val f_slice = fn("slice(indices: Iterable)") { include(CharSequences, Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt deleted file mode 100644 index dd8e16a4468..00000000000 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ /dev/null @@ -1,536 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package templates - -import templates.Family.* - -object PlatformSpecialized : TemplateGroupBase() { - - val f_fill = fn("fill(element: T, fromIndex: Int = 0, toIndex: Int = size)") { - platforms(Platform.JVM) - include(InvariantArraysOfObjects, ArraysOfPrimitives) - } builder { - doc { "Fills original array with the provided value." } - returns("Unit") - body { - """ - java.util.Arrays.fill(this, fromIndex, toIndex, element) - """ - } - } - - val f_binarySearch = fn("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") { - platforms(Platform.JVM) - include(ArraysOfObjects, ArraysOfPrimitives) - exclude(PrimitiveType.Boolean) - } builder { - doc { - """ - Searches the array or the range of the array for the provided [element] using the binary search algorithm. - The array is expected to be sorted, otherwise the result is undefined. - - If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - - @return the index of the element, if it is contained in the array within the specified range; - otherwise, the inverted insertion point `(-insertion point - 1)`. - The insertion point is defined as the index at which the element should be inserted, - so that the array (or the specified subrange of array) still remains sorted. - """ - } - returns("Int") - body { - "return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element)" - } - } - - val f_binarySearch_comparator = fn("binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size)") { - platforms(Platform.JVM) - include(ArraysOfObjects) - } builder { - doc { - """ - Searches the array or the range of the array for the provided [element] using the binary search algorithm. - The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. - - If the array contains multiple elements equal to the specified [element], there is no guarantee which one will be found. - - @return the index of the element, if it is contained in the array within the specified range; - otherwise, the inverted insertion point `(-insertion point - 1)`. - The insertion point is defined as the index at which the element should be inserted, - so that the array (or the specified subrange of array) still remains sorted according to the specified [comparator]. - """ - } - returns("Int") - body { - "return java.util.Arrays.binarySearch(this, fromIndex, toIndex, element, comparator)" - } - } - - - val f_sort = fn("sort()") { - // left with more generic signature for JVM only - platforms(Platform.JVM) - include(ArraysOfObjects) - } builder { - doc { - """ - Sorts the array in-place according to the natural order of its elements. - - @throws ClassCastException if any element of the array is not [Comparable]. - """ - } - returns("Unit") - body { - "if (size > 1) java.util.Arrays.sort(this)" - } - } - - val f_sort_range = fn("sort(fromIndex: Int = 0, toIndex: Int = size)") { - platforms(Platform.JVM) - include(ArraysOfObjects, ArraysOfPrimitives) - exclude(PrimitiveType.Boolean) - } builder { - doc { "Sorts a range in the array in-place." } - returns("Unit") - body { - "java.util.Arrays.sort(this, fromIndex, toIndex)" - } - } - - val f_sortWith = fn("sortWith(comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size)") { - platforms(Platform.JVM) - include(ArraysOfObjects) - } builder { - doc { "Sorts a range in the array in-place with the given [comparator]." } - returns("Unit") - body { - "java.util.Arrays.sort(this, fromIndex, toIndex, comparator)" - } - } - - val f_filterIsInstanceTo = fn("filterIsInstanceTo(destination: C, klass: Class)") { - platforms(Platform.JVM) - include(Iterables, ArraysOfObjects, Sequences) - } builder { - doc { "Appends all elements that are instances of specified class to the given [destination]." } - receiverAsterisk = true - typeParam("C : MutableCollection") - typeParam("R") - returns("C") - body { - """ - @Suppress("UNCHECKED_CAST") - for (element in this) if (klass.isInstance(element)) destination.add(element as R) - return destination - """ - } - } - - val f_filterIsInstance = fn("filterIsInstance(klass: Class)") { - platforms(Platform.JVM) - include(Iterables, ArraysOfObjects, Sequences) - } builder { - doc { "Returns a list containing all elements that are instances of specified class." } - receiverAsterisk= true - typeParam("R") - returns("List") - body { - """ - return filterIsInstanceTo(ArrayList(), klass) - """ - } - - specialFor(Sequences) { - doc { "Returns a sequence containing all elements that are instances of specified class." } - returns("Sequence") - } - body(Sequences) { - """ - @Suppress("UNCHECKED_CAST") - return filter { klass.isInstance(it) } as Sequence - """ - } - } -} - -object PlatformSpecializedJS : TemplateGroupBase() { - val f_sort = fn("sort(noinline comparison: (a: T, b: T) -> Int)") { - platforms(Platform.JS) - include(ArraysOfObjects, ArraysOfPrimitives) - exclude(PrimitiveType.Boolean) - } builder { - inlineOnly() - returns("Unit") - doc { "Sorts the array in-place according to the order specified by the given [comparison] function." } - body { "asDynamic().sort(comparison)" } - } -} - -object CommonArrays : TemplateGroupBase() { - - val f_plusElement = fn("plusElement(element: T)") { - include(InvariantArraysOfObjects) - } builder { - returns("SELF") - doc { "Returns an array containing all elements of the original array and then the given [element]." } - - on(Platform.JVM) { - inlineOnly() - body { "return plus(element)" } - } - on(Platform.JS) { - family = ArraysOfObjects - inline(suppressWarning = true) - returns("Array") - body { - """ - return this.asDynamic().concat(arrayOf(element)) - """ - } - } - } - - val f_plus = fn("plus(element: T)") { - include(InvariantArraysOfObjects, ArraysOfPrimitives) - } builderWith { primitive -> - doc { "Returns an array containing all elements of the original array and then the given [element]." } - operator() - returns("SELF") - - on(Platform.JVM) { - body { - """ - val index = size - val result = java.util.Arrays.copyOf(this, index + 1) - result[index] = element - return result - """ - } - } - - on(Platform.JS) { - inline(suppressWarning = true) - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - returns("Array") - } - - body { - if (primitive == null) - "return this.asDynamic().concat(arrayOf(element))" - else - "return plus(${primitive.name.toLowerCase()}ArrayOf(element))" - } - } - } - - - val f_plus_collection = fn("plus(elements: Collection)") { - include(InvariantArraysOfObjects, ArraysOfPrimitives) - } builder { - operator() - returns("SELF") - doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] collection." } - on(Platform.JVM) { - body { - """ - var index = size - val result = java.util.Arrays.copyOf(this, index + elements.size) - for (element in elements) result[index++] = element - return result - """ - } - } - on(Platform.JS) { - // TODO: inline arrayPlusCollection when @PublishedAPI is available -// inline(Platform.JS, Inline.Yes) -// annotations(Platform.JS, """@Suppress("NOTHING_TO_INLINE")""") - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - returns("Array") - } - when (primitive) { - null, PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return arrayPlusCollection(this, elements)" } - else -> - body { "return fillFromCollection(this.copyOf(size + elements.size), this.size, elements)" } - } - } - } - - val f_plus_array = fn("plus(elements: SELF)") { - include(InvariantArraysOfObjects, ArraysOfPrimitives) - } builder { - operator(true) - doc { "Returns an array containing all elements of the original array and then all elements of the given [elements] array." } - returns("SELF") - specialFor(InvariantArraysOfObjects) { - signature("plus(elements: Array)", notForSorting = true) - } - - on(Platform.JVM) { - body { - """ - val thisSize = size - val arraySize = elements.size - val result = java.util.Arrays.copyOf(this, thisSize + arraySize) - System.arraycopy(elements, 0, result, thisSize, arraySize) - return result - """ - } - - } - on(Platform.JS) { - inline(suppressWarning = true) - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - returns("Array") - body { """return this.asDynamic().concat(elements)""" } - } - specialFor(ArraysOfPrimitives) { - body { """return primitiveArrayConcat(this, elements)""" } - } - } - } - - - val f_copyOfRange = fn("copyOfRange(fromIndex: Int, toIndex: Int)") { - include(InvariantArraysOfObjects, ArraysOfPrimitives) - } builderWith { primitive -> - doc { "Returns new array which is a copy of range of original array." } - returns("SELF") - - on(Platform.JS) { - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - returns("Array") - } - when(primitive) { - PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return withType(\"${primitive}Array\", this.asDynamic().slice(fromIndex, toIndex))" } - else -> { - inline(suppressWarning = true) - body { "return this.asDynamic().slice(fromIndex, toIndex)" } - } - } - } - on(Platform.JVM) { - inlineOnly() - body { "return java.util.Arrays.copyOfRange(this, fromIndex, toIndex)" } - } - } - - val f_copyOf = fn("copyOf()") { - include(InvariantArraysOfObjects) - include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives) - } builder { - doc { "Returns new array which is a copy of the original array." } - returns("SELF") - on(Platform.JVM) { - inlineOnly() - body { "return java.util.Arrays.copyOf(this, size)" } - } - on(Platform.JS) { - specialFor(InvariantArraysOfObjects) { - family = ArraysOfObjects - returns("Array") - } - when (primitive) { - null -> { - inline(suppressWarning = true) - body { "return this.asDynamic().slice()" } - } - PrimitiveType.Char, PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return withType(\"${primitive}Array\", this.asDynamic().slice())" } - else -> { - inline(suppressWarning = true) - body { "return this.asDynamic().slice()" } - } - } - } - } - - val f_copyOf_newSize = fn("copyOf(newSize: Int)") { - include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives) - include(InvariantArraysOfObjects) - } builder { - doc { "Returns new array which is a copy of the original array, resized to the given [newSize]." } - specialFor(ArraysOfPrimitives) { - returns("SELF") - on(Platform.JS) { - when (primitive!!) { - PrimitiveType.Boolean -> - body { "return withType(\"BooleanArray\", arrayCopyResize(this, newSize, false))" } - PrimitiveType.Char -> - body { "return withType(\"CharArray\", fillFrom(this, ${primitive}Array(newSize)))" } - PrimitiveType.Long -> - body { "return withType(\"LongArray\", arrayCopyResize(this, newSize, ZERO))" } - else -> - body { "return fillFrom(this, ${primitive}Array(newSize))" } - } - } - - } - specialFor(InvariantArraysOfObjects) { - returns("Array") - on(Platform.JS) { - family = ArraysOfObjects - body { "return arrayCopyResize(this, newSize, null)" } - } - } - on(Platform.JVM) { - inlineOnly() - body { - "return java.util.Arrays.copyOf(this, newSize)" - } - } - } - - fun f_sort() = fn("sort()") { - include(ArraysOfPrimitives, PrimitiveType.numericPrimitives + PrimitiveType.Char) - include(ArraysOfObjects) - } builder { - typeParam("T: Comparable") - doc { "Sorts the array in-place according to the natural order of its elements." } - specialFor(ArraysOfPrimitives) { - doc { "Sorts the array in-place." } - } - - returns("Unit") - on(Platform.JS) { - body { - """ - if (size > 1) - sort { a: T, b: T -> a.compareTo(b) } - """ - } - specialFor(ArraysOfPrimitives) { - if (primitive != PrimitiveType.Long) { - annotation("""@library("primitiveArraySort")""") - body { "definedExternally" } - } - } - } - on(Platform.JVM) { - specialFor(ArraysOfObjects) { - inlineOnly() - body { - """ - @Suppress("UNCHECKED_CAST") - (this as Array).sort() - """ - } - } - specialFor(ArraysOfPrimitives) { - body { - "if (size > 1) java.util.Arrays.sort(this)" - } - } - } - } - - fun f_sortWith() = fn("sortWith(comparator: Comparator)") { - include(ArraysOfObjects) - } builder { - doc { "Sorts the array in-place according to the order specified by the given [comparator]." } - returns("Unit") - on(Platform.JVM) { - body { - "if (size > 1) java.util.Arrays.sort(this, comparator)" - } - } - on(Platform.JS) { - body { - """ - if (size > 1) - sort { a, b -> comparator.compare(a, b) } - """ - } - } - } - - fun f_asList() = fn("asList()") { - include(ArraysOfObjects, ArraysOfPrimitives) - } builder { - doc { "Returns a [List] that wraps the original array." } - returns("List") - on(Platform.JVM) { - body { """return ArraysUtilJVM.asList(this)""" } - } - on(Platform.JS) { - body { """return ArrayList(this.unsafeCast>())""" } - } - - specialFor(ArraysOfPrimitives) { - val objectLiteralImpl = """ - return object : AbstractList(), RandomAccess { - override val size: Int get() = this@asList.size - override fun isEmpty(): Boolean = this@asList.isEmpty() - override fun contains(element: T): Boolean = this@asList.contains(element) - override fun get(index: Int): T = this@asList[index] - override fun indexOf(element: T): Int = this@asList.indexOf(element) - override fun lastIndexOf(element: T): Int = this@asList.lastIndexOf(element) - } - """ - on(Platform.JVM) { - body { objectLiteralImpl } - } - on(Platform.JS) { - if (primitive == PrimitiveType.Char) { - body { objectLiteralImpl } - } - else { - inlineOnly() - body { "return this.unsafeCast>().asList()" } - } - } - } - } - - fun f_toTypedArray() = fn("toTypedArray()") { - include(ArraysOfPrimitives) - } builder { - returns("Array") - doc { - """ - Returns a *typed* object array containing all of the elements of this primitive array. - """ - } - on(Platform.JVM) { - body { - """ - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array - """ - } - } - on(Platform.JS) { - when (primitive) { - PrimitiveType.Char -> - body { "return Array(size, { i -> this[i] })" } - PrimitiveType.Boolean, PrimitiveType.Long -> - body { "return copyOf().unsafeCast>()" } - else -> - body { "return js(\"[]\").slice.call(this)" } - } - - } - } -}