All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.implementations.immutableMap.PersistentHashMapContentViews.kt Maven / Gradle / Ivy

There is a newer version: 0.3.8
Show newest version
/*
 * Copyright 2016-2019 JetBrains s.r.o.
 * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
 */

package kotlinx.collections.immutable.implementations.immutableMap

import kotlinx.collections.immutable.ImmutableCollection
import kotlinx.collections.immutable.ImmutableSet

internal class PersistentHashMapEntries(private val map: PersistentHashMap) : ImmutableSet>, AbstractSet>() {
    override val size: Int get() = map.size

    override fun contains(element: Map.Entry): Boolean {
        // TODO: Eliminate this check after KT-30016 gets fixed.
        if ((element as Any?) !is Map.Entry<*, *>) return false
        return map[element.key]?.let { candidate -> candidate == element.value }
                ?: (element.value == null && map.containsKey(element.key))
    }

    override fun iterator(): Iterator> {
        return PersistentHashMapEntriesIterator(map.node)
    }
}

internal class PersistentHashMapKeys(private val map: PersistentHashMap) : ImmutableSet, AbstractSet() {
    override val size: Int
        get() = map.size

    override fun contains(element: K): Boolean {
        return map.containsKey(element)
    }

    override fun iterator(): Iterator {
        return PersistentHashMapKeysIterator(map.node)
    }
}

internal class PersistentHashMapValues(private val map: PersistentHashMap) : ImmutableCollection, AbstractCollection() {
    override val size: Int
        get() = map.size

    override fun contains(element: V): Boolean {
        return map.containsValue(element)
    }

    override fun iterator(): Iterator {
        return PersistentHashMapValuesIterator(map.node)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy