[JS IR] Implement InternalMap interface

^KT-59001
This commit is contained in:
Alexander Korepanov
2023-06-19 14:43:19 +02:00
committed by Space Team
parent 6a77514a83
commit 6a1e14df0b
@@ -12,11 +12,11 @@ internal class InternalHashMap<K, V> private constructor(
private var hashArray: IntArray, private var hashArray: IntArray,
private var maxProbeDistance: Int, private var maxProbeDistance: Int,
private var length: Int, private var length: Int,
) { ) : InternalMap<K, V> {
private var hashShift: Int = computeShift(hashSize) private var hashShift: Int = computeShift(hashSize)
private var _size: Int = 0 private var _size: Int = 0
val size: Int override val size: Int
get() = _size get() = _size
private var keysView: HashMapKeys<K>? = null private var keysView: HashMapKeys<K>? = null
@@ -89,13 +89,17 @@ internal class InternalHashMap<K, V> private constructor(
fun containsKey(key: K): Boolean = findKey(key) >= 0 fun containsKey(key: K): Boolean = findKey(key) >= 0
fun containsValue(value: V): Boolean = findValue(value) >= 0 fun containsValue(value: V): Boolean = findValue(value) >= 0
operator fun get(key: K): V? { override operator fun get(key: K): V? {
val index = findKey(key) val index = findKey(key)
if (index < 0) return null if (index < 0) return null
return valuesArray!![index] return valuesArray!![index]
} }
fun put(key: K, value: V): V? { override fun contains(key: K): Boolean {
return containsKey(key)
}
override fun put(key: K, value: V): V? {
checkIsMutable() checkIsMutable()
val index = addKey(key) val index = addKey(key)
val valuesArray = allocateValuesArray() val valuesArray = allocateValuesArray()
@@ -114,7 +118,7 @@ internal class InternalHashMap<K, V> private constructor(
putAllEntries(from.entries) putAllEntries(from.entries)
} }
fun remove(key: K): V? { override fun remove(key: K): V? {
val index = removeKey(key) // mutability gets checked here val index = removeKey(key) // mutability gets checked here
if (index < 0) return null if (index < 0) return null
val valuesArray = valuesArray!! val valuesArray = valuesArray!!
@@ -123,7 +127,7 @@ internal class InternalHashMap<K, V> private constructor(
return oldValue return oldValue
} }
fun clear() { override fun clear() {
checkIsMutable() checkIsMutable()
// O(length) implementation for hashArray cleanup // O(length) implementation for hashArray cleanup
for (i in 0..length - 1) { for (i in 0..length - 1) {
@@ -139,6 +143,10 @@ internal class InternalHashMap<K, V> private constructor(
length = 0 length = 0
} }
override fun iterator(): MutableIterator<MutableMap.MutableEntry<K, V>> {
return entriesIterator()
}
val keys: MutableSet<K> val keys: MutableSet<K>
get() { get() {
val cur = keysView val cur = keysView