diff --git a/libraries/stdlib/js/src/kotlin/collections/HashMapEntry.kt b/libraries/stdlib/js/src/kotlin/collections/HashMapEntry.kt index 821a3aedff2..26d182c7e61 100644 --- a/libraries/stdlib/js/src/kotlin/collections/HashMapEntry.kt +++ b/libraries/stdlib/js/src/kotlin/collections/HashMapEntry.kt @@ -68,7 +68,6 @@ internal abstract class HashMapEntrySetBase> internal override val size: Int get() = backing.size override fun isEmpty(): Boolean = backing.size == 0 override fun contains(element: E): Boolean = backing.containsEntry(element) - protected abstract fun getEntry(element: Map.Entry): E? override fun clear() = backing.clear() override fun add(element: E): Boolean = throw UnsupportedOperationException() override fun addAll(elements: Collection): Boolean = throw UnsupportedOperationException() @@ -85,8 +84,5 @@ internal abstract class HashMapEntrySetBase> internal internal class HashMapEntrySet internal constructor( backing: InternalMap, ) : HashMapEntrySetBase>(backing) { - - override fun getEntry(element: Map.Entry): MutableMap.MutableEntry? = backing.getEntry(element) - override fun iterator(): MutableIterator> = backing.entriesIterator() } diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt index 3d806018022..dbc6cf088fd 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt @@ -425,15 +425,6 @@ internal class InternalHashMap private constructor( return containsEntry(entry as Map.Entry) } - override fun getEntry(entry: Map.Entry): MutableMap.MutableEntry? { - val index = findKey(entry.key) - return if (index < 0 || valuesArray!![index] != entry.value) { - null - } else { - EntryRef(this, index) - } - } - private fun contentEquals(other: Map<*, *>): Boolean = _size == other.size && containsAllEntries(other.entries) private fun putEntry(entry: Map.Entry): Boolean { diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt index e12cefa9b23..172a93be17f 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalMap.kt @@ -15,7 +15,6 @@ internal interface InternalMap { fun putAll(from: Map) operator fun get(key: K): V? - fun getEntry(entry: Map.Entry): MutableMap.MutableEntry? operator fun contains(key: K): Boolean fun containsValue(value: V): Boolean diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalStringLinkedMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalStringLinkedMap.kt index bda373d04e3..ac96e6647fe 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalStringLinkedMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalStringLinkedMap.kt @@ -60,6 +60,12 @@ internal class InternalStringLinkedMap : InternalStringMap() { return prevValue } + private fun IntArray.updateIndexAt(indexReference: Int, newIndex: Int) { + if (indexReference >= 0) { + this[indexReference] = newIndex + } + } + override fun removeKeyIndex(key: K, removingIndex: Int) { super.removeKeyIndex(key, removingIndex) @@ -67,8 +73,8 @@ internal class InternalStringLinkedMap : InternalStringMap() { val nextIndex = nextIndexes[removingIndex] val prevIndex = prevIndexes[removingIndex] - nextIndexes[prevIndex] = nextIndex - prevIndexes[nextIndex] = prevIndex + nextIndexes.updateIndexAt(prevIndex, nextIndex) + prevIndexes.updateIndexAt(nextIndex, prevIndex) if (headIndex == removingIndex) { headIndex = nextIndex @@ -82,8 +88,8 @@ internal class InternalStringLinkedMap : InternalStringMap() { if (removingIndex != lastIndex) { nextIndexes[removingIndex] = nextIndexes[lastIndex] prevIndexes[removingIndex] = prevIndexes[lastIndex] - nextIndexes[prevIndexes[removingIndex]] = removingIndex - prevIndexes[nextIndexes[removingIndex]] = removingIndex + nextIndexes.updateIndexAt(prevIndexes[removingIndex], removingIndex) + prevIndexes.updateIndexAt(nextIndexes[removingIndex], removingIndex) if (headIndex == lastIndex) { headIndex = removingIndex } diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt index c0ee7acd9bc..e252c3004cb 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalStringMap.kt @@ -96,28 +96,19 @@ internal open class InternalStringMap : InternalMap { override val size: Int get() = keys.length + private fun findKeyIndex(key: K): Int? { + if (key !is String) return null + val index = backingMap[key] + return if (index !== undefined) index.unsafeCast() else null + } + override operator fun contains(key: K): Boolean { - if (key !is String) return false - return backingMap[key] !== undefined + return findKeyIndex(key) != null } override operator fun get(key: K): V? { - if (key !is String) return null - val index = backingMap[key] - return if (index !== undefined) values.getElement(index.unsafeCast()) else null - } - - override fun getEntry(entry: Map.Entry): MutableEntry? { - val key = entry.key as? String ?: return null - val index = backingMap[key] - if (index === undefined) { - return null - } - val value = values.getElement(index.unsafeCast()) - if (value == entry.value) { - return EntryRef(entry.key, value, this) - } - return null + val index = findKeyIndex(key) ?: return null + return values.getElement(index) } override fun containsValue(value: V): Boolean { @@ -125,7 +116,8 @@ internal open class InternalStringMap : InternalMap { } override fun containsEntry(entry: Map.Entry): Boolean { - return getEntry(entry) != null + val index = findKeyIndex(entry.key) ?: return false + return values.getElement(index) == entry.value } override fun containsOtherEntry(entry: Map.Entry<*, *>): Boolean { @@ -134,8 +126,12 @@ internal open class InternalStringMap : InternalMap { } override fun removeEntry(entry: Map.Entry): Boolean { - val key = getEntry(entry)?.key ?: return false - return remove(key) != null + val index = findKeyIndex(entry.key) ?: return false + if (values.getElement(index) == entry.value) { + removeKeyIndex(keys.getElement(index), index) + return true + } + return false } override fun removeValue(value: V): Boolean { @@ -171,17 +167,10 @@ internal open class InternalStringMap : InternalMap { } override fun remove(key: K): V? { - if (key !is String) return null - val index = backingMap[key] - if (index === undefined) { - return null - } - - val i = index.unsafeCast() - val removedValue = values.getElement(i) - - removeKeyIndex(key, i) - return removedValue + val index = findKeyIndex(key) ?: return null + val removingValue = values.getElement(index) + removeKeyIndex(key, index) + return removingValue } internal open fun removeKeyIndex(key: K, removingIndex: Int) {