Skip to content

Commit

Permalink
[Feat]: Add fromIter() and fromArray() init functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijaga committed Aug 24, 2024
1 parent 880a28f commit 5ae740e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mops.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ base = "0.12.1"

[package]
name = "bit-map"
version = "0.0.1"
version = "0.1.0"
description = "A data structure for fast set operations on a set of integers each represented by a bit."
repository = "https://github.com/NatLabs/BitMap"
keywords = ["set"]
Expand Down
20 changes: 14 additions & 6 deletions src/lib.mo
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import Array "mo:base/Array";
import Buffer "mo:base/Buffer";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Nat16 "mo:base/Nat16";
import Nat32 "mo:base/Nat32";
import Nat64 "mo:base/Nat64";
import Iter "mo:base/Iter";
import Text "mo:base/Text";
import Debug "mo:base/Debug";

module {

let BYTE = 8;
let WORD_SIZE : Nat = 64;
public let WORD_SIZE : Nat = 64;

func div_ceil(n : Nat, d : Nat) : Nat {
(n + d - 1) / d;
};

public func fromIter(iter : Iter.Iter<Nat>) : BitMap {
let bitmap = BitMap(8);
for (n in iter) {
bitmap.set(n, true);
};

bitmap;
};

public func fromArray(arr : [Nat]) : BitMap {
fromIter(arr.vals());
};

/// A data structure for fast set operations on a set of integers each represented by a bit.
public class BitMap(init_size : Nat) {

Expand Down
12 changes: 12 additions & 0 deletions tests/BitMap.Test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ test(
assert bitmap.toArray() == [1, 2, 64, 324];
},
);

test(
"fromArray()",
func() {
let bitmap = BitMap.fromArray([1, 2, 64, 324]);

assert bitmap.get(1) == true;
assert bitmap.get(2) == true;
assert bitmap.get(64) == true;
assert bitmap.get(324) == true;
},
);

0 comments on commit 5ae740e

Please sign in to comment.