add symmetric difference in sets

This commit is contained in:
weilaaa
2022-09-07 20:25:44 +08:00
parent e7192a4955
commit 2b55c94e37
27 changed files with 168 additions and 54 deletions

View File

@@ -255,15 +255,15 @@ func (s $.type|public$) Clone() $.type|public$ {
return result
}
// Difference returns a set of objects that are not in s2
// Difference returns a set of objects that are not in s2.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s $.type|public$) Difference(s2 $.type|public$) $.type|public$ {
func (s1 $.type|public$) Difference(s2 $.type|public$) $.type|public$ {
result := New$.type|public$()
for key := range s {
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
@@ -271,6 +271,16 @@ func (s $.type|public$) Difference(s2 $.type|public$) $.type|public$ {
return result
}
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 $.type|public$) SymmetricDifference(s2 $.type|public$) $.type|public$ {
return s1.Difference(s2).Union(s2.Difference(s1))
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}

View File

@@ -96,15 +96,15 @@ func (s Byte) Clone() Byte {
return result
}
// Difference returns a set of objects that are not in s2
// Difference returns a set of objects that are not in s2.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s Byte) Difference(s2 Byte) Byte {
func (s1 Byte) Difference(s2 Byte) Byte {
result := NewByte()
for key := range s {
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
@@ -112,6 +112,16 @@ func (s Byte) Difference(s2 Byte) Byte {
return result
}
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Byte) SymmetricDifference(s2 Byte) Byte {
return s1.Difference(s2).Union(s2.Difference(s1))
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}

View File

@@ -96,15 +96,15 @@ func (s Int) Clone() Int {
return result
}
// Difference returns a set of objects that are not in s2
// Difference returns a set of objects that are not in s2.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s Int) Difference(s2 Int) Int {
func (s1 Int) Difference(s2 Int) Int {
result := NewInt()
for key := range s {
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
@@ -112,6 +112,16 @@ func (s Int) Difference(s2 Int) Int {
return result
}
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Int) SymmetricDifference(s2 Int) Int {
return s1.Difference(s2).Union(s2.Difference(s1))
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}

View File

@@ -96,15 +96,15 @@ func (s Int64) Clone() Int64 {
return result
}
// Difference returns a set of objects that are not in s2
// Difference returns a set of objects that are not in s2.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s Int64) Difference(s2 Int64) Int64 {
func (s1 Int64) Difference(s2 Int64) Int64 {
result := NewInt64()
for key := range s {
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
@@ -112,6 +112,16 @@ func (s Int64) Difference(s2 Int64) Int64 {
return result
}
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Int64) SymmetricDifference(s2 Int64) Int64 {
return s1.Difference(s2).Union(s2.Difference(s1))
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}

View File

@@ -96,15 +96,15 @@ func (s String) Clone() String {
return result
}
// Difference returns a set of objects that are not in s2
// Difference returns a set of objects that are not in s2.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s String) Difference(s2 String) String {
func (s1 String) Difference(s2 String) String {
result := NewString()
for key := range s {
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
@@ -112,6 +112,16 @@ func (s String) Difference(s2 String) String {
return result
}
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 String) SymmetricDifference(s2 String) String {
return s1.Difference(s2).Union(s2.Difference(s1))
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}

View File

@@ -674,6 +674,7 @@ func tcNameToName(in string) types.Name {
strings.HasPrefix(in, "chan<-") ||
strings.HasPrefix(in, "chan ") ||
strings.HasPrefix(in, "func(") ||
strings.HasPrefix(in, "func (") ||
strings.HasPrefix(in, "*") ||
strings.HasPrefix(in, "map[") ||
strings.HasPrefix(in, "[") {

4
vendor/modules.txt vendored
View File

@@ -2090,7 +2090,7 @@ k8s.io/cri-api/pkg/errors
## explicit; go 1.19
k8s.io/csi-translation-lib
k8s.io/csi-translation-lib/plugins
# k8s.io/gengo v0.0.0-20211129171323-c02415ce4185 => k8s.io/gengo v0.0.0-20211129171323-c02415ce4185
# k8s.io/gengo v0.0.0-20220902162205-c0856e24416d => k8s.io/gengo v0.0.0-20220902162205-c0856e24416d
## explicit; go 1.13
k8s.io/gengo/args
k8s.io/gengo/examples/deepcopy-gen/generators
@@ -2840,7 +2840,7 @@ sigs.k8s.io/yaml
# k8s.io/controller-manager => ./staging/src/k8s.io/controller-manager
# k8s.io/cri-api => ./staging/src/k8s.io/cri-api
# k8s.io/csi-translation-lib => ./staging/src/k8s.io/csi-translation-lib
# k8s.io/gengo => k8s.io/gengo v0.0.0-20211129171323-c02415ce4185
# k8s.io/gengo => k8s.io/gengo v0.0.0-20220902162205-c0856e24416d
# k8s.io/klog/v2 => k8s.io/klog/v2 v2.80.0
# k8s.io/kube-aggregator => ./staging/src/k8s.io/kube-aggregator
# k8s.io/kube-controller-manager => ./staging/src/k8s.io/kube-controller-manager