Skip to content

Commit

Permalink
Update Set API (#159)
Browse files Browse the repository at this point in the history
* Update Set API (set package)
* Update go modules
  • Loading branch information
moorara authored Jul 2, 2023
1 parent a2a6114 commit 8cdac42
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
29 changes: 17 additions & 12 deletions set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Set[T any] interface {
Remove(...T)
Cardinality() int
IsEmpty() bool
Contains(T) bool
Contains(...T) bool
Equals(Set[T]) bool
Members() []T
Clone() Set[T]
Expand All @@ -39,6 +39,16 @@ func New[T any](equal generic.EqualFunc[T]) Set[T] {
}
}

func (s *set[T]) find(v T) int {
for i, member := range s.members {
if s.equal(member, v) {
return i
}
}

return -1
}

func (s *set[T]) Add(vals ...T) {
for _, v := range vals {
if !s.Contains(v) {
Expand All @@ -49,7 +59,7 @@ func (s *set[T]) Add(vals ...T) {

func (s *set[T]) Remove(vals ...T) {
for _, v := range vals {
if i := s.contains(v); i != -1 {
if i := s.find(v); i != -1 {
s.members = append(s.members[:i], s.members[i+1:]...)
}
}
Expand All @@ -63,18 +73,13 @@ func (s *set[T]) IsEmpty() bool {
return len(s.members) == 0
}

func (s *set[T]) Contains(v T) bool {
return s.contains(v) != -1
}

func (s *set[T]) contains(v T) int {
for i, member := range s.members {
if s.equal(member, v) {
return i
func (s *set[T]) Contains(vals ...T) bool {
for _, v := range vals {
if s.find(v) == -1 {
return false
}
}

return -1
return true
}

func (s *set[T]) Equals(t Set[T]) bool {
Expand Down
10 changes: 5 additions & 5 deletions set/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestSet_Contains(t *testing.T) {
tests := []struct {
name string
s *set[string]
val string
vals []string
expected bool
}{
{
Expand All @@ -170,7 +170,7 @@ func TestSet_Contains(t *testing.T) {
equal: eqFunc,
members: []string{},
},
val: "c",
vals: []string{"c"},
expected: false,
},
{
Expand All @@ -179,7 +179,7 @@ func TestSet_Contains(t *testing.T) {
equal: eqFunc,
members: []string{"a", "b"},
},
val: "c",
vals: []string{"c"},
expected: false,
},
{
Expand All @@ -188,14 +188,14 @@ func TestSet_Contains(t *testing.T) {
equal: eqFunc,
members: []string{"a", "b", "c", "d"},
},
val: "c",
vals: []string{"b", "c"},
expected: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
b := tc.s.Contains(tc.val)
b := tc.s.Contains(tc.vals...)
assert.Equal(t, tc.expected, b)
})
}
Expand Down

0 comments on commit 8cdac42

Please sign in to comment.