From 178ae83e8d2a044170f3321672cb3c7c66ab07bf Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Wed, 7 May 2014 17:11:54 +0400 Subject: [PATCH] sort and binarySearch methods now have correct default toIndex parameter #KT-4963 Fixed --- libraries/stdlib/src/generated/_SpecialJVM.kt | 32 +++++++++---------- .../stdlib/test/collections/ArraysJVMTest.kt | 8 +++++ .../src/templates/SpecialJVM.kt | 4 +-- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt index de5b594df43..ec882315705 100644 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ b/libraries/stdlib/src/generated/_SpecialJVM.kt @@ -10,56 +10,56 @@ import java.util.* /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size - 1): Int { +public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } @@ -372,56 +372,56 @@ public fun , R : T> Stream.filterIsInstanceTo( /** * Sorts array or range in array inplace */ -public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size - 1): Unit { +public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { Arrays.sort(this, fromIndex, toIndex) } diff --git a/libraries/stdlib/test/collections/ArraysJVMTest.kt b/libraries/stdlib/test/collections/ArraysJVMTest.kt index 77c3f802466..a29049e08db 100644 --- a/libraries/stdlib/test/collections/ArraysJVMTest.kt +++ b/libraries/stdlib/test/collections/ArraysJVMTest.kt @@ -5,6 +5,14 @@ import org.junit.Test as test class ArraysJVMTest { + test fun sort() { + var a = intArray(5, 2, 1, 4, 3) + var b = intArray(1, 2, 3, 4, 5) + a.sort() + for (i in a.indices) + expect(b[i]) { a[i] } + } + test fun copyOf() { checkContent(booleanArray(true, false, true, false, true, false).copyOf().iterator(), 6) { it % 2 == 0 } checkContent(byteArray(0, 1, 2, 3, 4, 5).copyOf().iterator(), 6) { it.toByte() } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index c132354146b..b47b9e59fdf 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -53,7 +53,7 @@ fun specialJVM(): List { } } - templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size - 1)") { + templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") { only(ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) doc { "Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted." } @@ -63,7 +63,7 @@ fun specialJVM(): List { } } - templates add f("sort(fromIndex: Int = 0, toIndex: Int = size - 1)") { + templates add f("sort(fromIndex: Int = 0, toIndex: Int = size)") { only(ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) doc { "Sorts array or range in array inplace" }