-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from upfluence/sp/add-sets
pkg/maps: add generics sets implementations
- Loading branch information
Showing
6 changed files
with
193 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package maps | ||
|
||
import "golang.org/x/exp/maps" | ||
|
||
// Keys is a dropin replacement for x/maps.Keys | ||
func Keys[M ~map[K]T, K comparable, T any](m M) []K { | ||
return maps.Keys(m) | ||
} | ||
|
||
// Values is a dropin replacement for x/maps.Values | ||
func Values[M ~map[K]T, K comparable, T any](m M) []T { | ||
return maps.Values(m) | ||
} |
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,25 @@ | ||
package maps | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeys(t *testing.T) { | ||
var m = map[string]int{ | ||
"foo": 2, | ||
"fizz": 1, | ||
} | ||
|
||
assert.ElementsMatch(t, []string{"foo", "fizz"}, Keys(m)) | ||
} | ||
|
||
func TestValues(t *testing.T) { | ||
var m = map[string]int{ | ||
"foo": 2, | ||
"fizz": 1, | ||
} | ||
|
||
assert.ElementsMatch(t, []int{1, 2}, Values(m)) | ||
} |
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,66 @@ | ||
package maps | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// Set is a concurrency safe wrapper around a map to ensure uniqueness through | ||
// a collection. | ||
// While the underlying map can be manually manipulated, read and | ||
// mutation should be done with through methods to ensure | ||
// concurrency safety. | ||
type Set[T comparable] struct { | ||
mu sync.RWMutex | ||
|
||
Set map[T]struct{} | ||
} | ||
|
||
func (s *Set[T]) Add(vs ...T) { | ||
if len(vs) == 0 { | ||
return | ||
} | ||
|
||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if s.Set == nil { | ||
s.Set = make(map[T]struct{}, len(vs)) | ||
} | ||
|
||
for _, v := range vs { | ||
s.Set[v] = struct{}{} | ||
} | ||
} | ||
|
||
func (s *Set[T]) Has(v T) bool { | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
|
||
if len(s.Set) == 0 { | ||
return false | ||
} | ||
|
||
_, ok := s.Set[v] | ||
|
||
return ok | ||
} | ||
|
||
func (s *Set[T]) Keys() []T { | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
|
||
if len(s.Set) == 0 { | ||
return nil | ||
} | ||
|
||
return Keys(s.Set) | ||
} | ||
|
||
func (s *Set[T]) Delete(vs ...T) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
for _, v := range vs { | ||
delete(s.Set, v) | ||
} | ||
} |
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,66 @@ | ||
package maps | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSet_Add(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
in [][]string | ||
want []string | ||
}{ | ||
{ | ||
name: "single slice with duplicate", | ||
in: [][]string{{"foo", "bar", "foo"}}, | ||
want: []string{"foo", "bar"}, | ||
}, | ||
{ | ||
name: "multiple slices with duplicate", | ||
in: [][]string{{"foo"}, {"bar"}, {"foo"}}, | ||
want: []string{"foo", "bar"}, | ||
}, | ||
{name: "empty slices", in: [][]string{{}}}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var s Set[string] | ||
|
||
for _, b := range tt.in { | ||
s.Add(b...) | ||
} | ||
|
||
assert.ElementsMatch(t, tt.want, s.Keys()) | ||
}) | ||
} | ||
} | ||
|
||
func TestSet_Has(t *testing.T) { | ||
var s Set[string] | ||
|
||
assert.False(t, s.Has("foo")) | ||
|
||
s.Add("foo") | ||
|
||
assert.True(t, s.Has("foo")) | ||
assert.False(t, s.Has("bar")) | ||
} | ||
|
||
func TestSet_Delete(t *testing.T) { | ||
var s Set[string] | ||
|
||
assert.False(t, s.Has("foo")) | ||
|
||
s.Add("foo") | ||
s.Add("bar") | ||
s.Add("biz") | ||
assert.True(t, s.Has("foo")) | ||
assert.True(t, s.Has("bar")) | ||
assert.True(t, s.Has("biz")) | ||
|
||
s.Delete("bar", "biz") | ||
assert.True(t, s.Has("foo")) | ||
assert.False(t, s.Has("bar")) | ||
assert.False(t, s.Has("biz")) | ||
} |