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

commonMain.org.antlr.v4.kotlinruntime.misc.DoubleKeyMap.kt Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
/*
 * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

package org.antlr.v4.kotlinruntime.misc

/** Sometimes we need to map a key to a value but key is two pieces of data.
 * This nested hash table saves creating a single key each time we access
 * map; avoids mem creation.
 */
class DoubleKeyMap {
    internal var data: MutableMap> = LinkedHashMap()

    fun put(k1: Key1, k2: Key2, v: Value): Value? {
        var data2: MutableMap? = data[k1]
        var prev: Value? = null
        if (data2 == null) {
            data2 = LinkedHashMap()
            data[k1] = data2
        } else {
            prev = data2[k2]
        }
        data2[k2] = v
        return prev
    }

    operator fun get(k1: Key1, k2: Key2): Value? {
        val data2 = data[k1] ?: return null
        return data2[k2]
    }

    operator fun get(k1: Key1): Map {
        return data[k1]!!
    }

    /** Get all values associated with primary key  */
    fun values(k1: Key1): Collection? {
        val data2 = data[k1] ?: return null
        return data2.values
    }

    /** get all primary keys  */
    fun keySet(): Set {
        return data.keys
    }

    /** get all secondary keys associated with a primary key  */
    fun keySet(k1: Key1): Set? {
        val data2 = data[k1] ?: return null
        return data2.keys
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy