Skip to content

Commit 2bc68a1

Browse files
committed
wip: more slice fixings
1 parent 9833881 commit 2bc68a1

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

collection.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package set
55

6-
import "fmt"
7-
86
// Collection is a minimal common interface that all sets implement.
97

108
// Fundamental set operations and familiar utility methods are part of this
@@ -187,11 +185,8 @@ func intersect[T any](destination, a, b Collection[T]) {
187185
}
188186

189187
func containsSlice[T any](col Collection[T], items []T) bool {
190-
fmt.Println("containsSlice", "col", col, "items", items)
191188
for _, item := range items {
192-
fmt.Println("checking item", item)
193189
if !col.Contains(item) {
194-
fmt.Println("missing item", item)
195190
return false
196191
}
197192
}

examples_set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func ExampleSet_ContainsSlice() {
107107
fmt.Println(s.ContainsSlice([]string{"red", "blue", "green"}))
108108

109109
// Output:
110-
// false
110+
// true
111111
// false
112112
// true
113113
}

hashset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func TestHashSet_EqualSlice(t *testing.T) {
600600
t.Run("duplicates", func(t *testing.T) {
601601
a := HashSetFrom[*company, string]([]*company{c1, c2, c3, c4, c5})
602602
b := []*company{c1, c2, c2, c3, c3, c4, c5}
603-
must.False(t, a.EqualSlice(b))
603+
must.True(t, a.EqualSlice(b))
604604
})
605605
}
606606

set.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ func (s *Set[T]) Contains(item T) bool {
139139
return exists
140140
}
141141

142-
// ContainsSlice returns whether all elements in items are present
143-
// in s.
142+
// ContainsSlice returns whether all elements in items are present in s.
144143
func (s *Set[T]) ContainsSlice(items []T) bool {
145144
return containsSlice(s, items)
146145
}

treeset.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,9 @@ func (s *TreeSet[T]) Contains(item T) bool {
307307
return s.locate(s.root, item) != nil
308308
}
309309

310-
// ContainsSlice returns whether s contains the same set of elements that are in
311-
// items. The items slice may contain duplicate elements.
312-
//
313-
// If the items slice is known to be set-like (no duplicates), EqualSlice provides
314-
// a more efficient implementation.
310+
// ContainsSlice returns whether all elements in items are present in s.
315311
func (s *TreeSet[T]) ContainsSlice(items []T) bool {
316-
for _, item := range items {
317-
if !s.Contains(item) {
318-
return false
319-
}
320-
}
321-
return true
312+
return containsSlice(s, items)
322313
}
323314

324315
// Size returns the number of elements in s.
@@ -483,11 +474,31 @@ func (s *TreeSet[T]) EqualSet(col Collection[T]) bool {
483474
}
484475

485476
// EqualSlice returns whether s and items contain the same elements.
477+
//
478+
// The items slice may contain duplicates.
479+
//
480+
// If the items slice is known to contain no duplicates, EqualSliceSet may be
481+
// used instead as a faster implementation.
482+
//
483+
// To detect if a slice is a subset of s, use ContainsSlice.
486484
func (s *TreeSet[T]) EqualSlice(items []T) bool {
485+
other := TreeSetFrom[T](items, s.comparison)
486+
return s.Equal(other)
487+
}
488+
489+
// EqualSliceSet returns whether s and items contain exactly the same elements.
490+
//
491+
// If items contains duplicates EqualSliceSet will return false. The elements of
492+
// items are assumed to be set-like. For comparing s to a slice that may contain
493+
// duplicate elements, use EqualSlice instead.
494+
//
495+
// To detect if a slice is a subset of s, use ContainsSlice.
496+
func (s *TreeSet[T]) EqualSliceSet(items []T) bool {
497+
// TODO optimize
487498
if s.Size() != len(items) {
488499
return false
489500
}
490-
return s.ContainsSlice(items)
501+
return s.EqualSlice(items)
491502
}
492503

493504
// String creates a string representation of s, using "%v" printf formatting

0 commit comments

Comments
 (0)