| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2022 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. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
) |
| 12 |
|
| 13 |
func main() { |
| 14 |
x := [][]int{{1}} |
| 15 |
y := [][]int{{2, 3}} |
| 16 |
IntersectSS(x, y) |
| 17 |
} |
| 18 |
|
| 19 |
type list[E any] interface { |
| 20 |
~[]E |
| 21 |
Equal(x, y E) bool |
| 22 |
} |
| 23 |
|
| 24 |
// ss is a set of sets |
| 25 |
type ss[E comparable, T []E] []T |
| 26 |
|
| 27 |
func (ss[E, T]) Equal(a, b T) bool { |
| 28 |
return SetEq(a, b) |
| 29 |
} |
| 30 |
|
| 31 |
func IntersectSS[E comparable](x, y [][]E) [][]E { |
| 32 |
return IntersectT[[]E, ss[E, []E]](ss[E, []E](x), ss[E, []E](y)) |
| 33 |
} |
| 34 |
|
| 35 |
func IntersectT[E any, L list[E]](x, y L) L { |
| 36 |
var z L |
| 37 |
outer: |
| 38 |
for _, xe := range x { |
| 39 |
fmt.Println("xe", xe) |
| 40 |
for _, ye := range y { |
| 41 |
fmt.Println("ye", ye) |
| 42 |
fmt.Println("x", x) |
| 43 |
if x.Equal(xe, ye) { |
| 44 |
fmt.Println("appending") |
| 45 |
z = append(z, xe) |
| 46 |
continue outer |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
return z |
| 51 |
} |
| 52 |
|
| 53 |
func SetEq[S []E, E comparable](x, y S) bool { |
| 54 |
fmt.Println("SetEq", x, y) |
| 55 |
outer: |
| 56 |
for _, xe := range x { |
| 57 |
for _, ye := range y { |
| 58 |
if xe == ye { |
| 59 |
continue outer |
| 60 |
} |
| 61 |
} |
| 62 |
return false // xs wasn't found in y |
| 63 |
} |
| 64 |
return true |
| 65 |
} |