RBTree

The convention is to name the module alias after the file name it is defined in:

import RBTree "mo:base/RBTree";

On this page

Type RBTree.Color
Type RBTree.Tree<K, V>
Class RBTree.RBtree<K, V>

Class methods
    Method share
    Method unShare
    Method get
    Method replace
    Method put
    Method delete
    Method remove
    Method entries
    Method entriesRev

Module public functions
Function iter
Function size

Type RBTree.Color

import RBTree "mo:base/RBTree";

type Color = RBTree.Color;

type RedBlue = Color;

Type RBTree.Tree<K, V>

import RBTree "mo:base/RBTree";

type Tree<Text, Int> = RBTree.Tree<Text, Int>;

type MyTree = Tree<Text, Int>;

Class RBTree.RBtree<K,V>

class RBTree<K, V>(compare : (K, K) -> O.Order)

To construct a rbtree object, we use the RBTree class:

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);

Class methods

rbtree.share

func share() : Tree<K, V>

The function share takes no argument and returns an value of type Tree<K, V>.

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);

let share : RBTree.Tree<Text, Int> = textIntTree.share();
let treeSize : Nat = RBTree.size(share)

rbtree.unShare

func unShare(t : Tree<K, V>) : ()
Parameters
Variable argumentt : Tree<K, V>
Return type()

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);

let share : RBTree.Tree<Text, Int> = textIntTree.share();

let unshare : () = textIntTree.unshare(share);

let iter : Iter.Iter<(Text, Int)> = textIntTree.entries();

let array : [(Text, Int)] = Iter.toArray(iter)

rbtree.get

func get(key : K) : ?V
Parameters
Variable argumentkey : K
Return type?V

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);

let get : ?Int = textIntTree.get("bitcoin");

rbtree.replace

func replace(key : K, value : V) : ?V
Parameters
Variable argument1key : K
Variable argument2value : V
Return type?V

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);

let replace : ?Int = textIntTree.replace("bitcoin", 2);

rbtree.put

func put(key : K, value : V) : ()
Parameters
Variable argument1key : K
Variable argument2value : V
Return type()

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);
textIntTree.put("ICP", 3);

let iter : Iter.Iter<(Text, Int)> = textIntTree.entries();

let array : [(Text, Int)] = Iter.toArray(iter)

rbtree.delete

func delete(key : K) : ()
Parameters
Variable argumentkey : K
Return type()

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);
textIntTree.put("ICP", 3);

let delete : () = textIntTree.delete("bitcoin");

let iter : Iter.Iter<(Text, Int)> = textIntTree.entries();

let array : [(Text, Int)] = Iter.toArray(iter)

rbtree.remove

func remove(key : K) : ?V
Parameters
Variable argumentkey : K
Return type?V

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);
textIntTree.put("ICP", 3);

let remove : ?Int = textIntTree.remove("bitcoin");

let iter : Iter.Iter<(Text, Int)> = textIntTree.entries();

let array : [(Text, Int)] = Iter.toArray(iter)

rbtree.entries

func entries() : I.Iter<(K, V)>

The function entries takes no argument and returns an value of type I.Iter<(K, V)>.

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);
textIntTree.put("ICP", 3);

let entries : Iter.Iter<(Text, Int)> = textIntTree.entries();

let array : [(Text, Int)] = Iter.toArray(entries)

rbtree.entriesRev

func entriesRev() : I.Iter<(K, V)>
Parameters
Variable argument()
Return typeI.Iter<(K, V)>

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);
textIntTree.put("ICP", 3);

let entriesRev : Iter.Iter<(Text, Int)> = textIntTree.entriesRev();

let array : [(Text, Int)] = Iter.toArray(entriesRev)

Module public functions

RBTree.iter

func iter<X, Y>(

  tree : Tree<X, Y>,
  direction : {#fwd; #bwd}

) : I.Iter<(X, Y)>
Parameters
Generic parametersX, Y
Variable argument1tree : Tree<X< Y>
Variable argument2direction : {#fwd; #bwd}
Return typeI.Iter<(X, Y)>

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";
import Iter "mo:base/Iter";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);
textIntTree.put("ICP", 3);

let share : RBTree.Tree<Text, Int> = textIntTree.share();

let iter : Iter.Iter<(Text, Int)> = RBTree.iter<Text, Int>(share, #bwd);

let array : [(Text, Int)] = Iter.toArray(iter)

RBTree.size

func size<X, Y>(

  t : Tree<X, Y>

) : Nat
Parameters
Generic parametersX, Y
Variable argumentt : Tree<X< Y>
Return typeNat

Example

import RBTree "mo:base/RBTree";
import Text "mo:base/Text";

let textIntTree = RBTree.RBTree<Text, Int>(Text.compare);
textIntTree.put("bitcoin", 1);
textIntTree.put("ETH", 2);

let share : RBTree.Tree<Text, Int> = textIntTree.share();
let treeSize : Nat = RBTree.size(share)