-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
botalka/src/main/kotlin/ru/itmo/lms/botalka/commons/BiMap.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ru.itmo.lms.botalka.commons | ||
|
||
import java.util.EnumMap | ||
|
||
interface BiMap<T, U> { | ||
fun toRight(value: T): U | ||
fun toLeft(value: U): T | ||
|
||
companion object { | ||
inline fun <reified T : Enum<T>, reified U : Enum<U>> from( | ||
vararg pairs: Pair<T, U>, | ||
): BiMap<T, U> = | ||
object : BiMap<T, U> { | ||
private val rhs = pairs | ||
.associateTo(EnumMap(T::class.java)) { (l, r) -> l to r } | ||
|
||
private val lhs = pairs | ||
.associateTo(EnumMap(U::class.java)) { (l, r) -> r to l } | ||
|
||
override fun toRight(value: T): U = rhs[value]!! | ||
|
||
override fun toLeft(value: U): T = lhs[value]!! | ||
} | ||
|
||
@JvmName("BiMapLeftToRight") | ||
operator fun <T, U> BiMap<T, U>.invoke(value: T): U = | ||
this.toRight(value) | ||
|
||
@JvmName("BiMapRightToLeft") | ||
operator fun <T, U> BiMap<T, U>.invoke(value: U): T = | ||
this.toLeft(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters