3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
14 // _Equal reports whether two slices are equal: the same length and all
15 // elements equal. All floating point NaNs are considered equal.
16 func _SliceEqual[Elem comparable](s1, s2 []Elem) bool {
17 if len(s1) != len(s2) {
20 for i, v1 := range s1 {
23 isNaN := func(f Elem) bool { return f != f }
24 if !isNaN(v1) || !isNaN(v2) {
32 // A _Set is a set of elements of some type.
33 type _Set[Elem comparable] struct {
37 // _Make makes a new set.
38 func _Make[Elem comparable]() _Set[Elem] {
39 return _Set[Elem]{m: make(map[Elem]struct{})}
42 // Add adds an element to a set.
43 func (s _Set[Elem]) Add(v Elem) {
47 // Delete removes an element from a set. If the element is not present
48 // in the set, this does nothing.
49 func (s _Set[Elem]) Delete(v Elem) {
53 // Contains reports whether v is in the set.
54 func (s _Set[Elem]) Contains(v Elem) bool {
59 // Len returns the number of elements in the set.
60 func (s _Set[Elem]) Len() int {
64 // Values returns the values in the set.
65 // The values will be in an indeterminate order.
66 func (s _Set[Elem]) Values() []Elem {
67 r := make([]Elem, 0, len(s.m))
74 // _Equal reports whether two sets contain the same elements.
75 func _Equal[Elem comparable](s1, s2 _Set[Elem]) bool {
76 if len(s1.m) != len(s2.m) {
79 for v1 := range s1.m {
87 // Copy returns a copy of s.
88 func (s _Set[Elem]) Copy() _Set[Elem] {
89 r := _Set[Elem]{m: make(map[Elem]struct{}, len(s.m))}
96 // AddSet adds all the elements of s2 to s.
97 func (s _Set[Elem]) AddSet(s2 _Set[Elem]) {
103 // SubSet removes all elements in s2 from s.
104 // Values in s2 that are not in s are ignored.
105 func (s _Set[Elem]) SubSet(s2 _Set[Elem]) {
106 for v := range s2.m {
111 // Intersect removes all elements from s that are not present in s2.
112 // Values in s2 that are not in s are ignored.
113 func (s _Set[Elem]) Intersect(s2 _Set[Elem]) {
121 // Iterate calls f on every element in the set.
122 func (s _Set[Elem]) Iterate(f func(Elem)) {
128 // Filter deletes any elements from s for which f returns false.
129 func (s _Set[Elem]) Filter(f func(Elem) bool) {
139 if got := s1.Len(); got != 0 {
140 panic(fmt.Sprintf("Len of empty set = %d, want 0", got))
145 if got := s1.Len(); got != 1 {
146 panic(fmt.Sprintf("(%v).Len() == %d, want 1", s1, got))
151 if got := s1.Len(); got != 4 {
152 panic(fmt.Sprintf("(%v).Len() == %d, want 4", s1, got))
155 panic(fmt.Sprintf("(%v).Contains(1) == false, want true", s1))
158 panic(fmt.Sprintf("(%v).Contains(5) == true, want false", s1))
162 w1 := []int{1, 2, 3, 4}
163 if !_SliceEqual(vals, w1) {
164 panic(fmt.Sprintf("(%v).Values() == %v, want %v", s1, vals, w1))
169 s1 := _Make[string]()
170 s2 := _Make[string]()
172 panic(fmt.Sprintf("_Equal(%v, %v) = false, want true", s1, s2))
176 if got := s1.Len(); got != 2 {
177 panic(fmt.Sprintf("(%v).Len() == %d, want 2", s1, got))
180 panic(fmt.Sprintf("_Equal(%v, %v) = true, want false", s1, s2))
185 s1 := _Make[float64]()
189 panic(fmt.Sprintf("_Equal(%v, %v) = false, want true", s1, s2))
193 panic(fmt.Sprintf("_Equal(%v, %v) = true, want false", s1, s2))
205 if got := s1.Len(); got != 3 {
206 panic(fmt.Sprintf("(%v).Len() == %d, want 3", s1, got))
210 panic(fmt.Sprintf("_Equal(%v, %v) = false, want true", s1, s2))
222 if got := s1.Len(); got != 1 {
223 panic(fmt.Sprintf("(%v).Len() == %d, want 1", s1, got))
225 if vals, want := s1.Values(), []int{1}; !_SliceEqual(vals, want) {
226 panic(fmt.Sprintf("after SubSet got %v, want %v", vals, want))
230 func TestIntersect() {
238 if got := s1.Len(); got != 1 {
239 panic(fmt.Sprintf("(%v).Len() == %d, want 1", s1, got))
241 if vals, want := s1.Values(), []int{2}; !_SliceEqual(vals, want) {
242 panic(fmt.Sprintf("after Intersect got %v, want %v", vals, want))
253 s1.Iterate(func(i int) { tot += i })
255 panic(fmt.Sprintf("total of %v == %d, want 10", s1, tot))
264 s1.Filter(func(v int) bool { return v%2 == 0 })
265 if vals, want := s1.Values(), []int{2}; !_SliceEqual(vals, want) {
266 panic(fmt.Sprintf("after Filter got %v, want %v", vals, want))