diff --git a/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt b/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt index 668bf8678d6..d18ec9910f9 100644 --- a/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt +++ b/libraries/stdlib/js/src/kotlin/collections/InternalHashMap.kt @@ -134,11 +134,11 @@ internal class InternalHashMap private constructor( } override fun remove(key: K): V? { - val index = removeKey(key) // mutability gets checked here + checkIsMutable() + val index = findKey(key) if (index < 0) return null - val valuesArray = valuesArray!! - val oldValue = valuesArray[index] - valuesArray.resetAt(index) + val oldValue = valuesArray!![index] + removeEntryAt(index) return oldValue } @@ -348,16 +348,9 @@ internal class InternalHashMap private constructor( } } - private fun removeKey(key: K): Int { - checkIsMutable() - val index = findKey(key) - if (index < 0) return TOMBSTONE - removeKeyAt(index) - return index - } - - private fun removeKeyAt(index: Int) { + private fun removeEntryAt(index: Int) { keysArray.resetAt(index) + valuesArray?.resetAt(index) removeHashAt(presenceArray[index]) presenceArray[index] = TOMBSTONE _size-- @@ -459,7 +452,7 @@ internal class InternalHashMap private constructor( val index = findKey(entry.key) if (index < 0) return false if (valuesArray!![index] != entry.value) return false - removeKeyAt(index) + removeEntryAt(index) return true } @@ -467,7 +460,7 @@ internal class InternalHashMap private constructor( checkIsMutable() val index = findValue(value) if (index < 0) return false - removeKeyAt(index) + removeEntryAt(index) return true } @@ -508,7 +501,7 @@ internal class InternalHashMap private constructor( checkForComodification() check(lastIndex != -1) { "Call next() before removing element from the iterator." } map.checkIsMutable() - map.removeKeyAt(lastIndex) + map.removeEntryAt(lastIndex) lastIndex = -1 expectedModCount = map.modCount } diff --git a/libraries/stdlib/jvm/src/kotlin/collections/builders/MapBuilder.kt b/libraries/stdlib/jvm/src/kotlin/collections/builders/MapBuilder.kt index ee302978a8c..afe5afda85a 100644 --- a/libraries/stdlib/jvm/src/kotlin/collections/builders/MapBuilder.kt +++ b/libraries/stdlib/jvm/src/kotlin/collections/builders/MapBuilder.kt @@ -102,11 +102,11 @@ internal class MapBuilder private constructor( } override fun remove(key: K): V? { - val index = removeKey(key) // mutability gets checked here + checkIsMutable() + val index = findKey(key) if (index < 0) return null - val valuesArray = valuesArray!! - val oldValue = valuesArray[index] - valuesArray.resetAt(index) + val oldValue = valuesArray!![index] + removeEntryAt(index) return oldValue } @@ -342,16 +342,17 @@ internal class MapBuilder private constructor( } } - internal fun removeKey(key: K): Int { + internal fun removeKey(key: K): Boolean { checkIsMutable() val index = findKey(key) - if (index < 0) return TOMBSTONE - removeKeyAt(index) - return index + if (index < 0) return false + removeEntryAt(index) + return true } - private fun removeKeyAt(index: Int) { + private fun removeEntryAt(index: Int) { keysArray.resetAt(index) + valuesArray?.resetAt(index) removeHashAt(presenceArray[index]) presenceArray[index] = TOMBSTONE size-- @@ -463,7 +464,7 @@ internal class MapBuilder private constructor( val index = findKey(entry.key) if (index < 0) return false if (valuesArray!![index] != entry.value) return false - removeKeyAt(index) + removeEntryAt(index) return true } @@ -471,7 +472,7 @@ internal class MapBuilder private constructor( checkIsMutable() val index = findValue(element) if (index < 0) return false - removeKeyAt(index) + removeEntryAt(index) return true } @@ -514,7 +515,7 @@ internal class MapBuilder private constructor( checkForComodification() check(lastIndex != -1) { "Call next() before removing element from the iterator." } map.checkIsMutable() - map.removeKeyAt(lastIndex) + map.removeEntryAt(lastIndex) lastIndex = -1 expectedModCount = map.modCount } @@ -618,7 +619,7 @@ internal class MapBuilderKeys internal constructor( override fun clear() = backing.clear() override fun add(element: E): Boolean = throw UnsupportedOperationException() override fun addAll(elements: Collection): Boolean = throw UnsupportedOperationException() - override fun remove(element: E): Boolean = backing.removeKey(element) >= 0 + override fun remove(element: E): Boolean = backing.removeKey(element) override fun iterator(): MutableIterator = backing.keysIterator() override fun removeAll(elements: Collection): Boolean { diff --git a/libraries/stdlib/jvm/src/kotlin/collections/builders/SetBuilder.kt b/libraries/stdlib/jvm/src/kotlin/collections/builders/SetBuilder.kt index 96104235fa1..5197b48aedc 100644 --- a/libraries/stdlib/jvm/src/kotlin/collections/builders/SetBuilder.kt +++ b/libraries/stdlib/jvm/src/kotlin/collections/builders/SetBuilder.kt @@ -34,7 +34,7 @@ internal class SetBuilder internal constructor( override fun contains(element: E): Boolean = backing.containsKey(element) override fun clear() = backing.clear() override fun add(element: E): Boolean = backing.addKey(element) >= 0 - override fun remove(element: E): Boolean = backing.removeKey(element) >= 0 + override fun remove(element: E): Boolean = backing.removeKey(element) override fun iterator(): MutableIterator = backing.keysIterator() override fun addAll(elements: Collection): Boolean { diff --git a/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt b/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt index c34e9c7614b..8df313f4c9d 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/collections/HashMap.kt @@ -136,11 +136,11 @@ public actual class HashMap private constructor( } override actual fun remove(key: K): V? { - val index = removeKey(key) // mutability gets checked here + checkIsMutable() + val index = findKey(key) if (index < 0) return null - val valuesArray = valuesArray!! - val oldValue = valuesArray[index] - valuesArray.resetAt(index) + val oldValue = valuesArray!![index] + removeEntryAt(index) return oldValue } @@ -379,16 +379,17 @@ public actual class HashMap private constructor( } } - internal fun removeKey(key: K): Int { + internal fun removeKey(key: K): Boolean { checkIsMutable() val index = findKey(key) - if (index < 0) return TOMBSTONE - removeKeyAt(index) - return index + if (index < 0) return false + removeEntryAt(index) + return true } - private fun removeKeyAt(index: Int) { + private fun removeEntryAt(index: Int) { keysArray.resetAt(index) + valuesArray?.resetAt(index) removeHashAt(presenceArray[index]) presenceArray[index] = TOMBSTONE _size-- @@ -518,7 +519,7 @@ public actual class HashMap private constructor( val index = findKey(entry.key) if (index < 0) return false if (valuesArray!![index] != entry.value) return false - removeKeyAt(index) + removeEntryAt(index) return true } @@ -526,7 +527,7 @@ public actual class HashMap private constructor( checkIsMutable() val index = findValue(element) if (index < 0) return false - removeKeyAt(index) + removeEntryAt(index) return true } @@ -577,7 +578,7 @@ public actual class HashMap private constructor( checkForComodification() check(lastIndex != -1) { "Call next() before removing element from the iterator." } map.checkIsMutable() - map.removeKeyAt(lastIndex) + map.removeEntryAt(lastIndex) lastIndex = -1 expectedModCount = map.modCount } @@ -682,7 +683,7 @@ internal class HashMapKeys internal constructor( override fun clear() = backing.clear() override fun add(element: E): Boolean = throw UnsupportedOperationException() override fun addAll(elements: Collection): Boolean = throw UnsupportedOperationException() - override fun remove(element: E): Boolean = backing.removeKey(element) >= 0 + override fun remove(element: E): Boolean = backing.removeKey(element) override fun iterator(): MutableIterator = backing.keysIterator() override fun removeAll(elements: Collection): Boolean { diff --git a/libraries/stdlib/native-wasm/src/kotlin/collections/HashSet.kt b/libraries/stdlib/native-wasm/src/kotlin/collections/HashSet.kt index 51d3caa6c7c..1d0adbcc125 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/collections/HashSet.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/collections/HashSet.kt @@ -70,7 +70,7 @@ public actual class HashSet internal constructor( override fun getElement(element: E): E? = backing.getKey(element) override actual fun clear(): Unit = backing.clear() override actual fun add(element: E): Boolean = backing.addKey(element) >= 0 - override actual fun remove(element: E): Boolean = backing.removeKey(element) >= 0 + override actual fun remove(element: E): Boolean = backing.removeKey(element) override actual fun iterator(): MutableIterator = backing.keysIterator() override actual fun addAll(elements: Collection): Boolean {