-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adding All, Any, None, sets.FlipMap, maps.FlipSlice
- Loading branch information
Showing
7 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package set | ||
|
||
// FlipMap will return a new map with the keys and values of the input map | ||
// flipped. That is, the keys of the input map will be the values of the output | ||
// map and the values of the input map will be the keys of the output map. | ||
// However, the resulting value is a Set of keys. | ||
// | ||
// This will only work if the value type is comparable. If values repeat, the | ||
// keys will be collected into the value set. The ordering is indeterminate. | ||
// | ||
// You might also be interested in maps.Flip and maps.FlipSlice. | ||
func FlipMap[K comparable, V comparable](in map[K]V) map[V]Set[K] { | ||
out := make(map[V]Set[K], len(in)) | ||
for k, v := range in { | ||
outv, alreadyExists := out[v] | ||
if !alreadyExists { | ||
outv = New[K]() | ||
} | ||
outv.Insert(k) | ||
out[v] = outv | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package set_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/zostay/go-std/set" | ||
) | ||
|
||
func TestFlipMap(t *testing.T) { | ||
t.Parallel() | ||
|
||
a := map[string]int{ | ||
"a": 1, | ||
"b": 2, | ||
"c": 3, | ||
"d": 1, | ||
"e": 1, | ||
} | ||
|
||
vks := set.FlipMap(a) | ||
assert.Len(t, vks, 3) | ||
for k, v := range a { | ||
assert.Contains(t, vks, v) | ||
assert.True(t, vks[v].Contains(k)) | ||
} | ||
|
||
for k, vs := range vks { | ||
for _, v := range vs.Keys() { | ||
assert.Contains(t, a, v) | ||
assert.Equal(t, k, a[v]) | ||
} | ||
} | ||
} |