Specialize contentDeepEquals/HashCode/ToString for arrays of unsigned types

#KT-26388
This commit is contained in:
Ilya Gorbunov
2018-09-12 02:41:27 +03:00
parent 3cc606577c
commit 2d356b89b5
8 changed files with 208 additions and 47 deletions
@@ -295,9 +295,13 @@ public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: In
* If any of arrays contains itself on any nesting level the behavior is undefined.
*/
@SinceKotlin("1.1")
@JvmName("contentDeepEqualsInline")
@kotlin.internal.InlineOnly
public actual inline infix fun <T> Array<out T>.contentDeepEquals(other: Array<out T>): Boolean {
return java.util.Arrays.deepEquals(this, other)
if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0))
return contentDeepEqualsImpl(other)
else
return java.util.Arrays.deepEquals(this, other)
}
/**
@@ -307,9 +311,13 @@ public actual inline infix fun <T> Array<out T>.contentDeepEquals(other: Array<o
* If any of arrays contains itself on any nesting level the behavior is undefined.
*/
@SinceKotlin("1.1")
@JvmName("contentDeepHashCodeInline")
@kotlin.internal.InlineOnly
public actual inline fun <T> Array<out T>.contentDeepHashCode(): Int {
return java.util.Arrays.deepHashCode(this)
if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0))
return contentDeepHashCodeImpl()
else
return java.util.Arrays.deepHashCode(this)
}
/**
@@ -322,9 +330,13 @@ public actual inline fun <T> Array<out T>.contentDeepHashCode(): Int {
* @sample samples.collections.Arrays.ContentOperations.contentDeepToString
*/
@SinceKotlin("1.1")
@JvmName("contentDeepToStringInline")
@kotlin.internal.InlineOnly
public actual inline fun <T> Array<out T>.contentDeepToString(): String {
return java.util.Arrays.deepToString(this)
if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0))
return contentDeepToStringImpl()
else
return java.util.Arrays.deepToString(this)
}
/**
@@ -47,4 +47,14 @@ internal actual fun <T> arrayOfNulls(reference: Array<T>, size: Int): Array<T> {
@SinceKotlin("1.3")
internal fun copyOfRangeToIndexCheck(toIndex: Int, size: Int) {
if (toIndex > size) throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).")
}
}
@SinceKotlin("1.3")
@PublishedApi
@kotlin.jvm.JvmName("contentDeepHashCode")
internal fun <T> Array<out T>.contentDeepHashCodeImpl(): Int =
// returns valid result for unsigned arrays by accident:
// hash code of an inline class, which an unsigned array is,
// is calculated structurally as in a data class
java.util.Arrays.deepHashCode(this)