Skip to content

Commit

Permalink
internal/mapset: remove some unused methods on MapSet
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 4418dc6 commit f11e3cf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 69 deletions.
24 changes: 4 additions & 20 deletions internal/mapset/mapset.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ 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 AddSlice(items) on it.
// 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] {
h := Make[T](len(items))
h.AddSlice(items)
for _, i := range items {
h.Add(i)
}
return h
}

Expand All @@ -50,31 +52,13 @@ func (h *MapSet[T]) Add(item T) bool {
return !exists
}

// AddSlice adds a slice of items to the set, returning true if any new items were added to the set.
func (h *MapSet[T]) AddSlice(items []T) bool {
modified := false
for _, i := range items {
modified = h.Add(i) || modified
}
return modified
}

// Remove an item from the Set. Returns true if the item existed in the set.
func (h *MapSet[T]) Remove(item T) bool {
_, exists := h.m[item]
delete(h.m, item)
return exists
}

// RemoveSlice removes a slice of items from the set, returning true if any items existed in the set.
func (h *MapSet[T]) RemoveSlice(items []T) bool {
modified := false
for _, i := range items {
modified = h.Remove(i) || modified
}
return modified
}

// Contains returns whether the item exists in the set
func (h MapSet[T]) Contains(item T) bool {
_, exists := h.m[item]
Expand Down
49 changes: 0 additions & 49 deletions internal/mapset/mapset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,6 @@ func TestHashSet(t *testing.T) {
testutil.Equals(t, s.Add(1), false)
})

t.Run("add slice", func(t *testing.T) {
s := Make[int]()
s.AddSlice([]int{1, 2})
testutil.Equals(t, s.Contains(1), true)
testutil.Equals(t, s.Contains(2), true)
hashSetMustNotContain(t, s, 3)
})

t.Run("add same slice", func(t *testing.T) {
s := Make[int]()
testutil.Equals(t, s.AddSlice([]int{1, 2}), true)
testutil.Equals(t, s.AddSlice([]int{1, 2}), false)
})

t.Run("add disjoint slices", func(t *testing.T) {
s := Make[int]()
testutil.Equals(t, s.AddSlice([]int{1, 2}), true)
testutil.Equals(t, s.AddSlice([]int{3, 4}), true)
testutil.Equals(t, s.AddSlice([]int{1, 2, 3, 4}), false)
})

t.Run("add overlapping slices", func(t *testing.T) {
s := Make[int]()
testutil.Equals(t, s.AddSlice([]int{1, 2}), true)
testutil.Equals(t, s.AddSlice([]int{2, 3}), true)
testutil.Equals(t, s.AddSlice([]int{1, 3}), false)
})

t.Run("remove nonexistent", func(t *testing.T) {
s := Make[int]()
testutil.Equals(t, s.Remove(1), false)
Expand All @@ -81,27 +53,6 @@ func TestHashSet(t *testing.T) {
testutil.FatalIf(t, s.Contains(1), "set unexpectedly contained item")
})

t.Run("remove slice", func(t *testing.T) {
s := Make[int]()
s.AddSlice([]int{1, 2, 3})
s.RemoveSlice([]int{1, 2})
hashSetMustNotContain(t, s, 1)
hashSetMustNotContain(t, s, 2)
testutil.Equals(t, s.Contains(3), true)
})

t.Run("remove non-existent slice", func(t *testing.T) {
s := Make[int]()
testutil.Equals(t, s.RemoveSlice([]int{1, 2}), false)
})

t.Run("remove overlapping slice", func(t *testing.T) {
s := Make[int]()
s.Add(1)
testutil.Equals(t, s.RemoveSlice([]int{1, 2}), true)
testutil.Equals(t, s.RemoveSlice([]int{1, 2}), false)
})

t.Run("new from slice", func(t *testing.T) {
s := FromSlice([]int{1, 2, 2, 3})
testutil.Equals(t, s.Len(), 3)
Expand Down

0 comments on commit f11e3cf

Please sign in to comment.