Documentation ¶
Overview ¶
Package bimap provides an implementation of a bi-directional map.
It is implemented by using two Go maps, which keeps the lookup speed identical for both forward and reverse lookups, however it also doubles the memory usage of the map.
Example ¶
package main import ( "fmt" "github.com/fufuok/utils/generic/bimap" ) func main() { var bimap bimap.Bimap[int, string] bimap.Add(1, "foo") bimap.Add(2, "bar") bimap.Add(3, "moo") bimap.Add(4, "doo") fmt.Println(bimap.GetForward(4)) fmt.Println(bimap.GetReverse("moo")) fmt.Println(bimap.GetReverse("unknown")) }
Output: doo true 3 true 0 false
Index ¶
- type Bimap
- func (b *Bimap[K, V]) Add(key K, value V)
- func (b *Bimap[K, V]) Clear()
- func (b *Bimap[K, V]) ContainsForward(key K) bool
- func (b *Bimap[K, V]) ContainsReverse(value V) bool
- func (b *Bimap[K, V]) Copy() Bimap[K, V]
- func (b *Bimap[K, V]) Each(f func(key K, value V))
- func (b *Bimap[K, V]) GetForward(key K) (V, bool)
- func (b *Bimap[K, V]) GetReverse(value V) (K, bool)
- func (b *Bimap[K, V]) Len() int
- func (b *Bimap[K, V]) RemoveForward(key K)
- func (b *Bimap[K, V]) RemoveReverse(value V)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bimap ¶
type Bimap[K, V comparable] struct { // contains filtered or unexported fields }
Bimap is a bi-directional map where both the keys and values are indexed against each other, allowing performant lookup on both keys and values, at the cost of double the memory usage.
func Of ¶
func Of[K, V comparable](m map[K]V) Bimap[K, V]
Of returns a new Bimap initiated with the keys and values from the given map.
func (*Bimap[K, V]) Add ¶
func (b *Bimap[K, V]) Add(key K, value V)
Add another key-value pair to be indexed inside this map. Both the key and the value is indexed, to allow performant lookups on both key and value.
On collisions, the old values will be overwritten.
func (*Bimap[K, V]) Clear ¶
func (b *Bimap[K, V]) Clear()
Clear empties this bidirectional map, removing all items.
func (*Bimap[K, V]) ContainsForward ¶
ContainsForward checks if the given key exists.
func (*Bimap[K, V]) ContainsReverse ¶
ContainsReverse checks if the given value exists.
func (*Bimap[K, V]) Each ¶
func (b *Bimap[K, V]) Each(f func(key K, value V))
Each loops over all the values in this map.
func (*Bimap[K, V]) GetForward ¶
GetForward performs a lookup on the key to get the value.
func (*Bimap[K, V]) GetReverse ¶
GetReverse performs a lookup on the value to get the key.
func (*Bimap[K, V]) RemoveForward ¶
func (b *Bimap[K, V]) RemoveForward(key K)
RemoveForward removes a key-value pair from this map based on the key.
func (*Bimap[K, V]) RemoveReverse ¶
func (b *Bimap[K, V]) RemoveReverse(value V)
RemoveReverse removes a key-value pair from this map based on the value.