diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 1d1aec1a73f..b03b7a07148 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -3618,8 +3618,8 @@ public fun ShortArray.slice(indices: Iterable): List { /** * Returns an array containing elements of this array at specified [indices]. */ -public fun Array.sliceArray(indices: Collection): Array { - val result = arrayOfNulls(this, indices.size) as Array +public fun Array.sliceArray(indices: Collection): Array { + val result = arrayOfNulls(this, indices.size) var targetIndex = 0 for (sourceIndex in indices) { result[targetIndex++] = this[sourceIndex] @@ -3726,7 +3726,7 @@ public fun ShortArray.sliceArray(indices: Collection): ShortArray { /** * Returns a list containing elements at indices in the specified [indices] range. */ -public fun Array.sliceArray(indices: IntRange): Array { +public fun Array.sliceArray(indices: IntRange): Array { if (indices.isEmpty()) return copyOfRange(0, 0) return copyOfRange(indices.start, indices.endInclusive + 1) } @@ -4302,15 +4302,14 @@ public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List Array.reverse(): Unit { +public fun Array.reverse(): Unit { val midPoint = (size / 2) - 1 if (midPoint < 0) return - val _this = this as Array var reverseIndex = lastIndex for (index in 0..midPoint) { - val tmp = _this[index] - _this[index] = _this[reverseIndex] - _this[reverseIndex] = tmp + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp reverseIndex-- } } @@ -4528,9 +4527,9 @@ public fun ShortArray.reversed(): List { /** * Returns an array with elements of this array in reversed order. */ -public fun Array.reversedArray(): Array { +public fun Array.reversedArray(): Array { if (isEmpty()) return this - val result = arrayOfNulls(this, size) as Array + val result = arrayOfNulls(this, size) val lastIndex = lastIndex for (i in 0..lastIndex) result[lastIndex - i] = this[i] @@ -4783,7 +4782,7 @@ public fun ShortArray.sorted(): List { /** * Returns an array with all elements of this array sorted according to their natural sort order. */ -public fun > Array.sortedArray(): Array { +public fun > Array.sortedArray(): Array { if (isEmpty()) return this return this.copyOf().apply { sort() } } @@ -4847,7 +4846,7 @@ public fun ShortArray.sortedArray(): ShortArray { /** * Returns an array with all elements of this array sorted descending according to their natural sort order. */ -public fun > Array.sortedArrayDescending(): Array { +public fun > Array.sortedArrayDescending(): Array { if (isEmpty()) return this return this.copyOf().apply { sortWith(reverseOrder()) } } diff --git a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt index 695d5ba7eff..ef0c7134856 100644 --- a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt @@ -87,6 +87,6 @@ public inline fun Collection.toTypedArray(): Array { public inline fun Array?.orEmpty(): Array = this ?: arrayOf() /** Internal unsafe construction of array based on reference array type */ -internal fun arrayOfNulls(reference: Array, size: Int): Array { - return java.lang.reflect.Array.newInstance(reference.javaClass.componentType, size) as Array +internal fun arrayOfNulls(reference: Array, size: Int): Array { + return java.lang.reflect.Array.newInstance(reference.javaClass.componentType, size) as Array } diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 184602195e4..524c5583681 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -495,8 +495,10 @@ class ArraysTest { val coll: Collection = listOf(3, 1, 2) assertArrayNotSameButEquals(arrayOf("B"), arrayOf("A", "B", "C").sliceArray(1..1)) + assertArrayNotSameButEquals(arrayOf("B"), (arrayOf("A", "B", "C") as Array).sliceArray(1..1)) assertArrayNotSameButEquals(arrayOf('E', 'B', 'C'), arrayOf('A', 'B', 'C', 'E').sliceArray(coll)) + assertArrayNotSameButEquals(arrayOf(), arrayOf().sliceArray(5..4)) assertArrayNotSameButEquals(intArrayOf(), intArrayOf(1, 2, 3).sliceArray(5..1)) assertArrayNotSameButEquals(intArrayOf(2, 3, 9), intArrayOf(2, 3, 9, 2, 3, 9).sliceArray(coll)) @@ -655,6 +657,7 @@ class ArraysTest { doTest(build = { map {'a' + it}.toCharArray() }, reverse = { reverse() }, snapshot = { toList() }) doTest(build = { map {it % 2 == 0}.toBooleanArray() }, reverse = { reverse() }, snapshot = { toList() }) doTest(build = { map {it.toString()}.toTypedArray() }, reverse = { reverse() }, snapshot = { toList() }) + doTest(build = { map {it.toString()}.toTypedArray() as Array }, reverse = { reverse() }, snapshot = { toList() }) } @@ -680,6 +683,7 @@ class ArraysTest { assertArrayNotSameButEquals(charArrayOf('3', '2', '1'), charArrayOf('1', '2', '3').reversedArray()) assertArrayNotSameButEquals(booleanArrayOf(false, false, true), booleanArrayOf(true, false, false).reversedArray()) assertArrayNotSameButEquals(arrayOf("3", "2", "1"), arrayOf("1", "2", "3").reversedArray()) + assertArrayNotSameButEquals(arrayOf("3", "2", "1"), (arrayOf("1", "2", "3") as Array).reversedArray()) } @test fun drop() { @@ -971,6 +975,11 @@ class ArraysTest { fun > arrayData(vararg values: T, toArray: Array.() -> A) = ArraySortedChecker(values.toArray(), naturalOrder()) with (arrayData("ac", "aD", "aba") { toList().toTypedArray() }) { + checkSorted>({ sorted() }, { sortedDescending() }, { iterator() }) + checkSorted>({ sortedArray() }, { sortedArrayDescending()}, { iterator() } ) + } + + with (arrayData("ac", "aD", "aba") { toList().toTypedArray() as Array }) { checkSorted>({ sorted() }, { sortedDescending() }, { iterator() }) checkSorted>({ sortedArray() }, { sortedArrayDescending()}, { iterator() } ) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index 607f283d3dd..8b9d677070c 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -585,12 +585,12 @@ fun filtering(): List { } templates add f("sliceArray(indices: Collection)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) doc { "Returns an array containing elements of this array at specified [indices]." } returns("SELF") - body(ArraysOfObjects) { + body(InvariantArraysOfObjects) { """ - val result = arrayOfNulls(this, indices.size) as Array + val result = arrayOfNulls(this, indices.size) var targetIndex = 0 for (sourceIndex in indices) { result[targetIndex++] = this[sourceIndex] @@ -611,10 +611,10 @@ fun filtering(): List { } templates add f("sliceArray(indices: IntRange)") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) doc { "Returns a list containing elements at indices in the specified [indices] range." } returns("SELF") - body(ArraysOfObjects) { + body(InvariantArraysOfObjects) { """ if (indices.isEmpty()) return copyOfRange(0, 0) return copyOfRange(indices.start, indices.endInclusive + 1) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index eaad1eb1943..b4f7119f3b9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -7,20 +7,18 @@ fun ordering(): List { templates add f("reverse()") { doc { f -> "Reverses ${f.element.pluralize()} in the ${f.collection} in-place." } - only(Lists, ArraysOfObjects, ArraysOfPrimitives) + only(Lists, InvariantArraysOfObjects, ArraysOfPrimitives) customReceiver(Lists) { "MutableList" } returns { "Unit" } body { f -> - val _this = if (f == ArraysOfObjects) "_this" else "this" """ val midPoint = (size / 2) - 1 if (midPoint < 0) return - ${if (f == ArraysOfObjects) "val _this = this as Array" else "" } var reverseIndex = lastIndex for (index in 0..midPoint) { - val tmp = $_this[index] - $_this[index] = $_this[reverseIndex] - $_this[reverseIndex] = tmp + val tmp = this[index] + this[index] = this[reverseIndex] + this[reverseIndex] = tmp reverseIndex-- } """ @@ -62,12 +60,12 @@ fun ordering(): List { templates add f("reversedArray()") { doc { "Returns an array with elements of this array in reversed order." } - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) returns("SELF") - body(ArraysOfObjects) { + body(InvariantArraysOfObjects) { """ if (isEmpty()) return this - val result = arrayOfNulls(this, size) as Array + val result = arrayOfNulls(this, size) val lastIndex = lastIndex for (i in 0..lastIndex) result[lastIndex - i] = this[i] @@ -134,7 +132,7 @@ fun ordering(): List { } templates add f("sortedArray()") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) doc { "Returns an array with all elements of this array sorted according to their natural sort order." @@ -196,14 +194,14 @@ fun ordering(): List { } templates add f("sortedArrayDescending()") { - only(ArraysOfObjects, ArraysOfPrimitives) + only(InvariantArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) doc { "Returns an array with all elements of this array sorted descending according to their natural sort order." } typeParam("T : Comparable") returns("SELF") - body(ArraysOfObjects) { + body(InvariantArraysOfObjects) { """ if (isEmpty()) return this return this.copyOf().apply { sortWith(reverseOrder()) }