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
+4 -38
View File
@@ -48,20 +48,8 @@ Kotlin.arrayToString = function (a) {
return "[" + Array.prototype.map.call(a, function(e) { return toString(e); }).join(", ") + "]";
};
Kotlin.arrayDeepToString = function (a, visited) {
visited = visited || [a];
var toString = Kotlin.isCharArray(a) ? String.fromCharCode : Kotlin.toString;
return "[" + Array.prototype.map.call(a, function (e) {
if (Kotlin.isArrayish(e) && visited.indexOf(e) < 0) {
visited.push(e);
var result = Kotlin.arrayDeepToString(e, visited);
visited.pop();
return result;
}
else {
return toString(e);
}
}).join(", ") + "]";
Kotlin.arrayDeepToString = function (arr) {
return Kotlin.kotlin.collections.contentDeepToStringImpl(arr);
};
Kotlin.arrayEquals = function (a, b) {
@@ -81,24 +69,7 @@ Kotlin.arrayEquals = function (a, b) {
};
Kotlin.arrayDeepEquals = function (a, b) {
if (a === b) {
return true;
}
if (!Kotlin.isArrayish(b) || a.length !== b.length) {
return false;
}
for (var i = 0, n = a.length; i < n; i++) {
if (Kotlin.isArrayish(a[i])) {
if (!Kotlin.arrayDeepEquals(a[i], b[i])) {
return false;
}
}
else if (!Kotlin.equals(a[i], b[i])) {
return false;
}
}
return true;
return Kotlin.kotlin.collections.contentDeepEqualsImpl(a, b);
};
Kotlin.arrayHashCode = function (arr) {
@@ -110,12 +81,7 @@ Kotlin.arrayHashCode = function (arr) {
};
Kotlin.arrayDeepHashCode = function (arr) {
var result = 1;
for (var i = 0, n = arr.length; i < n; i++) {
var e = arr[i];
result = ((31 * result | 0) + (Kotlin.isArrayish(e) ? Kotlin.arrayDeepHashCode(e) : Kotlin.hashCode(e))) | 0;
}
return result;
return Kotlin.kotlin.collections.contentDeepHashCodeImpl(arr);
};
Kotlin.primitiveArraySort = function (array) {
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.collections
@UseExperimental(ExperimentalUnsignedTypes::class)
@SinceKotlin("1.3")
@kotlin.js.JsName("contentDeepHashCodeImpl")
internal fun <T> Array<out T>.contentDeepHashCodeImpl(): Int {
var result = 1
for (element in this) {
val elementHash = when {
element == null -> 0
js("Kotlin").isArrayish(element) -> (element.unsafeCast<Array<*>>()).contentDeepHashCodeImpl()
element is UByteArray -> element.contentHashCode()
element is UShortArray -> element.contentHashCode()
element is UIntArray -> element.contentHashCode()
element is ULongArray -> element.contentHashCode()
else -> element.hashCode()
}
result = 31 * result + elementHash
}
return result
}