Skip to content

Commit

Permalink
internal/mapset: rename FromSlice to FromItems and use variadic args
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Jakubowski <[email protected]>
  • Loading branch information
patjakdev committed Sep 24, 2024
1 parent f11e3cf commit 99f7de5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/mapset/immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
type ImmutableMapSet[T comparable] MapSet[T]

func Immutable[T comparable](args ...T) ImmutableMapSet[T] {
return ImmutableMapSet[T](*FromSlice(args))
return ImmutableMapSet[T](*FromItems(args...))
}

// Contains returns whether the item exists in the set
Expand Down
6 changes: 3 additions & 3 deletions internal/mapset/mapset.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func Make[T comparable](args ...int) *MapSet[T] {
return &MapSet[T]{m: make(map[T]struct{}, size)}
}

// FromSlice creates a MapSet of size len(items) and calls Add for each of the items to it.
func FromSlice[T comparable](items []T) *MapSet[T] {
// FromItems creates a MapSet of size len(items) and calls Add for each of the items to it.
func FromItems[T comparable](items ...T) *MapSet[T] {
h := Make[T](len(items))
for _, i := range items {
h.Add(i)
Expand Down Expand Up @@ -131,6 +131,6 @@ func (h *MapSet[T]) UnmarshalJSON(b []byte) error {
return err
}

*h = *FromSlice(s)
*h = *FromItems(s...)
return nil
}
28 changes: 14 additions & 14 deletions internal/mapset/mapset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestHashSet(t *testing.T) {
})

t.Run("new from slice", func(t *testing.T) {
s := FromSlice([]int{1, 2, 2, 3})
s := FromItems(1, 2, 2, 3)
testutil.Equals(t, s.Len(), 3)
testutil.Equals(t, s.Contains(1), true)
testutil.Equals(t, s.Contains(2), true)
Expand All @@ -69,17 +69,17 @@ func TestHashSet(t *testing.T) {
testutil.Equals(t, s.Slice(), []int{})

inSlice := []int{1, 2, 3}
s = FromSlice(inSlice)
s = FromItems(inSlice...)
outSlice := s.Slice()
slices.Sort(outSlice)
testutil.Equals(t, inSlice, outSlice)
})

t.Run("equal", func(t *testing.T) {
s1 := FromSlice([]int{1, 2, 3})
s1 := FromItems(1, 2, 3)
testutil.Equals(t, s1.Equal(s1), true)

s2 := FromSlice([]int{1, 2, 3})
s2 := FromItems(1, 2, 3)
testutil.Equals(t, s1.Equal(s2), true)

s2.Add(4)
Expand All @@ -94,7 +94,7 @@ func TestHashSet(t *testing.T) {
})

t.Run("iterate", func(t *testing.T) {
s1 := FromSlice([]int{1, 2, 3})
s1 := FromItems(1, 2, 3)

s2 := Make[int]()
s1.Iterate(func(item int) bool {
Expand All @@ -106,7 +106,7 @@ func TestHashSet(t *testing.T) {
})

t.Run("iterate break early", func(t *testing.T) {
s1 := FromSlice([]int{1, 2, 3})
s1 := FromItems(1, 2, 3)

i := 0
var items []int
Expand All @@ -127,19 +127,19 @@ func TestHashSet(t *testing.T) {
})

t.Run("intersection with overlap", func(t *testing.T) {
s1 := FromSlice([]int{1, 2, 3})
s2 := FromSlice([]int{2, 3, 4})
s1 := FromItems(1, 2, 3)
s2 := FromItems(2, 3, 4)

s3 := s1.Intersection(s2)
testutil.Equals(t, s3, FromSlice([]int{2, 3}))
testutil.Equals(t, s3, FromItems(2, 3))

s4 := s1.Intersection(s2)
testutil.Equals(t, s4, FromSlice([]int{2, 3}))
testutil.Equals(t, s4, FromItems(2, 3))
})

t.Run("intersection disjoint", func(t *testing.T) {
s1 := FromSlice([]int{1, 2})
s2 := FromSlice([]int{3, 4})
s1 := FromItems(1, 2)
s2 := FromItems(3, 4)

s3 := s1.Intersection(s2)
testutil.Equals(t, s3.Len(), 0)
Expand All @@ -158,7 +158,7 @@ func TestHashSet(t *testing.T) {
})

t.Run("encode json", func(t *testing.T) {
s := FromSlice([]int{1, 2, 3})
s := FromItems(1, 2, 3)

out, err := json.Marshal(s)

Expand All @@ -179,7 +179,7 @@ func TestHashSet(t *testing.T) {
var s1 MapSet[int]
err := s1.UnmarshalJSON([]byte("[2,3,1,2]"))
testutil.OK(t, err)
testutil.Equals(t, &s1, FromSlice([]int{1, 2, 3}))
testutil.Equals(t, &s1, FromItems(1, 2, 3))
})

t.Run("decode json empty", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/exp/batch/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func TestFindVariables(t *testing.T) {
t.Parallel()
out := mapset.Make[types.String]()
findVariables(out, tt.in)
testutil.Equals(t, out, mapset.FromSlice(tt.out))
testutil.Equals(t, out, mapset.FromItems(tt.out...))
})
}

Expand Down

0 comments on commit 99f7de5

Please sign in to comment.