-
Notifications
You must be signed in to change notification settings - Fork 0
/
setmap.go
125 lines (110 loc) · 2.69 KB
/
setmap.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package setmap
// SetMap is map<K, set<V>>
type SetMap[K, V comparable] struct {
m map[K]map[V]struct{}
}
func NewSetMap[K, V comparable]() SetMap[K, V] {
return SetMap[K, V]{make(map[K]map[V]struct{})}
}
// Add a value to the set whose ID is key
func (m SetMap[K, V]) Add(key K, value V) {
if set, ok := m.m[key]; ok {
set[value] = struct{}{}
} else {
m.m[key] = map[V]struct{}{value: {}}
}
}
// Remove a value from the set whose ID is key
func (m SetMap[K, V]) Remove(key K, value V) {
if set, ok := m.m[key]; ok {
delete(set, value)
if len(set) <= 0 {
delete(m.m, key)
}
}
}
// Exists show if a value is in the set whose ID is key
func (m SetMap[K, V]) Exists(key K, value V) bool {
var set map[V]struct{}
var ok bool
if set, ok = m.m[key]; ok {
_, ok = set[value]
}
return ok
}
// GetSet get all the values from the set whose ID is key
func (m SetMap[K, V]) GetSet(key K) []V {
var r []V
if set, ok := m.m[key]; ok {
r = make([]V, len(set))
i := 0
for value := range set {
r[i] = value
i++
}
}
return r
}
// SetMapaMteS consists of a SetMap map<K, set<V>> and a reverse SetMap map<V, set<K>>
// so it can show K和V之间的多对多关系
type SetMapaMteS[K, V comparable] struct {
SetMap[K, V]
reverse SetMap[V, K]
}
func NewSetMapaMteS[K, V comparable]() SetMapaMteS[K, V] {
return SetMapaMteS[K, V]{
SetMap: NewSetMap[K, V](),
reverse: NewSetMap[V, K](),
}
}
func (m SetMapaMteS[K, V]) Add(key K, value V) {
m.SetMap.Add(key, value)
m.reverse.Add(value, key)
}
func (m SetMapaMteS[K, V]) Remove(key K, value V) {
m.SetMap.Remove(key, value)
m.reverse.Remove(value, key)
}
func (m SetMapaMteS[K, V]) RemoveValue(value V) {
if set, ok := m.reverse.m[value]; ok {
for key := range set {
m.SetMap.Remove(key, value)
}
delete(m.reverse.m, value)
}
}
func (m SetMapaMteS[K, V]) RemoveKey(key K) {
if set, ok := m.m[key]; ok {
for value := range set {
m.reverse.Remove(value, key)
}
delete(m.m, key)
}
}
func (m SetMapaMteS[K, V]) GetKeys(value V) []K {
return m.reverse.GetSet(value)
}
// GetUniqueValues only get the key's values that is not exists in other key's set
func (m SetMapaMteS[K, V]) GetUniqueValues(key K) []V {
var r []V
if set, ok := m.m[key]; ok {
for value := range set {
if len(m.reverse.GetSet(value)) <= 1 { // only have this value?
r = append(r, value) // that's what I find
}
}
}
return r
}
// GetUniqueKeys only get the value's keys that is not other value's key
func (m SetMapaMteS[K, V]) GetUniqueKeys(value V) []K {
var r []K
if set, ok := m.reverse.m[value]; ok {
for key := range set {
if len(m.GetSet(key)) <= 1 { // only have this key?
r = append(r, key) // that's what I find
}
}
}
return r
}