From 229ecc59b044b26517181831cac04ecd90c843b4 Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Mon, 21 Sep 2020 17:54:46 -0700 Subject: [PATCH 1/4] add share/unshare for class objects --- src/HashMap.mo | 11 +++++++++++ src/RBTree.mo | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/HashMap.mo b/src/HashMap.mo index a0d978e9..ef59a7a0 100644 --- a/src/HashMap.mo +++ b/src/HashMap.mo @@ -35,6 +35,17 @@ public class HashMap ( var table : [var KVs] = [var]; var _count : Nat = 0; + /// Get purely-functional representation + public func share() : ([KVs], Nat) { + (A.freeze table, _count) + }; + + /// Put purely-functional representation into class. Need to make sure the tree is constructed with the same Eq and Hash functions + public func unsafeUnshare(t : [KVs], count : Nat) { + table := A.thaw(t); + _count := count; + }; + /// Returns the number of entries in this HashMap. public func size() : Nat = _count; diff --git a/src/RBTree.mo b/src/RBTree.mo index 84770b65..18d253d2 100644 --- a/src/RBTree.mo +++ b/src/RBTree.mo @@ -25,12 +25,17 @@ public class RBTree(compareTo:(X, X) -> O.Order) { /// Tree as share data. /// /// Get non-OO, purely-functional representation: - /// for drawing, pretty-printing and non-OO contexts + /// for drawing, pretty-printing, upgrades and non-OO contexts /// (e.g., async args and results): public func share() : Tree { tree }; + /// Put purely-functional representation into class. Need to make sure the tree is constructed with the same compare function + public func unsafeUnshare(t: Tree) { + tree := t; + }; + /// Get the value associated with a given key. public func get(x:X) : ?Y = getRec(x, compareTo, tree); From d1cd28ed6fb0fc1cd4529f06e0f29f6dd47672c3 Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Mon, 21 Sep 2020 18:28:18 -0700 Subject: [PATCH 2/4] add Buffer --- src/Buffer.mo | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Buffer.mo b/src/Buffer.mo index 4608fa59..d8ca594b 100644 --- a/src/Buffer.mo +++ b/src/Buffer.mo @@ -20,6 +20,7 @@ /// A "buffer" is a mutable sequence that grows, either one element at a /// time, or one (second) buffer at time. import Prim "mo:prim"; +import A "mo:base/Array"; module { @@ -32,6 +33,17 @@ public class Buffer (initCapacity : Nat) { var count : Nat = 0; var elems : [var X] = [var]; // initially empty; allocated upon first `add` + /// Get purely-functional representation + public func share() : ([X], Nat) { + (A.freeze elems, count) + }; + + /// Put purely-functional representation into class. Need to make sure the count and elems are consistent + public func unsafeUnshare(t : [X], c : Nat) { + elems := A.thaw(t); + count := c; + }; + /// Adds a single element to the buffer. public func add(elem : X) { if (count == elems.size()) { From 6d2569c0e6aa45c9c6a76b00a17302865a9fc1cc Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Mon, 21 Sep 2020 18:37:26 -0700 Subject: [PATCH 3/4] fix --- src/Buffer.mo | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Buffer.mo b/src/Buffer.mo index d8ca594b..a6f43b1a 100644 --- a/src/Buffer.mo +++ b/src/Buffer.mo @@ -20,7 +20,6 @@ /// A "buffer" is a mutable sequence that grows, either one element at a /// time, or one (second) buffer at time. import Prim "mo:prim"; -import A "mo:base/Array"; module { @@ -34,14 +33,20 @@ public class Buffer (initCapacity : Nat) { var elems : [var X] = [var]; // initially empty; allocated upon first `add` /// Get purely-functional representation - public func share() : ([X], Nat) { - (A.freeze elems, count) + public func share() : [X] { + Prim.Array_tabulate(count, func i = elems[i]) }; - /// Put purely-functional representation into class. Need to make sure the count and elems are consistent - public func unsafeUnshare(t : [X], c : Nat) { - elems := A.thaw(t); - count := c; + /// Put purely-functional representation into class. + public func unshare(xs : [X]) { + count := xs.size(); + if (count == 0) { + elems := [var]; + }; + let elems := Prim.Array_init(count, xs[0]); + for (i in elems.keys()) { + elems[i] := xs[i]; + }; }; /// Adds a single element to the buffer. From 13f62c89e7f368a655f5e7b7555c9683e05a3312 Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Mon, 21 Sep 2020 18:39:19 -0700 Subject: [PATCH 4/4] fix --- src/Buffer.mo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Buffer.mo b/src/Buffer.mo index a6f43b1a..ad341a2c 100644 --- a/src/Buffer.mo +++ b/src/Buffer.mo @@ -43,7 +43,7 @@ public class Buffer (initCapacity : Nat) { if (count == 0) { elems := [var]; }; - let elems := Prim.Array_init(count, xs[0]); + elems := Prim.Array_init(count, xs[0]); for (i in elems.keys()) { elems[i] := xs[i]; };