Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Map.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import TrieMap "TrieMap";
import Order "Order";
import Hash "Hash";
import Nat "Nat";
import Iter "Iter";

class Map<Key, Value>(
utilities : module {
compare : (Key, Key) -> Order.Order;
hash : Key -> Hash.Hash
}
) {
// Pulling out utility functions
let compare = utilities.compare;
let hash = utilities.hash;
func equal(key1 : Key, key2 : Key) : Bool {
switch (compare(key1, key2)) {
case (#equal) true;
case _ false
}
};

// Internal map implementation
var map = TrieMap.TrieMap<Key, Value>(equal, hash);

// Public API
public func size() : Nat {
map.size()
};

public func put(key : Key, value : Value) {
map.put(key, value)
};

public func get(key : Key) : ?Value {
map.get(key)
};

public func delete(key : Key) {
map.delete(key)
};

public func clear() {
map := TrieMap.TrieMap<Key, Value>(equal, hash)
};

public func share() : [(Key, Value)] {
Iter.toArray(map.entries())
};

public func unshare(entries : [(Key, Value)]) {
map := TrieMap.fromEntries<Key, Value>(entries.vals(), equal, hash)
}
}
4 changes: 4 additions & 0 deletions src/Nat.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Int "Int";
import Order "Order";
import Prim "mo:⛔";
import Hash "Hash";

module {

Expand Down Expand Up @@ -48,6 +49,9 @@ module {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

/// Returns the hash of `number`.
public func hash(number : Nat) : Hash.Hash { Hash.hash(number) };

/// Returns the sum of `x` and `y`, `x + y`.
public func add(x : Nat, y : Nat) : Nat { x + y };

Expand Down
58 changes: 58 additions & 0 deletions test/mapTest.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Map } "mo:base/Map";
import Nat "mo:base/Nat";
import Text "mo:base/Text";
import Order "mo:base/Order";
import Hash "mo:base/Hash";

import Suite "mo:matchers/Suite";
import T "mo:matchers/Testable";
import M "mo:matchers/Matchers";

let map1 = Map<Text, Nat>(Text);

module Mod {
public class C() {
public let x = 4
};
public func compare(c1 : C, c2 : C) : Order.Order {
Nat.compare(c1.x, c2.x)
};
public func hash(c : C) : Hash.Hash {
Hash.hash(c.x)
}
};

let map2 = Map<Mod.C, Nat>(Mod);

class D() {
public let x = 4
};

func compare2(d1 : D, d2 : D) : Order.Order {
Nat.compare(d1.x, d2.x)
};
func hash2(d : D) : Hash.Hash {
Hash.hash(d.x)
};

let map3 = Map<D, Nat>(
module {
public let compare : (D, D) -> Order.Order = compare2;
public let hash : D -> Hash.Hash = hash2
}
);
// map4 = Map<Mod.C, Nat>({ compare; hash })
// map5 = Map<Mod.C, Nat>(compare, hash);

let suite = Suite.suite(
"Map",
[
Suite.test(
"size empty",
map1.size(),
M.equals(T.nat(0))
)
]
);

Suite.run(suite)