Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aviate-labs/bimap.mo

Generic Bijective Maps in Motoko
https://github.com/aviate-labs/bimap.mo

Last synced: 3 months ago
JSON representation

Generic Bijective Maps in Motoko

Awesome Lists containing this project

README

        

# BiMap

A Motoko module for bijective maps.

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys.

## Usage

```motoko
let m = BiMap.New(
BiHashMap.empty(0, Nat.equal, Hash.hash),
BiHashMap.empty(0, Text.equal, Text.hash),
Text.equal,
);

m.insert(0, "a");

m.getByLeft(0);
// [(0, "a")];
m.getByRight("a");
// [(0, "a")];
```

Works with any `object` that implements the following interface:

```motoko
type Map = {
// Returns the number of entries in this map.
size() : Nat;
// Returns an iterator over the key value pairs in this map.
entries() : Iter.Iter<(L, R)>;
// Gets the entry with the key `k` and returns its associated value if it existed or `null` otherwise.
get(k : L) : ?R;
// Removes the entry with the key `k` and returns the associated value if it existed or `null` otherwise.
remove(k : L) : ?R;
// Insert the value `v` at key `k`. Overwrites an existing entry with key `k`.
put(k : L, v : R) : ();
};
```

## Predefined Empty Map Generators

- TrieMap
- HashMap