github.com/jxskiss/gopkg@v0.17.3/set/uint32.go (about) 1 // Code generated by go generate at 2021-05-30T01:20:54+08:00; DO NOT EDIT. 2 3 package set 4 5 import "encoding/json" 6 7 // Uint32 is uint32 set collection. 8 // The zero value of Uint32 is an empty instance ready to use. A zero Uint32 9 // value shall not be copied, or it may result incorrect behavior. 10 type Uint32 struct { 11 m map[uint32]struct{} 12 } 13 14 // NewUint32 creates Uint32 instance. 15 func NewUint32(vals ...uint32) Uint32 { 16 size := max(len(vals), minSize) 17 set := Uint32{ 18 m: make(map[uint32]struct{}, size), 19 } 20 set.Add(vals...) 21 return set 22 } 23 24 // NewUint32WithSize creates Uint32 instance with given initial size. 25 func NewUint32WithSize(size int) Uint32 { 26 set := Uint32{ 27 m: make(map[uint32]struct{}, size), 28 } 29 return set 30 } 31 32 // Size returns the size of set. 33 func (s Uint32) Size() int { return len(s.m) } 34 35 // Add adds values into the set. 36 func (s *Uint32) Add(vals ...uint32) { 37 if s.m == nil { 38 size := max(len(vals), minSize) 39 s.m = make(map[uint32]struct{}, size) 40 } 41 for idx := range vals { 42 s.m[vals[idx]] = struct{}{} 43 } 44 } 45 46 // Del deletes values from the set. 47 func (s *Uint32) Del(vals ...uint32) { 48 for idx := range vals { 49 delete(s.m, vals[idx]) 50 } 51 } 52 53 // Pop pops an element from the set, in no particular order. 54 func (s *Uint32) Pop() uint32 { 55 for val := range s.m { 56 delete(s.m, val) 57 return val 58 } 59 return 0 60 } 61 62 // Iterate iterates the set in no particular order and call the given function 63 // for each set element. 64 func (s Uint32) Iterate(fn func(uint32)) { 65 for val := range s.m { 66 fn(val) 67 } 68 } 69 70 // Contains returns true if the set contains all the values. 71 func (s Uint32) Contains(vals ...uint32) bool { 72 if len(vals) == 0 { 73 return false 74 } 75 for _, v := range vals { 76 if _, ok := s.m[v]; !ok { 77 return false 78 } 79 } 80 return true 81 } 82 83 // ContainsAny returns true if the set contains any of the values. 84 func (s Uint32) ContainsAny(vals ...uint32) bool { 85 for _, v := range vals { 86 if _, ok := s.m[v]; ok { 87 return true 88 } 89 } 90 return false 91 } 92 93 // Diff returns new Uint32 about the values which other set doesn't contain. 94 func (s Uint32) Diff(other Uint32) Uint32 { 95 res := NewUint32WithSize(s.Size()) 96 97 for val := range s.m { 98 if _, ok := other.m[val]; !ok { 99 res.m[val] = struct{}{} 100 } 101 } 102 return res 103 } 104 105 // DiffSlice is similar to Diff, but takes a slice as parameter. 106 func (s Uint32) DiffSlice(other []uint32) Uint32 { 107 if len(s.m) > len(other) { 108 tmp := NewUint32WithSize(len(other)) 109 dup := 0 110 for _, val := range other { 111 if _, ok := s.m[val]; ok { 112 dup++ 113 } 114 tmp.m[val] = struct{}{} 115 } 116 res := NewUint32WithSize(max(s.Size()-dup, 0)) 117 for val := range s.m { 118 if _, ok := tmp.m[val]; !ok { 119 res.m[val] = struct{}{} 120 } 121 } 122 return res 123 } else { 124 res := NewUint32WithSize(s.Size()) 125 for val := range s.m { 126 res.m[val] = struct{}{} 127 } 128 for _, val := range other { 129 if _, ok := res.m[val]; ok { 130 delete(res.m, val) 131 } 132 } 133 return res 134 } 135 } 136 137 // FilterInclude returns a new slice which contains values that present in 138 // the provided slice and also present in the Uint32 set. 139 func (s Uint32) FilterInclude(slice []uint32) []uint32 { 140 res := make([]uint32, 0, min(s.Size(), len(slice))) 141 for _, val := range slice { 142 if _, ok := s.m[val]; ok { 143 res = append(res, val) 144 } 145 } 146 return res 147 } 148 149 // FilterExclude returns a new slice which contains values that present in 150 // the provided slice but don't present in the Uint32 set. 151 func (s Uint32) FilterExclude(slice []uint32) []uint32 { 152 res := make([]uint32, 0, len(slice)) 153 for _, val := range slice { 154 if _, ok := s.m[val]; !ok { 155 res = append(res, val) 156 } 157 } 158 return res 159 } 160 161 // Intersect returns new Uint32 about values which other set also contains. 162 func (s Uint32) Intersect(other Uint32) Uint32 { 163 res := NewUint32WithSize(min(s.Size(), other.Size())) 164 165 // loop over the smaller set 166 if len(s.m) <= len(other.m) { 167 for val := range s.m { 168 if _, ok := other.m[val]; ok { 169 res.m[val] = struct{}{} 170 } 171 } 172 } else { 173 for val := range other.m { 174 if _, ok := s.m[val]; ok { 175 res.m[val] = struct{}{} 176 } 177 } 178 } 179 return res 180 } 181 182 // IntersectSlice is similar to Intersect, but takes a slice as parameter. 183 func (s Uint32) IntersectSlice(other []uint32) Uint32 { 184 res := NewUint32WithSize(min(s.Size(), len(other))) 185 186 for _, val := range other { 187 if _, ok := s.m[val]; ok { 188 res.m[val] = struct{}{} 189 } 190 } 191 return res 192 } 193 194 // Union returns new Uint32 about values either in the set or the other set. 195 func (s Uint32) Union(other Uint32) Uint32 { 196 res := NewUint32WithSize(s.Size() + other.Size()) 197 198 for val := range s.m { 199 res.m[val] = struct{}{} 200 } 201 for val := range other.m { 202 res.m[val] = struct{}{} 203 } 204 return res 205 } 206 207 // UnionSlice is similar to Union, but takes a slice as parameter. 208 func (s Uint32) UnionSlice(other []uint32) Uint32 { 209 res := NewUint32WithSize(s.Size() + len(other)) 210 211 for val := range s.m { 212 res.m[val] = struct{}{} 213 } 214 for _, val := range other { 215 res.m[val] = struct{}{} 216 } 217 return res 218 } 219 220 // Slice converts set into uint32 slice. 221 func (s Uint32) Slice() []uint32 { 222 res := make([]uint32, 0, len(s.m)) 223 224 for val := range s.m { 225 res = append(res, val) 226 } 227 return res 228 } 229 230 // Map converts set into map[uint32]bool. 231 func (s Uint32) Map() map[uint32]bool { 232 res := make(map[uint32]bool, len(s.m)) 233 234 for val := range s.m { 235 res[val] = true 236 } 237 return res 238 } 239 240 // MarshalJSON implements json.Marshaler interface, the set will be 241 // marshaled as an uint32 array. 242 func (s Uint32) MarshalJSON() ([]byte, error) { 243 res := s.Slice() 244 return json.Marshal(res) 245 } 246 247 // UnmarshalJSON implements json.Unmarshaler interface, it will unmarshal 248 // an uint32 array to the set. 249 func (s *Uint32) UnmarshalJSON(b []byte) error { 250 vals := make([]uint32, 0) 251 err := json.Unmarshal(b, &vals) 252 if err == nil { 253 s.Add(vals...) 254 } 255 return err 256 } 257 258 // MarshalYAML implements yaml.Marshaler interface of the yaml package, 259 // the set will be marshaled as an uint32 array. 260 func (s Uint32) MarshalYAML() (interface{}, error) { 261 res := s.Slice() 262 return res, nil 263 } 264 265 // UnmarshalYAML implements yaml.Unmarshaler interface of the yaml package, 266 // it will unmarshal an uint32 array to the set. 267 func (s *Uint32) UnmarshalYAML(unmarshal func(interface{}) error) error { 268 vals := make([]uint32, 0) 269 err := unmarshal(&vals) 270 if err == nil { 271 s.Add(vals...) 272 } 273 return err 274 }