gset

    同时,gset支持可选的并发安全参数选项,支持并发安全的场景。

    使用场景

    集合操作。

    使用方式

    接口文档https://godoc.org/github.com/gogf/gf/container/gset

    1. package main
    2. import (
    3. "github.com/gogf/gf/container/gset"
    4. "fmt"
    5. )
    6. func main() {
    7. // 创建一个并发安全的集合对象
    8. s := gset.New(true)
    9. // 添加数据项
    10. s.Add(1)
    11. // 批量添加数据项
    12. s.Add([]interface{}{1, 2, 3}...)
    13. // 集合数据项大小
    14. fmt.Println(s.Size())
    15. // 集合中是否存在指定数据项
    16. fmt.Println(s.Contains(2))
    17. // 返回数据项slice
    18. fmt.Println(s.Slice())
    19. // 删除数据项
    20. s.Remove(3)
    21. // 遍历数据项
    22. s.Iterator(func(v interface{}) bool {
    23. fmt.Println("Iterator:", v)
    24. return true
    25. })
    26. // 将集合转换为字符串
    27. fmt.Println(s.String())
    28. // 并发安全写锁操作
    29. s.LockFunc(func(m map[interface{}]struct{}) {
    30. m[4] = struct{}{}
    31. })
    32. // 并发安全读锁操作
    33. s.RLockFunc(func(m map[interface{}]struct{}) {
    34. fmt.Println(m)
    35. })
    36. // 清空集合
    37. s.Clear()
    38. fmt.Println(s.Size())
    39. }
    1. 3
    2. true
    3. [1 2 3]
    4. Iterator: 1
    5. Iterator: 2
    6. [1 2]
    7. map[1:{} 2:{} 4:{}]
    8. 0

    交差并补集

    我们可以使用以下方法实现交差并补集,并返回一个新的结果集合,

    1. func (set *Set) Intersect(others ...*Set) (newSet *Set)
    2. func (set *Set) Diff(others ...*Set) (newSet *Set)
    3. func (set *Set) Union(others ...*Set) (newSet *Set)
    4. func (set *Set) Complement(full *Set) (newSet *Set)
    1. Intersect: 交集,属于set且属于others的元素为元素的集合。
    2. Diff: 差集,属于set且不属于others的元素为元素的集合。
    3. Union: 并集,属于set或属于others的元素为元素的集合。
    4. Complement: 补集,(前提: set应当为full的子集)属于全集full不属于集合set的元素组成的集合。如果给定的full集合不是set的全集时,返回full与set的差集.

    通过集合方法我们可以发现,交差并集方法支持多个集合参数进行计算。以下为简化示例,只使用一个参数集合。

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/frame/g"
    5. "github.com/gogf/gf/container/gset"
    6. )
    7. s1 := gset.NewFrom(g.Slice{1, 2, 3})
    8. s2 := gset.NewFrom(g.Slice{4, 5, 6})
    9. s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
    10. // 交集
    11. // 差集
    12. fmt.Println(s3.Diff(s1).Slice())
    13. // 并集
    14. fmt.Println(s1.Union(s2).Slice())
    15. // 补集
    16. fmt.Println(s1.Complement(s3).Slice())
    17. }

    执行后,输出结果为:

    Contains/ContainsI包含判断

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/container/gset"
    5. )
    6. func main() {
    7. var set gset.StrSet
    8. set.Add("a")
    9. fmt.Println(set.Contains("a"))
    10. fmt.Println(set.Contains("A"))
    11. fmt.Println(set.ContainsI("A"))
    12. // Output:
    13. // true
    14. // false
    15. // true
    16. }
    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/container/gset"
    5. )
    6. func main() {
    7. var set gset.Set
    8. set.Add(1, 2, 3, 4)
    9. fmt.Println(set.Pop())
    10. fmt.Println(set.Pops(2))
    11. fmt.Println(set.Size())
    12. // May Output:
    13. // 1
    14. // [2 3]
    15. // 1
    16. }

    Join集合项串连

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/container/gset"
    5. )
    6. func main() {
    7. var set gset.Set
    8. set.Add("a", "b", "c", "d")
    9. fmt.Println(set.Join(","))
    10. // May Output:
    11. // a,b,c,d
    12. }

    IsSubsetOf子集判断

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/container/gset"
    5. "github.com/gogf/gf/frame/g"
    6. )
    7. func main() {
    8. var s1, s2 gset.Set
    9. s1.Add(g.Slice{1, 2, 3}...)
    10. s2.Add(g.Slice{2, 3}...)
    11. fmt.Println(s1.IsSubsetOf(&s2))
    12. fmt.Println(s2.IsSubsetOf(&s1))
    13. // Output:
    14. // false
    15. // true
    16. }

    判断性写入是指当指定的数据项不存在时则写入并且方法返回true,否则忽略吸入并且方法返回false。相关方法如下:

    • AddIfNotExist
    • AddIfNotExistFunc

    方法具体描述请查看接口文档或源码注释。

    遍历修改

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/container/gset"
    5. "github.com/gogf/gf/frame/g"
    6. )
    7. func main() {
    8. var (
    9. set gset.StrSet
    10. names = g.SliceStr{"user", "user_detail"}
    11. prefix = "gf_"
    12. )
    13. set.Add(names...)
    14. // Add prefix for given table names.
    15. set.Walk(func(item string) string {
    16. return prefix + item
    17. })
    18. fmt.Println(set.Slice())
    19. // May Output:
    20. // [gf_user gf_user_detail]
    21. }

    JSON序列化/反序列

    1. Marshal

      1. package main
      2. import (
      3. "encoding/json"
      4. "fmt"
      5. "github.com/gogf/gf/container/gset"
      6. )
      7. func main() {
      8. type Student struct {
      9. Id int
      10. Name string
      11. Scores *gset.IntSet
      12. }
      13. s := Student{
      14. Id: 1,
      15. Name: "john",
      16. Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
      17. }
      18. b, _ := json.Marshal(s)
      19. fmt.Println(string(b))
      20. }

      执行后,输出结果:

      1. {"Id":1,"Name":"john","Scores":[100,99,98]}
    2. Unmarshal

      1. package main
      2. import (
      3. "encoding/json"
      4. "fmt"
      5. "github.com/gogf/gf/container/gset"
      6. )
      7. func main() {
      8. b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
      9. type Student struct {
      10. Id int
      11. Name string
      12. Scores *gset.IntSet
      13. }
      14. s := Student{}
      15. json.Unmarshal(b, &s)
      16. fmt.Println(s)
      17. }

      执行后,输出结果:

    性能测试

    https://github.com/gogf/gf/blob/master/container/gset/gset_z_bench_test.go

    1. goos: linux
    2. goarch: amd64
    3. Benchmark_IntSet_Add-4 10000000 277 ns/op 8 B/op 0 allocs/op
    4. Benchmark_IntSet_Contains-4 20000000 60.6 ns/op 0 B/op 0 allocs/op
    5. Benchmark_IntSet_Remove-4 10000000 211 ns/op 0 B/op 0 allocs/op
    6. Benchmark_AnySet_Add-4 5000000 312 ns/op 21 B/op 1 allocs/op
    7. Benchmark_AnySet_Contains-4 30000000 68.2 ns/op 0 B/op 0 allocs/op
    8. Benchmark_AnySet_Remove-4 5000000 267 ns/op 0 B/op 0 allocs/op
    9. Benchmark_StrSet_Add-4 5000000 383 ns/op 20 B/op 1 allocs/op
    10. Benchmark_StrSet_Contains-4 10000000 160 ns/op 7 B/op 0 allocs/op
    11. Benchmark_StrSet_Remove-4 5000000 306 ns/op 7 B/op 0 allocs/op
    12. Benchmark_Unsafe_IntSet_Add-4 10000000 258 ns/op 35 B/op 0 allocs/op
    13. Benchmark_Unsafe_IntSet_Contains-4 20000000 146 ns/op 0 B/op 0 allocs/op
    14. Benchmark_Unsafe_IntSet_Remove-4 10000000 173 ns/op 0 B/op 0 allocs/op
    15. Benchmark_Unsafe_AnySet_Add-4 5000000 355 ns/op 41 B/op 1 allocs/op
    16. Benchmark_Unsafe_AnySet_Contains-4 10000000 150 ns/op 0 B/op 0 allocs/op
    17. Benchmark_Unsafe_AnySet_Remove-4 200000000 11.9 ns/op 0 B/op 0 allocs/op
    18. Benchmark_Unsafe_StrSet_Add-4 5000000 486 ns/op 59 B/op 1 allocs/op