gleamy/bimap

This module provides a simple implementation of a bidirectional map. Each key can have a single value, and each value can have a single key. The bimap is based on Gleam dict.Dicts, so ordering is not guaranteed.

Types

The Bimap(a, b) type represents a bi-directional map that associates values of type a with values of type b and vice versa.

pub opaque type Bimap(a, b)

Values

pub fn count(bimap: Bimap(a, b)) -> Int

Get the count of key-value pairs in the bimap.

pub fn delete_by_key(bimap: Bimap(a, b), key: a) -> Bimap(a, b)

Delete a key-value pair from the bimap by key.

pub fn delete_by_value(
  bimap: Bimap(a, b),
  value: b,
) -> Bimap(a, b)

Delete a key-value pair from the bimap by value.

pub fn from_list(members: List(#(a, b))) -> Bimap(a, b)

Create a bimap from list entries.

pub fn get_by_key(bimap: Bimap(a, b), key: a) -> Result(b, Nil)

Get a value by its key, if present.

pub fn get_by_value(
  bimap: Bimap(a, b),
  value: b,
) -> Result(a, Nil)

Get a key by its value, if present.

pub fn has_key(bimap: Bimap(a, b), key: a) -> Bool

Check if a key exists in the bimap.

pub fn has_value(bimap: Bimap(a, b), value: b) -> Bool

Check if a value exists in the bimap.

pub fn insert(
  bimap: Bimap(a, b),
  key: a,
  value: b,
) -> Bimap(a, b)

Insert a new key-value pair into the bimap. If either the key or value already exists, the existing pair is removed before inserting the new one.

pub fn new() -> Bimap(a, b)

Creates a new empty bimap.

pub fn to_list(bimap: Bimap(a, b)) -> List(#(a, b))

Converts a bimap into a list of key-value pairs.

Search Document