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

Merge branch 'main' into v2

This commit is contained in:
dudaodong
2022-07-20 17:48:26 +08:00
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) - [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) - [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) - [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. ### 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"} 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 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)) 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))
}