Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add share/unshare for class objects #180

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add Buffer
chenyan-dfinity committed Sep 22, 2020
commit d1cd28ed6fb0fc1cd4529f06e0f29f6dd47672c3
12 changes: 12 additions & 0 deletions src/Buffer.mo
Original file line number Diff line number Diff line change
@@ -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<X> (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()) {