github.com/wangyougui/gf/v2@v2.6.5/container/gset/gset_z_example_int_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gset_test 8 9 import ( 10 "encoding/json" 11 "fmt" 12 13 "github.com/wangyougui/gf/v2/container/gset" 14 "github.com/wangyougui/gf/v2/frame/g" 15 ) 16 17 // New create and returns a new set, which contains un-repeated items. 18 // The parameter `safe` is used to specify whether using set in concurrent-safety, 19 // which is false in default. 20 func ExampleNewIntSet() { 21 intSet := gset.NewIntSet() 22 intSet.Add([]int{1, 2, 3}...) 23 fmt.Println(intSet.Slice()) 24 25 // May Output: 26 // [2 1 3] 27 } 28 29 // NewIntSetFrom returns a new set from `items`. 30 func ExampleNewFrom() { 31 intSet := gset.NewIntSetFrom([]int{1, 2, 3}) 32 fmt.Println(intSet.Slice()) 33 34 // May Output: 35 // [2 1 3] 36 } 37 38 // Add adds one or multiple items to the set. 39 func ExampleIntSet_Add() { 40 intSet := gset.NewIntSetFrom([]int{1, 2, 3}) 41 intSet.Add(1) 42 fmt.Println(intSet.Slice()) 43 fmt.Println(intSet.AddIfNotExist(1)) 44 45 // May Output: 46 // [1 2 3] 47 // false 48 } 49 50 // AddIfNotExist checks whether item exists in the set, 51 // it adds the item to set and returns true if it does not exists in the set, 52 // or else it does nothing and returns false. 53 func ExampleIntSet_AddIfNotExist() { 54 intSet := gset.NewIntSetFrom([]int{1, 2, 3}) 55 intSet.Add(1) 56 fmt.Println(intSet.Slice()) 57 fmt.Println(intSet.AddIfNotExist(1)) 58 59 // May Output: 60 // [1 2 3] 61 // false 62 } 63 64 // AddIfNotExistFunc checks whether item exists in the set, 65 // it adds the item to set and returns true if it does not exists in the set and function `f` returns true, 66 // or else it does nothing and returns false. 67 // Note that, the function `f` is executed without writing lock. 68 func ExampleIntSet_AddIfNotExistFunc() { 69 intSet := gset.NewIntSetFrom([]int{1, 2, 3}) 70 intSet.Add(1) 71 fmt.Println(intSet.Slice()) 72 fmt.Println(intSet.AddIfNotExistFunc(5, func() bool { 73 return true 74 })) 75 76 // May Output: 77 // [1 2 3] 78 // true 79 } 80 81 // AddIfNotExistFunc checks whether item exists in the set, 82 // it adds the item to set and returns true if it does not exists in the set and function `f` returns true, 83 // or else it does nothing and returns false. 84 // Note that, the function `f` is executed without writing lock. 85 func ExampleIntSet_AddIfNotExistFuncLock() { 86 intSet := gset.NewIntSetFrom([]int{1, 2, 3}) 87 intSet.Add(1) 88 fmt.Println(intSet.Slice()) 89 fmt.Println(intSet.AddIfNotExistFuncLock(4, func() bool { 90 return true 91 })) 92 93 // May Output: 94 // [1 2 3] 95 // true 96 } 97 98 // Clear deletes all items of the set. 99 func ExampleIntSet_Clear() { 100 intSet := gset.NewIntSetFrom([]int{1, 2, 3}) 101 fmt.Println(intSet.Size()) 102 intSet.Clear() 103 fmt.Println(intSet.Size()) 104 105 // Output: 106 // 3 107 // 0 108 } 109 110 // Complement returns a new set which is the complement from `set` to `full`. 111 // Which means, all the items in `newSet` are in `full` and not in `set`. 112 // It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`. 113 func ExampleIntSet_Complement() { 114 intSet := gset.NewIntSetFrom([]int{1, 2, 3, 4, 5}) 115 s := gset.NewIntSetFrom([]int{1, 2, 3}) 116 fmt.Println(s.Complement(intSet).Slice()) 117 118 // May Output: 119 // [4 5] 120 } 121 122 // Contains checks whether the set contains `item`. 123 func ExampleIntSet_Contains() { 124 var set1 gset.IntSet 125 set1.Add(1, 4, 5, 6, 7) 126 fmt.Println(set1.Contains(1)) 127 128 var set2 gset.IntSet 129 set2.Add(1, 4, 5, 6, 7) 130 fmt.Println(set2.Contains(8)) 131 132 // Output: 133 // true 134 // false 135 } 136 137 // Diff returns a new set which is the difference set from `set` to `other`. 138 // Which means, all the items in `newSet` are in `set` but not in `other`. 139 func ExampleIntSet_Diff() { 140 s1 := gset.NewIntSetFrom([]int{1, 2, 3}) 141 s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4}) 142 fmt.Println(s2.Diff(s1).Slice()) 143 144 // Output: 145 // [4] 146 } 147 148 // Equal checks whether the two sets equal. 149 func ExampleIntSet_Equal() { 150 s1 := gset.NewIntSetFrom([]int{1, 2, 3}) 151 s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4}) 152 fmt.Println(s2.Equal(s1)) 153 154 s3 := gset.NewIntSetFrom([]int{1, 2, 3}) 155 s4 := gset.NewIntSetFrom([]int{1, 2, 3}) 156 fmt.Println(s3.Equal(s4)) 157 158 // Output: 159 // false 160 // true 161 } 162 163 // Intersect returns a new set which is the intersection from `set` to `other`. 164 // Which means, all the items in `newSet` are in `set` and also in `other`. 165 func ExampleIntSet_Intersect() { 166 s1 := gset.NewIntSet() 167 s1.Add([]int{1, 2, 3}...) 168 var s2 gset.IntSet 169 s2.Add([]int{1, 2, 3, 4}...) 170 fmt.Println(s2.Intersect(s1).Slice()) 171 172 // May Output: 173 // [1 2 3] 174 } 175 176 // IsSubsetOf checks whether the current set is a sub-set of `other` 177 func ExampleIntSet_IsSubsetOf() { 178 s1 := gset.NewIntSet() 179 s1.Add([]int{1, 2, 3, 4}...) 180 var s2 gset.IntSet 181 s2.Add([]int{1, 2, 4}...) 182 fmt.Println(s2.IsSubsetOf(s1)) 183 184 // Output: 185 // true 186 } 187 188 // Iterator iterates the set readonly with given callback function `f`, 189 // if `f` returns true then continue iterating; or false to stop. 190 func ExampleIntSet_Iterator() { 191 s1 := gset.NewIntSet() 192 s1.Add([]int{1, 2, 3, 4}...) 193 s1.Iterator(func(v int) bool { 194 fmt.Println("Iterator", v) 195 return true 196 }) 197 // May Output: 198 // Iterator 2 199 // Iterator 3 200 // Iterator 1 201 // Iterator 4 202 } 203 204 // Join joins items with a string `glue`. 205 func ExampleIntSet_Join() { 206 s1 := gset.NewIntSet() 207 s1.Add([]int{1, 2, 3, 4}...) 208 fmt.Println(s1.Join(",")) 209 210 // May Output: 211 // 3,4,1,2 212 } 213 214 // LockFunc locks writing with callback function `f`. 215 func ExampleIntSet_LockFunc() { 216 s1 := gset.NewIntSet() 217 s1.Add([]int{1, 2}...) 218 s1.LockFunc(func(m map[int]struct{}) { 219 m[3] = struct{}{} 220 }) 221 fmt.Println(s1.Slice()) 222 223 // May Output 224 // [2 3 1] 225 } 226 227 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 228 func ExampleIntSet_MarshalJSON() { 229 type Student struct { 230 Id int 231 Name string 232 Scores *gset.IntSet 233 } 234 s := Student{ 235 Id: 1, 236 Name: "john", 237 Scores: gset.NewIntSetFrom([]int{100, 99, 98}), 238 } 239 b, _ := json.Marshal(s) 240 fmt.Println(string(b)) 241 242 // May Output: 243 // {"Id":1,"Name":"john","Scores":[100,99,98]} 244 } 245 246 // Merge adds items from `others` sets into `set`. 247 func ExampleIntSet_Merge() { 248 s1 := gset.NewIntSet() 249 s1.Add([]int{1, 2, 3, 4}...) 250 251 s2 := gset.NewIntSet() 252 fmt.Println(s1.Merge(s2).Slice()) 253 254 // May Output: 255 // [1 2 3 4] 256 } 257 258 // Pops randomly pops an item from set. 259 func ExampleIntSet_Pop() { 260 s1 := gset.NewIntSet() 261 s1.Add([]int{1, 2, 3, 4}...) 262 263 fmt.Println(s1.Pop()) 264 265 // May Output: 266 // 1 267 } 268 269 // Pops randomly pops `size` items from set. 270 // It returns all items if size == -1. 271 func ExampleIntSet_Pops() { 272 s1 := gset.NewIntSet() 273 s1.Add([]int{1, 2, 3, 4}...) 274 for _, v := range s1.Pops(2) { 275 fmt.Println(v) 276 } 277 278 // May Output: 279 // 1 280 // 2 281 } 282 283 // RLockFunc locks reading with callback function `f`. 284 func ExampleIntSet_RLockFunc() { 285 s1 := gset.NewIntSet() 286 s1.Add([]int{1, 2, 3, 4}...) 287 s1.RLockFunc(func(m map[int]struct{}) { 288 fmt.Println(m) 289 }) 290 291 // Output: 292 // map[1:{} 2:{} 3:{} 4:{}] 293 } 294 295 // Remove deletes `item` from set. 296 func ExampleIntSet_Remove() { 297 s1 := gset.NewIntSet() 298 s1.Add([]int{1, 2, 3, 4}...) 299 s1.Remove(1) 300 fmt.Println(s1.Slice()) 301 302 // May Output: 303 // [3 4 2] 304 } 305 306 // Size returns the size of the set. 307 func ExampleIntSet_Size() { 308 s1 := gset.NewIntSet() 309 s1.Add([]int{1, 2, 3, 4}...) 310 fmt.Println(s1.Size()) 311 312 // Output: 313 // 4 314 } 315 316 // Slice returns the an of items of the set as slice. 317 func ExampleIntSet_Slice() { 318 s1 := gset.NewIntSet() 319 s1.Add([]int{1, 2, 3, 4}...) 320 fmt.Println(s1.Slice()) 321 322 // May Output: 323 // [1, 2, 3, 4] 324 } 325 326 // String returns items as a string, which implements like json.Marshal does. 327 func ExampleIntSet_String() { 328 s1 := gset.NewIntSet() 329 s1.Add([]int{1, 2, 3, 4}...) 330 fmt.Println(s1.String()) 331 332 // May Output: 333 // [1,2,3,4] 334 } 335 336 // Sum sums items. Note: The items should be converted to int type, 337 // or you'd get a result that you unexpected. 338 func ExampleIntSet_Sum() { 339 s1 := gset.NewIntSet() 340 s1.Add([]int{1, 2, 3, 4}...) 341 fmt.Println(s1.Sum()) 342 343 // Output: 344 // 10 345 } 346 347 // Union returns a new set which is the union of `set` and `other`. 348 // Which means, all the items in `newSet` are in `set` or in `other`. 349 func ExampleIntSet_Union() { 350 s1 := gset.NewIntSet() 351 s1.Add([]int{1, 2, 3, 4}...) 352 s2 := gset.NewIntSet() 353 s2.Add([]int{1, 2, 4}...) 354 fmt.Println(s1.Union(s2).Slice()) 355 356 // May Output: 357 // [3 4 1 2] 358 } 359 360 // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. 361 func ExampleIntSet_UnmarshalJSON() { 362 b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`) 363 type Student struct { 364 Id int 365 Name string 366 Scores *gset.IntSet 367 } 368 s := Student{} 369 json.Unmarshal(b, &s) 370 fmt.Println(s) 371 372 // May Output: 373 // {1 john [100,99,98]} 374 } 375 376 // UnmarshalValue is an interface implement which sets any type of value for set. 377 func ExampleIntSet_UnmarshalValue() { 378 b := []byte(`{"Id":1,"Name":"john","Scores":100,99,98}`) 379 type Student struct { 380 Id int 381 Name string 382 Scores *gset.IntSet 383 } 384 s := Student{} 385 json.Unmarshal(b, &s) 386 fmt.Println(s) 387 388 // May Output: 389 // {1 john [100,99,98]} 390 } 391 392 // Walk applies a user supplied function `f` to every item of set. 393 func ExampleIntSet_Walk() { 394 var ( 395 set gset.IntSet 396 names = g.SliceInt{1, 0} 397 delta = 10 398 ) 399 set.Add(names...) 400 // Add prefix for given table names. 401 set.Walk(func(item int) int { 402 return delta + item 403 }) 404 fmt.Println(set.Slice()) 405 406 // May Output: 407 // [12 60] 408 }