1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

maputil: IsDisjoint (#43)

Verifies map a and map b have no keys in common.

if it returns true then the maps are disjoint otherwise a join in some form available
This commit is contained in:
donutloop
2022-07-20 11:26:54 +02:00
committed by GitHub
parent ecf325a06c
commit 336e454ce7
4 changed files with 83 additions and 0 deletions

View File

@@ -292,6 +292,7 @@ import "github.com/duke-git/lancet/v2/maputil"
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Merge)
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Minus)
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)
### 11. Mathutil package implements some functions for math calculation.

View File

@@ -301,4 +301,54 @@ func main() {
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
}
```
### <span id="Values">Values</span>
<p>Returns a boolean</p>
<b>Signature:</b>
```go
IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
m2 := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
m3 := map[int]string{
6: "a",
}
ok := maputil.IsDisjoint(m2, m1)
fmt.Println(ok) // false
ok = maputil.IsDisjoint(m2, m3)
fmt.Println(ok) // true
}
```

View File

@@ -108,3 +108,13 @@ func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
}
return result
}
// IsDisjoint two map are disjoint if they have no keys in common
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool {
for k := range mapA {
if _, ok := mapB[k]; ok {
return false
}
}
return true
}

View File

@@ -148,3 +148,25 @@ func TestMinus(t *testing.T) {
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
}
func TestIsDisjoint(t *testing.T) {
assert := internal.NewAssert(t, "TestMinus")
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"d": 22,
}
assert.Equal(true, IsDisjoint(m1, m2))
m3 := map[string]int{
"a": 22,
}
assert.Equal(false, IsDisjoint(m1, m3))
}