BidirectionalMap

open class BidirectionalMap<T : Any, R : Any>(elements: Pair<T, R>)

A bidirectional map. Each key-value-pair gets mapped in both directions. Keys and values must be unique in the sense that there must not be a duplicate key in the domain, nor a duplicate value in the coDomain, but the same element may appear once as key and once as value.

Example:

A -> B

A -> C

is invalid because A is contained twice in the Domain.

A -> B

C -> B

is invalid because B is contained twice in the coDomain.

A -> B

C -> A

is valid because A is only contained once in the domain and in the coDomain.

Parameters

T

Type of domain elements.

R

Type of co-domain elements.

elements

Elements to be initialized in the map.

Constructors

Link copied to clipboard
fun <T : Any, R : Any> BidirectionalMap(vararg elements: Pair<T, R>)

Creates a map with the given set of elements mapping pair.first -> pair.second.

Functions

Link copied to clipboard
fun add(element: Pair<T, R>): Boolean
fun add(entity: T, value: R): Boolean

Adds a relation A -> B if domain does not contain A and coDomain does not contain B. Returns false if the relation could not be added.

Link copied to clipboard
fun addAll(vararg items: Pair<T, R>): Boolean

Adds all relations A -> B. If any of the given items already exist, it gets ignored. If any item contains a key or value that already exists, the map remains unchanged.

Link copied to clipboard
fun backward(value: R): T

Backward lookup for entry.

Link copied to clipboard
fun backwardOrNull(value: R): T?

Backward lookup for entry.

Link copied to clipboard
fun clear()

Clears the map.

Link copied to clipboard
fun contains(pair: Pair<T, R>): Boolean
fun contains(entity: T, value: R): Boolean

Returns whether relation A -> B exists in this map.

Link copied to clipboard
fun containsBackward(value: R): Boolean

Returns whether a relation * -> B exists.

Link copied to clipboard
fun containsForward(entity: T): Boolean

Returns whether a relation A -> * exists.

Link copied to clipboard
fun forward(entity: T): R

Forward lookup for entry.

Link copied to clipboard
fun forwardOrNull(entity: T): R?

Forward lookup for entry.

Link copied to clipboard
operator fun get(it: T): Any

Get the value for a given domain key.

Link copied to clipboard
fun getCoDomain(): Set<R>

Returns the coDomain of this map as a set.

Link copied to clipboard
fun getDomain(): Set<T>

Returns the domain of this map as a set.

Link copied to clipboard
fun isEmpty(): Boolean

Returns whether this map contains no elements.

Link copied to clipboard
fun isNotEmpty(): Boolean

Returns whether this map contains elements.

Link copied to clipboard
fun put(key: T, value: R): Pair<Pair<T, R>?, Pair<T, R>?>

Set the given co-domain value for a given domain key. Overwrites existing values in domain and co-domain.

Link copied to clipboard
fun putAll(vararg items: Pair<T, R>)
fun putAll(bidirectionalMap: BidirectionalMap<T, R>)

Set the given entries. Overwrites existing values in domain and co-domain.

Link copied to clipboard
fun remove(element: Pair<T, R>): Boolean
fun remove(entity: T, value: R): Boolean

Removes relation A -> B if it exists.

Link copied to clipboard
fun removeBackward(value: R): Boolean

Removes by backward lookup. Removes relation * -> B.

Link copied to clipboard
fun removeForward(entity: T): Boolean

Removes by forward lookup. Removes relation A -> * if it exists.

Link copied to clipboard
operator fun set(it: T, value: R)

Set the given co-domain value for a given domain key. Overwrites existing values in domain and co-domain.

Properties

Link copied to clipboard
val entries: Set<Pair<T, R>>

Represents the entries of this map as a set.

Link copied to clipboard
val keysBackward: Set<R>

Represents the co-domain keys of this map as a set.

Link copied to clipboard
val keysForward: Set<T>

Represents the domain keys of this map as a set.

Link copied to clipboard
val size: Int

The size of this map.