Skip to content

Commit 1f5c86e

Browse files
AsterlessLampese
andcommitted
feat: add set operations to @immut/hash{map, set} and @internal/sparse_array (moonbitlang#2145)
* feat: add four new functions to HAMT and their tests * fix(union_with): fix union_with for HAMT,now it can handle branch * feat(sparse_array): add intersection and difference methods * fix(HAMT): fix union_with, intersection, intersection_with and difference methods, now they can handle branchs * feat(hashset): add intersection and difference methods to hashset * commit other files * feat: add four new functions to HAMT and their tests * fix(union_with): fix union_with for HAMT,now it can handle branch * feat(sparse_array): add intersection and difference methods * fix(HAMT): fix union_with, intersection, intersection_with and difference methods, now they can handle branchs * feat(hashset): add intersection and difference methods to hashset * style: change the position of some function declarations * fix: fix formatting of the code * feat: Update the function declarations of hash tables and sparse arrays * refactor:update mbti * refactor: update hashmap and hashset function signatures to include type prefix --------- Co-authored-by: 东灯 <[email protected]>
1 parent 9446eef commit 1f5c86e

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

immut/hashmap/HAMT.mbt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ pub fn[K, V] size(self : T[K, V]) -> Int {
311311

312312
///|
313313
/// Union two hashmaps
314-
pub fn[K : Eq + Hash, V] union(self : T[K, V], other : T[K, V]) -> T[K, V] {
314+
pub fn[K : Eq + Hash, V] T::union(self : T[K, V], other : T[K, V]) -> T[K, V] {
315315
match (self, other) {
316316
(_, Empty) => self
317317
(Empty, _) => other
@@ -331,7 +331,7 @@ pub fn[K : Eq + Hash, V] union(self : T[K, V], other : T[K, V]) -> T[K, V] {
331331

332332
///|
333333
/// Union two hashmaps with a function
334-
pub fn[K : Eq + Hash, V] union_with(
334+
pub fn[K : Eq + Hash, V] T::union_with(
335335
self : T[K, V],
336336
other : T[K, V],
337337
f : (K, V, V) -> V
@@ -365,7 +365,7 @@ pub fn[K : Eq + Hash, V] union_with(
365365

366366
///|
367367
/// Intersect two hashmaps
368-
pub fn[K : Eq + Hash, V] intersection(
368+
pub fn[K : Eq + Hash, V] T::intersection(
369369
self : T[K, V],
370370
other : T[K, V]
371371
) -> T[K, V] {
@@ -399,7 +399,7 @@ pub fn[K : Eq + Hash, V] intersection(
399399

400400
///|
401401
/// Intersection two hashmaps with a function
402-
pub fn[K : Eq + Hash, V] intersection_with(
402+
pub fn[K : Eq + Hash, V] T::intersection_with(
403403
self : T[K, V],
404404
other : T[K, V],
405405
f : (K, V, V) -> V
@@ -433,7 +433,10 @@ pub fn[K : Eq + Hash, V] intersection_with(
433433

434434
///|
435435
/// Difference of two hashmaps: elements in `self` but not in `other`
436-
pub fn[K : Eq + Hash, V] difference(self : T[K, V], other : T[K, V]) -> T[K, V] {
436+
pub fn[K : Eq + Hash, V] T::difference(
437+
self : T[K, V],
438+
other : T[K, V]
439+
) -> T[K, V] {
437440
match (self, other) {
438441
(Empty, _) => Empty
439442
(_, Empty) => self

immut/hashmap/hashmap.mbti

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ fn[K, V] size(T[K, V]) -> Int
6060

6161
fn[K, V] to_array(T[K, V]) -> Array[(K, V)]
6262

63-
fn[K : Eq + Hash, V] union(T[K, V], T[K, V]) -> T[K, V]
64-
6563
fn[K, V] values(T[K, V]) -> Iter[V]
6664

6765
// Types and methods
6866
type T[K, V]
6967
fn[K : Eq + Hash, V] T::add(Self[K, V], K, V) -> Self[K, V]
7068
fn[K : Eq + Hash, V] T::contains(Self[K, V], K) -> Bool
69+
fn[K : Eq + Hash, V] T::difference(Self[K, V], Self[K, V]) -> Self[K, V]
7170
fn[K, V] T::each(Self[K, V], (K, V) -> Unit) -> Unit
7271
#deprecated
7372
fn[K, V] T::elems(Self[K, V]) -> Iter[V]
@@ -77,6 +76,8 @@ fn[K : Eq + Hash, V] T::find(Self[K, V], K) -> V?
7776
fn[K, V, A] T::fold(Self[K, V], init~ : A, (A, V) -> A) -> A
7877
fn[K, V, A] T::fold_with_key(Self[K, V], init~ : A, (A, K, V) -> A) -> A
7978
fn[K : Eq + Hash, V] T::get(Self[K, V], K) -> V?
79+
fn[K : Eq + Hash, V] T::intersection(Self[K, V], Self[K, V]) -> Self[K, V]
80+
fn[K : Eq + Hash, V] T::intersection_with(Self[K, V], Self[K, V], (K, V, V) -> V) -> Self[K, V]
8081
fn[K, V] T::iter(Self[K, V]) -> Iter[(K, V)]
8182
fn[K, V] T::iter2(Self[K, V]) -> Iter2[K, V]
8283
fn[K, V] T::keys(Self[K, V]) -> Iter[K]
@@ -88,6 +89,7 @@ fn[K : Eq + Hash, V] T::remove(Self[K, V], K) -> Self[K, V]
8889
fn[K, V] T::size(Self[K, V]) -> Int
8990
fn[K, V] T::to_array(Self[K, V]) -> Array[(K, V)]
9091
fn[K : Eq + Hash, V] T::union(Self[K, V], Self[K, V]) -> Self[K, V]
92+
fn[K : Eq + Hash, V] T::union_with(Self[K, V], Self[K, V], (K, V, V) -> V) -> Self[K, V]
9193
fn[K, V] T::values(Self[K, V]) -> Iter[V]
9294
impl[K : Eq + Hash, V : Eq] Eq for T[K, V]
9395
impl[K : Hash, V : Hash] Hash for T[K, V]

immut/hashset/HAMT.mbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn[A] size(self : T[A]) -> Int {
180180

181181
///|
182182
/// Union two hashsets
183-
pub fn[K : Eq + Hash] union(self : T[K], other : T[K]) -> T[K] {
183+
pub fn[K : Eq + Hash] T::union(self : T[K], other : T[K]) -> T[K] {
184184
match (self, other) {
185185
(_, Empty) => self
186186
(Empty, _) => other
@@ -194,7 +194,7 @@ pub fn[K : Eq + Hash] union(self : T[K], other : T[K]) -> T[K] {
194194

195195
///|
196196
/// Intersect two hashsets
197-
pub fn[K : Eq + Hash] intersection(self : T[K], other : T[K]) -> T[K] {
197+
pub fn[K : Eq + Hash] T::intersection(self : T[K], other : T[K]) -> T[K] {
198198
match (self, other) {
199199
(_, Empty) => Empty
200200
(Empty, _) => Empty
@@ -223,7 +223,7 @@ pub fn[K : Eq + Hash] intersection(self : T[K], other : T[K]) -> T[K] {
223223

224224
///|
225225
/// Difference of two hashsets: elements in `self` but not in `other`
226-
pub fn[K : Eq + Hash] difference(self : T[K], other : T[K]) -> T[K] {
226+
pub fn[K : Eq + Hash] T::difference(self : T[K], other : T[K]) -> T[K] {
227227
match (self, other) {
228228
(Empty, _) => Empty
229229
(_, Empty) => self

immut/hashset/hashset.mbti

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ fn[A : Eq + Hash] remove(T[A], A) -> T[A]
3131

3232
fn[A] size(T[A]) -> Int
3333

34-
fn[K : Eq + Hash] union(T[K], T[K]) -> T[K]
35-
3634
// Types and methods
3735
type T[A]
3836
fn[A : Eq + Hash] T::add(Self[A], A) -> Self[A]
3937
fn[A : Eq + Hash] T::contains(Self[A], A) -> Bool
38+
fn[K : Eq + Hash] T::difference(Self[K], Self[K]) -> Self[K]
4039
fn[A] T::each(Self[A], (A) -> Unit) -> Unit
40+
fn[K : Eq + Hash] T::intersection(Self[K], Self[K]) -> Self[K]
4141
fn[A] T::is_empty(Self[A]) -> Bool
4242
fn[A] T::iter(Self[A]) -> Iter[A]
4343
fn[A : Eq + Hash] T::remove(Self[A], A) -> Self[A]

immut/internal/sparse_array/sparse_array.mbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn[X] add(self : SparseArray[X], idx : Int, value : X) -> SparseArray[X] {
7070
/// `union(self: SparseArray[X], other: SparseArray[X]) -> SparseArray[X]`
7171
///
7272
/// Combine two sparse arrays into one.
73-
pub fn[X] union(
73+
pub fn[X] SparseArray::union(
7474
self : SparseArray[X],
7575
other : SparseArray[X],
7676
union : (X, X) -> X
@@ -106,7 +106,7 @@ pub fn[X] union(
106106
/// `intersection(self: SparseArray[X], other: SparseArray[X], f: (X, X) -> X) -> SparseArray[X]`
107107
///
108108
/// Only keep indices present in both sparse arrays, and merge values with f.
109-
pub fn[X] intersection(
109+
pub fn[X] SparseArray::intersection(
110110
self : SparseArray[X],
111111
other : SparseArray[X],
112112
f : (X, X) -> X
@@ -132,7 +132,7 @@ pub fn[X] intersection(
132132
/// `difference(self: SparseArray[X], other: SparseArray[X]) -> SparseArray[X]`
133133
///
134134
/// Keep indices and values only present in self but not in other.
135-
pub fn[X] difference(
135+
pub fn[X] SparseArray::difference(
136136
self : SparseArray[X],
137137
other : SparseArray[X]
138138
) -> SparseArray[X] {

immut/internal/sparse_array/sparse_array.mbti

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ fn[X] singleton(Int, X) -> SparseArray[X]
2121

2222
fn[X] size(SparseArray[X]) -> Int
2323

24-
fn[X] union(SparseArray[X], SparseArray[X], (X, X) -> X) -> SparseArray[X]
25-
2624
// Types and methods
2725
pub(all) type Bitset UInt
2826
fn Bitset::add(Self, Int) -> Self
@@ -41,8 +39,10 @@ pub(all) struct SparseArray[X] {
4139
data : FixedArray[X]
4240
}
4341
fn[X] SparseArray::add(Self[X], Int, X) -> Self[X]
42+
fn[X] SparseArray::difference(Self[X], Self[X]) -> Self[X]
4443
fn[X] SparseArray::each(Self[X], (X) -> Unit) -> Unit
4544
fn[X] SparseArray::has(Self[X], Int) -> Bool
45+
fn[X] SparseArray::intersection(Self[X], Self[X], (X, X) -> X) -> Self[X]
4646
fn[X] SparseArray::op_get(Self[X], Int) -> X?
4747
fn[X] SparseArray::replace(Self[X], Int, X) -> Self[X]
4848
fn[X] SparseArray::size(Self[X]) -> Int

0 commit comments

Comments
 (0)