-
Notifications
You must be signed in to change notification settings - Fork 246
/
onrset.go
111 lines (94 loc) · 2.58 KB
/
onrset.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package tuple
import (
"maps"
expmaps "golang.org/x/exp/maps"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
// ONRSet is a set of ObjectAndRelation's.
type ONRSet struct {
onrs map[string]*core.ObjectAndRelation
}
// NewONRSet creates a new set.
func NewONRSet(onrs ...*core.ObjectAndRelation) *ONRSet {
created := &ONRSet{
onrs: map[string]*core.ObjectAndRelation{},
}
created.Update(onrs)
return created
}
// Length returns the size of the set.
func (ons *ONRSet) Length() uint64 {
return uint64(len(ons.onrs))
}
// IsEmpty returns whether the set is empty.
func (ons *ONRSet) IsEmpty() bool {
return len(ons.onrs) == 0
}
// Has returns true if the set contains the given ONR.
func (ons *ONRSet) Has(onr *core.ObjectAndRelation) bool {
_, ok := ons.onrs[StringONR(onr)]
return ok
}
// Add adds the given ONR to the set. Returns true if the object was not in the set before this
// call and false otherwise.
func (ons *ONRSet) Add(onr *core.ObjectAndRelation) bool {
if _, ok := ons.onrs[StringONR(onr)]; ok {
return false
}
ons.onrs[StringONR(onr)] = onr
return true
}
// Update updates the set by adding the given ONRs to it.
func (ons *ONRSet) Update(onrs []*core.ObjectAndRelation) {
for _, onr := range onrs {
ons.Add(onr)
}
}
// UpdateFrom updates the set by adding the ONRs found in the other set to it.
func (ons *ONRSet) UpdateFrom(otherSet *ONRSet) {
for _, onr := range otherSet.onrs {
ons.Add(onr)
}
}
// Intersect returns an intersection between this ONR set and the other set provided.
func (ons *ONRSet) Intersect(otherSet *ONRSet) *ONRSet {
updated := NewONRSet()
for _, onr := range ons.onrs {
if otherSet.Has(onr) {
updated.Add(onr)
}
}
return updated
}
// Subtract returns a subtraction from this ONR set of the other set provided.
func (ons *ONRSet) Subtract(otherSet *ONRSet) *ONRSet {
updated := NewONRSet()
for _, onr := range ons.onrs {
if !otherSet.Has(onr) {
updated.Add(onr)
}
}
return updated
}
// With returns a copy of this ONR set with the given element added.
func (ons *ONRSet) With(onr *core.ObjectAndRelation) *ONRSet {
updated := &ONRSet{
onrs: maps.Clone(ons.onrs),
}
updated.Add(onr)
return updated
}
// Union returns a copy of this ONR set with the other set's elements added in.
func (ons *ONRSet) Union(otherSet *ONRSet) *ONRSet {
updated := &ONRSet{
onrs: maps.Clone(ons.onrs),
}
for _, current := range otherSet.onrs {
updated.Add(current)
}
return updated
}
// AsSlice returns the ONRs found in the set as a slice.
func (ons *ONRSet) AsSlice() []*core.ObjectAndRelation {
return expmaps.Values(ons.onrs)
}