-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplicit_set.go
64 lines (56 loc) · 2.52 KB
/
implicit_set.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
package cantor
// [ImplicitSet] represents a set, which is only defined by an arbitrary [Predicate].
// Thus, an [ImplicitSet] can represent an infinite amount of elements without performance or memory overhead.
// Due to its unconstrained nature, this type of set can only be used for lookups.
//
// [ImplicitSet] implements [Container].
type ImplicitSet[T comparable] Predicate[T]
// [NewImplicitSet] returns an [ImplicitSet] representing all elements e where the given [Predicate] returns true.
func NewImplicitSet[T comparable](predicate Predicate[T]) ImplicitSet[T] {
return ImplicitSet[T](predicate)
}
func (predicate ImplicitSet[T]) Contains(element T) bool {
return predicate(element)
}
// Union returns an [ImplicitSet] set representing the set union of its arguments.
//
// The result is a data view and will reflect future changes of the underlying structures.
func (predicate ImplicitSet[T]) Union(other Container[T]) ImplicitSet[T] {
return func(element T) bool {
return predicate(element) || other.Contains(element)
}
}
// Intersect returns an [ImplicitSet] set representing the set intersection of its arguments.
//
// The result is a data view and will reflect future changes of the underlying structures.
func (predicate ImplicitSet[T]) Intersect(other Container[T]) ImplicitSet[T] {
return func(element T) bool {
return predicate(element) && other.Contains(element)
}
}
// Complement returns an [ImplicitSet], that contains all elements not contained in this [ImplicitSet].
//
// The result is a data view and will reflect future changes of the underlying structures.
func (predicate ImplicitSet[T]) Complement() ImplicitSet[T] {
return func(element T) bool {
return !predicate.Contains(element)
}
}
// Difference returns an [ImplicitSet] set with all elements of this [ImplicitSet],
// which are not contained in the argument.
//
// The result is a data view and will reflect future changes of the underlying structures.
func (predicate ImplicitSet[T]) Difference(other Container[T]) ImplicitSet[T] {
return func(element T) bool {
return predicate(element) && !other.Contains(element)
}
}
// SymmetricDifference returns an [ImplicitSet] set with all elements of this [ImplicitSet] and the other [Container],
// which are contained in exactly one of the two.
//
// The result is a data view and will reflect future changes of the underlying structures.
func (predicate ImplicitSet[T]) SymmetricDifference(other Container[T]) ImplicitSet[T] {
return func(element T) bool {
return predicate(element) != other.Contains(element)
}
}