Skip to content

Latest commit

 

History

History

bimap

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

bimap

import "github.com/zyedidia/generic/bimap"

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/zyedidia/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

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.

type Bimap[K, V comparable] struct {
    // contains filtered or unexported fields
}

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

func (b *Bimap[K, V]) ContainsForward(key K) bool

ContainsForward checks if the given key exists.

func (*Bimap[K, V]) ContainsReverse

func (b *Bimap[K, V]) ContainsReverse(value V) bool

ContainsReverse checks if the given value exists.

func (*Bimap[K, V]) Copy

func (b *Bimap[K, V]) Copy() Bimap[K, V]

Copy creates a shallow copy of this bidirectional map.

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

func (b *Bimap[K, V]) GetForward(key K) (V, bool)

GetForward performs a lookup on the key to get the value.

func (*Bimap[K, V]) GetReverse

func (b *Bimap[K, V]) GetReverse(value V) (K, bool)

GetReverse performs a lookup on the value to get the key.

func (*Bimap[K, V]) Len

func (b *Bimap[K, V]) Len() int

Len returns the number of key-value pairs in this map.

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.

Generated by gomarkdoc