| 1 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package a |
| 6 |
|
| 7 |
// SliceEqual reports whether two slices are equal: the same length and all |
| 8 |
// elements equal. All floating point NaNs are considered equal. |
| 9 |
func SliceEqual[Elem comparable](s1, s2 []Elem) bool { |
| 10 |
if len(s1) != len(s2) { |
| 11 |
return false |
| 12 |
} |
| 13 |
for i, v1 := range s1 { |
| 14 |
v2 := s2[i] |
| 15 |
if v1 != v2 { |
| 16 |
isNaN := func(f Elem) bool { return f != f } |
| 17 |
if !isNaN(v1) || !isNaN(v2) { |
| 18 |
return false |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
return true |
| 23 |
} |
| 24 |
|
| 25 |
// A Set is a set of elements of some type. |
| 26 |
type Set[Elem comparable] struct { |
| 27 |
m map[Elem]struct{} |
| 28 |
} |
| 29 |
|
| 30 |
// Make makes a new set. |
| 31 |
func Make[Elem comparable]() Set[Elem] { |
| 32 |
return Set[Elem]{m: make(map[Elem]struct{})} |
| 33 |
} |
| 34 |
|
| 35 |
// Add adds an element to a set. |
| 36 |
func (s Set[Elem]) Add(v Elem) { |
| 37 |
s.m[v] = struct{}{} |
| 38 |
} |
| 39 |
|
| 40 |
// Delete removes an element from a set. If the element is not present |
| 41 |
// in the set, this does nothing. |
| 42 |
func (s Set[Elem]) Delete(v Elem) { |
| 43 |
delete(s.m, v) |
| 44 |
} |
| 45 |
|
| 46 |
// Contains reports whether v is in the set. |
| 47 |
func (s Set[Elem]) Contains(v Elem) bool { |
| 48 |
_, ok := s.m[v] |
| 49 |
return ok |
| 50 |
} |
| 51 |
|
| 52 |
// Len returns the number of elements in the set. |
| 53 |
func (s Set[Elem]) Len() int { |
| 54 |
return len(s.m) |
| 55 |
} |
| 56 |
|
| 57 |
// Values returns the values in the set. |
| 58 |
// The values will be in an indeterminate order. |
| 59 |
func (s Set[Elem]) Values() []Elem { |
| 60 |
r := make([]Elem, 0, len(s.m)) |
| 61 |
for v := range s.m { |
| 62 |
r = append(r, v) |
| 63 |
} |
| 64 |
return r |
| 65 |
} |
| 66 |
|
| 67 |
// Equal reports whether two sets contain the same elements. |
| 68 |
func Equal[Elem comparable](s1, s2 Set[Elem]) bool { |
| 69 |
if len(s1.m) != len(s2.m) { |
| 70 |
return false |
| 71 |
} |
| 72 |
for v1 := range s1.m { |
| 73 |
if !s2.Contains(v1) { |
| 74 |
return false |
| 75 |
} |
| 76 |
} |
| 77 |
return true |
| 78 |
} |
| 79 |
|
| 80 |
// Copy returns a copy of s. |
| 81 |
func (s Set[Elem]) Copy() Set[Elem] { |
| 82 |
r := Set[Elem]{m: make(map[Elem]struct{}, len(s.m))} |
| 83 |
for v := range s.m { |
| 84 |
r.m[v] = struct{}{} |
| 85 |
} |
| 86 |
return r |
| 87 |
} |
| 88 |
|
| 89 |
// AddSet adds all the elements of s2 to s. |
| 90 |
func (s Set[Elem]) AddSet(s2 Set[Elem]) { |
| 91 |
for v := range s2.m { |
| 92 |
s.m[v] = struct{}{} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// SubSet removes all elements in s2 from s. |
| 97 |
// Values in s2 that are not in s are ignored. |
| 98 |
func (s Set[Elem]) SubSet(s2 Set[Elem]) { |
| 99 |
for v := range s2.m { |
| 100 |
delete(s.m, v) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Intersect removes all elements from s that are not present in s2. |
| 105 |
// Values in s2 that are not in s are ignored. |
| 106 |
func (s Set[Elem]) Intersect(s2 Set[Elem]) { |
| 107 |
for v := range s.m { |
| 108 |
if !s2.Contains(v) { |
| 109 |
delete(s.m, v) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Iterate calls f on every element in the set. |
| 115 |
func (s Set[Elem]) Iterate(f func(Elem)) { |
| 116 |
for v := range s.m { |
| 117 |
f(v) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// Filter deletes any elements from s for which f returns false. |
| 122 |
func (s Set[Elem]) Filter(f func(Elem) bool) { |
| 123 |
for v := range s.m { |
| 124 |
if !f(v) { |
| 125 |
delete(s.m, v) |
| 126 |
} |
| 127 |
} |
| 128 |
} |