From 0e302d26a829650528468c8b3d07daa82573abb2 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 10 Apr 2017 16:22:57 +0700 Subject: [PATCH] stdlib: Add contentEquals extension for arrays --- runtime/src/main/kotlin/kotlin/Arrays.kt | 189 +++++++++++++++++++++++ 1 file changed, 189 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index f857c8dfac1..083d9a5c76e 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -2803,6 +2803,195 @@ public inline fun Array.subarrayContentToString(offset: Int, length: return sb.toString() } +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun Array.contentEquals(other: Array): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun ByteArray.contentEquals(other: ByteArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun ShortArray.contentEquals(other: ShortArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun IntArray.contentEquals(other: IntArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun LongArray.contentEquals(other: LongArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun FloatArray.contentEquals(other: FloatArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + +/** + * 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. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline infix fun CharArray.contentEquals(other: CharArray): Boolean { + if (this === other) { + return true + } + if (size != other.size) { + return false + } + for (i in indices) { + if (this[i] != other[i]) { + return false + } + } + return true; +} + // From Library.kt. /** * Returns an array of objects of the given type with the given [size], initialized with null values.