bimap

package module
v0.0.0-...-a9fb9b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 1 Imported by: 21 modules, 19 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

bimap

GoDoc

A generic bidirectional map written in Go

Installation

go get github.com/vishalkuo/bimap

Usage

import "github.com/vishalkuo/bimap"

biMap := bimap.NewBiMap[string, string]()
biMap.Insert("key", "value")
val, ok := biMap.GetInverse("value") // val should be "key", ok should be true
biMap.Delete("key")
biMap.Size() // == 0

biMap2 := bimap.NewBiMap[string, int]()
biMap2.Insert("key", 1) // Notice different types
val, ok = biMap2.Get("key") // Returns 1
val, ok = biMap2.GetInverse(1) // Returns "key"

Documentation

Overview

Package bimap provides a threadsafe bidirectional map

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BiMap

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

BiMap is a bi-directional hashmap that is thread safe and supports immutability

func NewBiMap

func NewBiMap[K comparable, V comparable]() *BiMap[K, V]

NewBiMap returns a an empty, mutable, biMap

func NewBiMapFromMap

func NewBiMapFromMap[K comparable, V comparable](forwardMap map[K]V) *BiMap[K, V]

NewBiMapFrom returns a new BiMap from a map[K, V]

func (*BiMap[K, V]) Delete

func (b *BiMap[K, V]) Delete(k K)

Delete removes a key-value pair from the BiMap for a given key. Returns if the key doesn't exist

func (*BiMap[K, V]) DeleteInverse

func (b *BiMap[K, V]) DeleteInverse(v V)

DeleteInverse emoves a key-value pair from the BiMap for a given value. Returns if the value doesn't exist

func (*BiMap[K, V]) Exists

func (b *BiMap[K, V]) Exists(k K) bool

Exists checks whether or not a key exists in the BiMap

func (*BiMap[K, V]) ExistsInverse

func (b *BiMap[K, V]) ExistsInverse(k V) bool

ExistsInverse checks whether or not a value exists in the BiMap

func (*BiMap[K, V]) Get

func (b *BiMap[K, V]) Get(k K) (V, bool)

Get returns the value for a given key in the BiMap and whether or not the element was present.

func (*BiMap[K, V]) GetForwardMap

func (b *BiMap[K, V]) GetForwardMap() map[K]V

GetForwardMap returns a regular go map mapping from the BiMap's keys to its values

func (*BiMap[K, V]) GetInverse

func (b *BiMap[K, V]) GetInverse(v V) (K, bool)

GetInverse returns the key for a given value in the BiMap and whether or not the element was present.

func (*BiMap[K, V]) GetInverseMap

func (b *BiMap[K, V]) GetInverseMap() map[V]K

GetInverseMap returns a regular go map mapping from the BiMap's values to its keys

func (*BiMap[K, V]) Insert

func (b *BiMap[K, V]) Insert(k K, v V)

Insert puts a key and value into the BiMap, provided its mutable. Also creates the reverse mapping from value to key.

func (*BiMap[K, V]) Lock

func (b *BiMap[K, V]) Lock()

Lock manually locks the BiMap's mutex

func (*BiMap[K, V]) MakeImmutable

func (b *BiMap[K, V]) MakeImmutable()

MakeImmutable freezes the BiMap preventing any further write actions from taking place

func (*BiMap[K, V]) Size

func (b *BiMap[K, V]) Size() int

Size returns the number of elements in the bimap

func (*BiMap[K, V]) Unlock

func (b *BiMap[K, V]) Unlock()

Unlock manually unlocks the BiMap's mutex

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL