-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcartesian_product.go
More file actions
38 lines (31 loc) · 982 Bytes
/
cartesian_product.go
File metadata and controls
38 lines (31 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package set
import "fmt"
// Pair represents an ordered pair of elements for use in Cartesian products.
type Pair[T comparable] struct {
First T
Second T
}
// String returns a string representation of the pair in the format "(First, Second)".
func (p Pair[T]) String() string {
return fmt.Sprintf("(%v, %v)", p.First, p.Second)
}
// CartesianProduct returns a new set containing all possible ordered pairs
// (x, y) where x is from the first set and y is from the second set.
// For sets A and B, it returns A × B = {(x, y) | x ∈ A, y ∈ B}.
//
// For example, if A = {1, 2} and B = {3, 4}, then
// CartesianProduct(A, B) = {(1, 3), (1, 4), (2, 3), (2, 4)}.
func CartesianProduct[T comparable](s1, s2 Set[T]) Set[Pair[T]] {
result := NewHashSet[Pair[T]]()
s1Elems := s1.ToSlice()
s2Elems := s2.ToSlice()
for _, elem1 := range s1Elems {
for _, elem2 := range s2Elems {
result.Insert(Pair[T]{
First: elem1,
Second: elem2,
})
}
}
return result
}