Introduce contentEquals/HashCode/ToString for Arrays. Different methods for deep equals/hashCode/toString rather than overloads with a parameter, because most of the time it would be constant, and make these methods inline-only.

Make `contentEquals` infix function.
Add docs.
#KT-13582
This commit is contained in:
Ilya Gorbunov
2016-09-21 07:04:19 +03:00
parent ab2facf030
commit a868eecb1a
2 changed files with 384 additions and 0 deletions
+288
View File
@@ -5148,6 +5148,294 @@ public fun CharArray.sortedWith(comparator: Comparator<in Char>): List<Char> {
return toTypedArray().apply { sortWith(comparator) }.asList()
}
/**
* 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.
*
* If two corresponding elements are nested arrays, they are also compared deeply.
* If any of arrays contains itself on any nesting level the behavior is undefined.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun <T> Array<out T>.contentDeepEquals(other: Array<out T>): Boolean {
return Arrays.deepEquals(this, other)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*
* Nested arrays are treated as lists too.
* If any of arrays contains itself on any nesting level the behavior is undefined.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.contentDeepHashCode(): Int {
return Arrays.deepHashCode(this)
}
/**
* Returns a string representation of the contents of this array as if it is [List].
*
* Nested arrays are treated as lists too.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.contentDeepToString(): String {
return Arrays.deepToString(this)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun <T> Array<out T>.contentEquals(other: Array<out T>): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun ByteArray.contentEquals(other: ByteArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun ShortArray.contentEquals(other: ShortArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun IntArray.contentEquals(other: IntArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun LongArray.contentEquals(other: LongArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun FloatArray.contentEquals(other: FloatArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns `true` if the two specified arrays are *structurally* equal to one another,
* i.e. contain the same number of the same elements in the same order.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline infix fun CharArray.contentEquals(other: CharArray): Boolean {
return Arrays.equals(this, other)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun ByteArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun ShortArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun IntArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun LongArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun FloatArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun DoubleArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun BooleanArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a hash code based on the contents of this array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun CharArray.contentHashCode(): Int {
return Arrays.hashCode(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun ByteArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun ShortArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun IntArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun LongArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun FloatArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun DoubleArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun BooleanArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns a string representation of the contents of the specified array as if it is [List].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun CharArray.contentToString(): String {
return Arrays.toString(this)
}
/**
* Returns the range of valid indices for the array.
*/
@@ -44,6 +44,102 @@ fun arrays(): List<GenericFunction> {
}
}
templates add f("contentEquals(other: SELF)") {
only(ArraysOfObjects, ArraysOfPrimitives)
jvmOnly(true)
inline(Inline.Only)
infix(true)
doc {
"""
Returns `true` if the two specified arrays are *structurally* equal to one another,
i.e. contain the same number of the same elements in the same order.
"""
}
returns("Boolean")
body {
"return Arrays.equals(this, other)"
}
}
templates add f("contentDeepEquals(other: SELF)") {
only(ArraysOfObjects)
jvmOnly(true)
inline(Inline.Only)
infix(true)
doc {
"""
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.
If two corresponding elements are nested arrays, they are also compared deeply.
If any of arrays contains itself on any nesting level the behavior is undefined.
"""
}
returns("Boolean")
body {
"return Arrays.deepEquals(this, other)"
}
}
templates add f("contentToString()") {
only(ArraysOfObjects, ArraysOfPrimitives)
jvmOnly(true)
inline(Inline.Only)
doc { "Returns a string representation of the contents of the specified array as if it is [List]." }
returns("String")
body {
"return Arrays.toString(this)"
}
}
templates add f("contentDeepToString()") {
only(ArraysOfObjects)
jvmOnly(true)
inline(Inline.Only)
doc {
"""
Returns a string representation of the contents of this array as if it is [List].
Nested arrays are treated as lists too.
"""
}
returns("String")
body {
"return Arrays.deepToString(this)"
}
}
templates add f("contentHashCode()") {
only(ArraysOfObjects, ArraysOfPrimitives)
jvmOnly(true)
inline(Inline.Only)
doc {
"Returns a hash code based on the contents of this array as if it is [List]."
}
returns("Int")
body {
"return Arrays.hashCode(this)"
}
}
templates add f("contentDeepHashCode()") {
only(ArraysOfObjects)
jvmOnly(true)
inline(Inline.Only)
doc {
"""
Returns a hash code based on the contents of this array as if it is [List].
Nested arrays are treated as lists too.
If any of arrays contains itself on any nesting level the behavior is undefined.
"""
}
returns("Int")
body {
"return Arrays.deepHashCode(this)"
}
}
templates addAll PrimitiveType.defaultPrimitives.map { primitive ->
val arrayType = primitive.name + "Array"
f("to$arrayType()") {