-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringset.go
157 lines (137 loc) · 3 KB
/
stringset.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2021 Cloud Privacy Labs, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lpg
import (
"encoding/json"
"sort"
"strings"
)
type StringSet struct {
M map[string]struct{}
}
func NewStringSet(s ...string) StringSet {
ret := StringSet{M: make(map[string]struct{}, len(s))}
for _, x := range s {
ret.M[x] = struct{}{}
}
return ret
}
func (set StringSet) Clone() StringSet {
return StringSet{M: set.M}
}
func (set StringSet) IsEqual(s StringSet) bool {
return len(set.M) == len(s.M) && set.HasAllSet(s)
}
func (set StringSet) Has(s string) bool {
_, ok := set.M[s]
return ok
}
func (set StringSet) HasAny(s ...string) bool {
for _, x := range s {
if _, ok := set.M[x]; ok {
return true
}
}
return false
}
func (set StringSet) HasAnySet(s StringSet) bool {
for x := range s.M {
if _, ok := set.M[x]; ok {
return true
}
}
return false
}
func (set StringSet) HasAll(s ...string) bool {
for _, x := range s {
if _, ok := set.M[x]; !ok {
return false
}
}
return true
}
func (set StringSet) HasAllSet(s StringSet) bool {
for x := range s.M {
if _, ok := set.M[x]; !ok {
return false
}
}
return true
}
func (set *StringSet) Add(s ...string) *StringSet {
m := set.M
set.M = make(map[string]struct{}, len(s)+len(m))
for x := range m {
set.M[x] = struct{}{}
}
for _, x := range s {
set.M[x] = struct{}{}
}
return set
}
func (set *StringSet) AddSet(s StringSet) *StringSet {
m := set.M
set.M = make(map[string]struct{}, len(s.M)+len(m))
for x := range m {
set.M[x] = struct{}{}
}
for x := range s.M {
set.M[x] = struct{}{}
}
return set
}
func (set *StringSet) Remove(s ...string) *StringSet {
m := set.M
set.M = make(map[string]struct{}, len(m))
for x := range m {
has := false
for _, y := range s {
if y == x {
has = true
break
}
}
if !has {
set.M[x] = struct{}{}
}
}
return set
}
func (set StringSet) Slice() []string {
ret := make([]string, 0, len(set.M))
for k := range set.M {
ret = append(ret, k)
}
return ret
}
func (set StringSet) SortedSlice() []string {
ret := set.Slice()
sort.Strings(ret)
return ret
}
func (set StringSet) String() string {
return strings.Join(set.Slice(), ", ")
}
func (set StringSet) MarshalJSON() ([]byte, error) {
return json.Marshal(set.Slice())
}
func (set *StringSet) UnmarshalJSON(in []byte) error {
var arr []string
if err := json.Unmarshal(in, &arr); err != nil {
return err
}
*set = NewStringSet(arr...)
return nil
}
func (set StringSet) Len() int { return len(set.M) }