-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.go
85 lines (68 loc) · 1.94 KB
/
intersection.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cantor
type intersection[T comparable] struct {
arg ReadableSet[T]
args []Container[T]
}
func newIntersection[T comparable](arg ReadableSet[T], args ...Container[T]) ReadableSet[T] {
return intersection[T]{
arg: arg,
args: args,
}
}
func (set intersection[T]) Contains(element T) bool {
if !set.arg.Contains(element) {
return false
}
for _, arg := range set.args {
if !arg.Contains(element) {
return false
}
}
return true
}
func (set intersection[T]) Union(other ReadableSet[T]) ReadableSet[T] {
return newUnion[T](set, other)
}
func (set intersection[T]) Intersect(other Container[T]) ReadableSet[T] {
return newIntersection[T](set.arg, append(set.args, other)...)
}
func (set intersection[T]) Complement() ImplicitSet[T] {
return NewImplicitSet(func(element T) bool {
return !set.Contains(element)
})
}
func (set intersection[T]) Difference(other Container[T]) ReadableSet[T] {
return set.Intersect(NewImplicitSet[T](func(element T) bool {
return !other.Contains(element)
}))
}
func (set intersection[T]) SymmetricDifference(other ReadableSet[T]) ReadableSet[T] {
return set.Difference(other).Union(other.Difference(set))
}
func (set intersection[T]) Subset(other Container[T]) bool {
return set.Difference(other).Size() == 0
}
func (set intersection[T]) StrictSubset(other ReadableSet[T]) bool {
return set.Difference(other).Size() == 0 && other.Difference(set).Size() > 0
}
func (set intersection[T]) Equals(other ReadableSet[T]) bool {
return set.SymmetricDifference(other).Size() == 0
}
func (set intersection[T]) Elements() Iterator[T] {
return func(yield func(element T) (next bool)) {
set.arg.Elements()(func(element T) (next bool) {
for _, arg := range set.args {
if !arg.Contains(element) {
return true
}
}
return yield(element)
})
}
}
func (set intersection[T]) String() string {
return toString[T](set)
}
func (set intersection[T]) Size() int {
return count(set.Elements())
}