Skip to content

Commit

Permalink
chore: move immut/sparse_array to internal lib (moonbitlang#562)
Browse files Browse the repository at this point in the history
* chore: move immut/sparse_array to internal lib

* fix: pass moon test --enable-coverage

* internal: move to internal/sparse_array/

* update

---------

Co-authored-by: Hongbo Zhang <[email protected]>
  • Loading branch information
Young-Flash and bobzhang authored Jun 17, 2024
1 parent afb3459 commit 4031acf
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 308 deletions.
12 changes: 6 additions & 6 deletions immut/hashmap/HAMT.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
//
// Hash-Array-Mapped-Trie (HAMT) is a persistent hash-table data structure.
// It is a trie over the hash of keys (i.e. strings of binary digits)
//
//
// Every level in a HAMT can have up to 32 branches (5 digits),
// so HAMT has a tree height of at most 7,
// and is more efficient compared to most other tree data structures.
//
//
// HAMT uses bitmap-based sparse array to avoid space waste
//
//
// Some references:
// - <https://handwiki.org/wiki/Hash%20array%20mapped%20trie>
// - <https://lampwww.epfl.ch/papers/idealhashtrees.pdf>
Expand All @@ -32,7 +32,7 @@ enum Map[K, V] {
Empty
Leaf(K, V) // optimize for the case of no collision
Collision(Bucket[K, V]) // use a list of buckets to resolve collision
Branch(SparseArray[Map[K, V]])
Branch(@sparse_array.SparseArray[Map[K, V]])
}

// The number of bits consumed at every [Branch] node
Expand All @@ -50,7 +50,7 @@ pub fn find[K : Eq + Hash, V](self : Map[K, V], key : K) -> V? {
Empty, _ => None
Leaf(key1, value), _ => if key == key1 { Some(value) } else { None }
Collision(bucket), _ => bucket.find(key)
// get the first segment (lower 5 bits) of the hash value
// get the first segment (lower 5 bits) of the hash value
// inline the hot path of Sparse_array::op_get
Branch(children), hash => {
let idx = hash.land(segment_mask)
Expand Down Expand Up @@ -86,7 +86,7 @@ fn add_with_hash[K : Eq, V](
hash.lsr(segment_length),
value,
)
Map::Branch(singleton(idx, child))
Map::Branch(@sparse_array.SparseArray::singleton(idx, child))
}
}

Expand Down
3 changes: 2 additions & 1 deletion immut/hashmap/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/array",
"moonbitlang/core/coverage"
"moonbitlang/core/coverage",
"moonbitlang/core/immut/internal/sparse_array"
],
"test-import" : [
"moonbitlang/core/assertion",
Expand Down
82 changes: 0 additions & 82 deletions immut/hashmap/sparse_array.mbt

This file was deleted.

12 changes: 6 additions & 6 deletions immut/hashset/HAMT.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
//
// Hash-Array-Mapped-Trie (HAMT) is a persistent hash-table data structure.
// It is a trie over the hash of keys (i.e. strings of binary digits)
//
//
// Every level in a HAMT can have up to 32 branches (5 digits),
// so HAMT has a tree height of at most 7,
// and is more efficient compared to most other tree data structures.
//
//
// HAMT uses bitmap-based sparse array to avoid space waste
//
//
// Some references:
// - <https://handwiki.org/wiki/Hash%20array%20mapped%20trie>
// - <https://lampwww.epfl.ch/papers/idealhashtrees.pdf>
Expand All @@ -32,7 +32,7 @@ enum Set[T] {
Empty
Leaf(T) // optimize for the case of no collision
Collision(Bucket[T]) // use a list of buckets to resolve collision
Branch(SparseArray[Set[T]])
Branch(@sparse_array.SparseArray[Set[T]])
}

// The number of bits consumed at every [Branch] node
Expand All @@ -50,7 +50,7 @@ pub fn contain[T : Eq + Hash](self : Set[T], key : T) -> Bool {
Empty, _ => false
Leaf(key1), _ => key == key1
Collision(bucket), _ => bucket.find(key)
// get the first segment (lower 5 bits) of the hash value
// get the first segment (lower 5 bits) of the hash value
// inline the hot path of Sparse_array::op_get
Branch(children), hash => {
let idx = hash.land(segment_mask)
Expand Down Expand Up @@ -80,7 +80,7 @@ fn add_with_hash[T : Eq](
key,
hash.lsr(segment_length),
)
Set::Branch(singleton(idx, child))
Set::Branch(@sparse_array.SparseArray::singleton(idx, child))
}
}

Expand Down
53 changes: 0 additions & 53 deletions immut/hashset/bitset.mbt

This file was deleted.

68 changes: 0 additions & 68 deletions immut/hashset/bitset_test.mbt

This file was deleted.

3 changes: 2 additions & 1 deletion immut/hashset/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/array",
"moonbitlang/core/coverage"
"moonbitlang/core/coverage",
"moonbitlang/core/immut/internal/sparse_array"
],
"test-import" : [
"moonbitlang/core/assertion",
Expand Down
53 changes: 0 additions & 53 deletions immut/hashset/sparse_array_test.mbt

This file was deleted.

Loading

0 comments on commit 4031acf

Please sign in to comment.