Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

BiDiMap.kt
/**
 * Dracon - An IntelliJ-Pijul integration.
 * Copyright 2021 JonathanxD <jhrldev@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.github.jonathanxd.dracon.util

import com.intellij.util.containers.map2Array

interface BiDiMap<K, V> : Map<K, V> {
    val otherDirection: BiDiMap<V, K>
}

interface MutableBiDiMap<K, V> : BiDiMap<K, V>, MutableMap<K, V> {
    override val otherDirection: MutableBiDiMap<V, K>
}

class BiDiMapImpl<K, V>(val kvMap: Map<K, V>,
                        val vkMap: Map<V, K>): BiDiMap<K, V> {

    constructor(map: Map<K, V>): this(map, map.createVKMap())

    override val entries: Set<Map.Entry<K, V>>
        get() = this.kvMap.entries
    override val keys: Set<K>
        get() = this.kvMap.keys
    override val size: Int
        get() = this.kvMap.size
    override val values: Collection<V>
        get() = this.kvMap.values

    override fun containsKey(key: K): Boolean = this.kvMap.containsKey(key)
    override fun containsValue(value: V): Boolean = this.kvMap.containsValue(value)
    override fun get(key: K): V? = this.kvMap[key]
    override fun isEmpty(): Boolean = this.kvMap.isEmpty()

    override val otherDirection: BiDiMap<V, K> = OtherDirection()

    inner class OtherDirection: BiDiMap<V, K> {
        override val entries: Set<Map.Entry<V, K>>
            get() = this@BiDiMapImpl.vkMap.entries
        override val keys: Set<V>
            get() = this@BiDiMapImpl.vkMap.keys
        override val size: Int
            get() = this@BiDiMapImpl.vkMap.size
        override val values: Collection<K>
            get() = this@BiDiMapImpl.vkMap.values

        override fun containsKey(key: V): Boolean = this@BiDiMapImpl.vkMap.containsKey(key)
        override fun containsValue(value: K): Boolean = this@BiDiMapImpl.vkMap.containsValue(value)
        override fun get(key: V): K? = this@BiDiMapImpl.vkMap[key]
        override fun isEmpty(): Boolean = this@BiDiMapImpl.vkMap.isEmpty()

        override val otherDirection: BiDiMap<K, V>
            get() = this@BiDiMapImpl
    }

}

class MutableBiDiMapImpl<K, V>(val kvMap: MutableMap<K, V>,
                               val vkMap: MutableMap<V, K>): MutableBiDiMap<K, V> {

    constructor(map: MutableMap<K, V>): this(map, map.createVKMutableMap())

    override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
        get() = (this.kvMap.entries.zip(this.vkMap.entries)).mapTo(mutableSetOf()) {
            MutableBiDiSharedEntry(it.first, it.second, this.kvMap, this.vkMap)
        }

    override val keys: MutableSet<K>
        get() = this.kvMap.keys

    override val values: MutableCollection<V>
        get() = this.vkMap.keys

    override val size: Int
        get() = this.kvMap.size

    override fun containsKey(key: K): Boolean = this.kvMap.containsKey(key)
    override fun containsValue(value: V): Boolean = this.kvMap.containsValue(value)
    override fun get(key: K): V? = this.kvMap[key]
    override fun isEmpty(): Boolean = this.kvMap.isEmpty()

    override fun clear() {
        this.kvMap.clear()
        this.vkMap.clear()
    }

    override fun remove(key: K): V? {
        val value = this.kvMap.remove(key)
        this.vkMap.remove(value)
        return value
    }

    override fun put(key: K, value: V): V? {
        val oldValue = this.kvMap.put(key, value)
        this.vkMap[value] = key
        return oldValue
    }

    override fun putAll(from: Map<out K, V>) {
        for ((k, v) in from) {
            this[k] = v
        }
    }

    override val otherDirection: MutableBiDiMap<V, K> = OtherDirection()

    inner class OtherDirection: MutableBiDiMap<V, K> {
        override val entries: MutableSet<MutableMap.MutableEntry<V, K>>
            get() = this@MutableBiDiMapImpl.entries.mapTo(mutableSetOf()) {
                MutableBiDiRevEntry(it, this@OtherDirection)
            }

        override val keys: MutableSet<V>
            get() = this@MutableBiDiMapImpl.values.toMutableSet()

        override val values: MutableCollection<K>
            get() = this@MutableBiDiMapImpl.keys.toMutableSet()

        override val size: Int
            get() = this@MutableBiDiMapImpl.vkMap.size

        override fun containsKey(key: V): Boolean = this@MutableBiDiMapImpl.vkMap.containsKey(key)
        override fun containsValue(value: K): Boolean = this@MutableBiDiMapImpl.vkMap.containsValue(value)
        override fun get(key: V): K? = this@MutableBiDiMapImpl.vkMap[key]
        override fun isEmpty(): Boolean = this@MutableBiDiMapImpl.vkMap.isEmpty()

        override val otherDirection: MutableBiDiMap<K, V>
            get() = this@MutableBiDiMapImpl

        override fun clear() {
            this@MutableBiDiMapImpl.kvMap.clear()
            this@MutableBiDiMapImpl.vkMap.clear()
        }

        override fun remove(key: V): K? {
            val value = this@MutableBiDiMapImpl.vkMap.remove(key)
            this@MutableBiDiMapImpl.kvMap.remove(value)
            return value
        }

        override fun put(key: V, value: K): K? {
            val oldValue = this@MutableBiDiMapImpl.vkMap.put(key, value)
            this@MutableBiDiMapImpl.kvMap[value] = key
            return oldValue
        }

        override fun putAll(from: Map<out V, K>) {
            for ((k, v) in from) {
                this[k] = v
            }
        }
    }

}

class MutableBiDiEntry<K, V>(val entry: MutableMap.MutableEntry<K, V>, val vkMap: MutableMap<V, K>) : MutableMap.MutableEntry<K, V> {

    override val key: K
        get() = entry.key
    override val value: V
        get() = entry.value

    override fun setValue(newValue: V): V {
        this.vkMap[newValue] = this.key
        return entry.setValue(newValue)
    }

}

class MutableBiDiRevEntry<K, V>(val entry: MutableMap.MutableEntry<K, V>, val vkMap: MutableMap<V, K>) : MutableMap.MutableEntry<V, K> {

    override val key: V
        get() = entry.value
    override val value: K
        get() = entry.key

    override fun setValue(newValue: K): K {
        val set = this.vkMap.put(this.key, newValue)
        entry.setValue(this.key)
        return set!!
    }

}

class MutableBiDiSharedEntry<K, V>(
    val kvEntry: MutableMap.MutableEntry<K, V>,
    val vkEntry: MutableMap.MutableEntry<V, K>,
    val kvMap: MutableMap<K, V>,
    val vkMap: MutableMap<V, K>,
) : MutableMap.MutableEntry<K, V> {

    override val key: K
        get() = kvEntry.key
    override val value: V
        get() = vkEntry.key

    override fun setValue(newValue: V): V {
        this.vkMap.remove(this.value)
        val setted = this.kvEntry.setValue(newValue)
        this.vkMap[newValue] = this.key
        return setted
    }

}


fun <K, V> Map<K, V>.createVKMap(): Map<V, K> {
    val map = mutableMapOf<V, K>()
    this.entries.forEach {
        map[it.value] = it.key
    }
    return map
}

fun <K, V> MutableMap<K, V>.createVKMutableMap(): MutableMap<V, K> {
    val map = mutableMapOf<V, K>()
    this.entries.forEach {
        map[it.value] = it.key
    }
    return map
}

fun <K, V> mutableBiDiMap(vararg pairs: Pair<K, V>): MutableBiDiMap<K, V> =
    MutableBiDiMapImpl(
        mutableMapOf(*pairs),
        mutableMapOf(*pairs.inverted()),
    )

fun <K, V> biDiMap(vararg pairs: Pair<K, V>): BiDiMap<K, V> =
    BiDiMapImpl(
        mutableMapOf(*pairs),
        mutableMapOf(*pairs.inverted()),
    )

fun <K, V> Array<out Pair<K, V>>.inverted(): Array<Pair<V, K>> =
    Array(this.size) {
        this[it].second to this[it].first
    }