mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 19:27:46 +08:00
edit review
This commit is contained in:
parent
5e34aee2d7
commit
f2bb9d65c3
@ -11,10 +11,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gogf/gf/v2/container/gset"
|
"github.com/gogf/gf/v2/container/gset"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewStrSet create and returns a new set, which contains un-repeated items.
|
// NewStrSet create and returns a new set, which contains un-repeated items.
|
||||||
//The parameter <safe> is used to specify whether using set in concurrent-safety,
|
// The parameter `safe` is used to specify whether using set in concurrent-safety,
|
||||||
// which is false in default.
|
// which is false in default.
|
||||||
func ExampleStrSet_NewStrSet() {
|
func ExampleStrSet_NewStrSet() {
|
||||||
strSet := gset.NewStrSet(true)
|
strSet := gset.NewStrSet(true)
|
||||||
@ -27,7 +28,7 @@ func ExampleStrSet_NewStrSet() {
|
|||||||
//Iterator str3
|
//Iterator str3
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewStrSetFrom returns a new set from <items>.
|
// NewStrSetFrom returns a new set from `items`.
|
||||||
func ExampleStrSet_NewStrSetFrom() {
|
func ExampleStrSet_NewStrSetFrom() {
|
||||||
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
fmt.Println(strSet.Slice())
|
fmt.Println(strSet.Slice())
|
||||||
@ -38,68 +39,79 @@ func ExampleStrSet_NewStrSetFrom() {
|
|||||||
|
|
||||||
// Add adds one or multiple items to the set.
|
// Add adds one or multiple items to the set.
|
||||||
func ExampleStrSet_Add() {
|
func ExampleStrSet_Add() {
|
||||||
var strSet gset.StrSet
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
strSet.Add([]string{"str1", "str2", "str3"}...)
|
strSet.Add("str")
|
||||||
|
fmt.Println(strSet.Slice())
|
||||||
|
fmt.Println(strSet.AddIfNotExist("str"))
|
||||||
|
|
||||||
// Mya Output:
|
// Mya Output:
|
||||||
//Iterator str1
|
// [str str1 str2 str3]
|
||||||
//Iterator str2
|
// false
|
||||||
//Iterator str3
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddIfNotExist checks whether item exists in the set,
|
// AddIfNotExist checks whether item exists in the set,
|
||||||
// it adds the item to set and returns true if it does not exists in the set,
|
// it adds the item to set and returns true if it does not exists in the set,
|
||||||
// or else it does nothing and returns false.
|
// or else it does nothing and returns false.
|
||||||
func ExampleStrSet_AddIfNotExist() {
|
func ExampleStrSet_AddIfNotExist() {
|
||||||
var strSet gset.StrSet
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
|
strSet.Add("str")
|
||||||
|
fmt.Println(strSet.Slice())
|
||||||
fmt.Println(strSet.AddIfNotExist("str"))
|
fmt.Println(strSet.AddIfNotExist("str"))
|
||||||
|
|
||||||
// Output:
|
// Mya Output:
|
||||||
// true
|
// [str str1 str2 str3]
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddIfNotExistFunc checks whether item exists in the set,
|
// AddIfNotExistFunc checks whether item exists in the set,
|
||||||
//it adds the item to set and returns true if it does not exists in the set and function <f> returns true,
|
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
|
||||||
// or else it does nothing and returns false.
|
// or else it does nothing and returns false.
|
||||||
//Note that, the function <f> is executed without writing lock.
|
// Note that, the function `f` is executed without writing lock.
|
||||||
func ExampleStrSet_AddIfNotExistFunc() {
|
func ExampleStrSet_AddIfNotExistFunc() {
|
||||||
var strSet gset.StrSet
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
fmt.Println(strSet.AddIfNotExistFunc("str", func() bool {
|
strSet.Add("str")
|
||||||
|
fmt.Println(strSet.Slice())
|
||||||
|
fmt.Println(strSet.AddIfNotExistFunc("str5", func() bool {
|
||||||
return true
|
return true
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Output:
|
// May Output:
|
||||||
|
// [str1 str2 str3 str]
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddIfNotExistFunc checks whether item exists in the set,
|
// AddIfNotExistFunc checks whether item exists in the set,
|
||||||
//it adds the item to set and returns true if it does not exists in the set and function <f> returns true,
|
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
|
||||||
// or else it does nothing and returns false.
|
// or else it does nothing and returns false.
|
||||||
//Note that, the function <f> is executed without writing lock.
|
// Note that, the function `f` is executed without writing lock.
|
||||||
func ExampleStrSet_AddIfNotExistFuncLock() {
|
func ExampleStrSet_AddIfNotExistFuncLock() {
|
||||||
var strSet gset.StrSet
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
fmt.Println(strSet.AddIfNotExistFuncLock("str", func() bool {
|
strSet.Add("str")
|
||||||
|
fmt.Println(strSet.Slice())
|
||||||
|
fmt.Println(strSet.AddIfNotExistFuncLock("str4", func() bool {
|
||||||
return true
|
return true
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Output:
|
// May Output:
|
||||||
|
// [str1 str2 str3 str]
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear deletes all items of the set.
|
// Clear deletes all items of the set.
|
||||||
func ExampleStrSet_Clear() {
|
func ExampleStrSet_Clear() {
|
||||||
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
|
fmt.Println(strSet.Size())
|
||||||
strSet.Clear()
|
strSet.Clear()
|
||||||
|
|
||||||
fmt.Println(strSet.Size())
|
fmt.Println(strSet.Size())
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
|
// 3
|
||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//Complement returns a new set which is the complement from <set> to <full>.
|
// Complement returns a new set which is the complement from `set` to `full`.
|
||||||
//Which means, all the items in <newSet> are in <full> and not in <set>.
|
// Which means, all the items in `newSet` are in `full` and not in `set`.
|
||||||
//It returns the difference between <full> and <set> if the given set <full> is not the full set of <set>.
|
// It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
|
||||||
func ExampleStrSet_Complement() {
|
func ExampleStrSet_Complement() {
|
||||||
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
|
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
|
||||||
s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
|
||||||
@ -109,7 +121,7 @@ func ExampleStrSet_Complement() {
|
|||||||
// [str4 str5]
|
// [str4 str5]
|
||||||
}
|
}
|
||||||
|
|
||||||
//Contains checks whether the set contains <item>.
|
// Contains checks whether the set contains `item`.
|
||||||
func ExampleStrSet_Contains() {
|
func ExampleStrSet_Contains() {
|
||||||
var set gset.StrSet
|
var set gset.StrSet
|
||||||
set.Add("a")
|
set.Add("a")
|
||||||
@ -134,12 +146,11 @@ func ExampleStrSet_ContainsI() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
//Diff returns a new set which is the difference set from <set> to <other>.
|
// Diff returns a new set which is the difference set from `set` to `other`.
|
||||||
//Which means, all the items in <newSet> are in <set> but not in <other>.
|
// Which means, all the items in `newSet` are in `set` but not in `other`.
|
||||||
func ExampleStrSet_Diff() {
|
func ExampleStrSet_Diff() {
|
||||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||||
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
|
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
|
||||||
// 差集
|
|
||||||
fmt.Println(s2.Diff(s1).Slice())
|
fmt.Println(s2.Diff(s1).Slice())
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
@ -148,31 +159,33 @@ func ExampleStrSet_Diff() {
|
|||||||
|
|
||||||
// Equal checks whether the two sets equal.
|
// Equal checks whether the two sets equal.
|
||||||
func ExampleStrSet_Equal() {
|
func ExampleStrSet_Equal() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||||
s1.Add([]string{"a", "b", "c"}...)
|
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
|
||||||
var s2 gset.StrSet
|
|
||||||
s2.Add([]string{"a", "b", "c", "d"}...)
|
|
||||||
fmt.Println(s2.Equal(s1))
|
fmt.Println(s2.Equal(s1))
|
||||||
|
|
||||||
|
s3 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||||
|
s4 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||||
|
fmt.Println(s3.Equal(s4))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
//Intersect returns a new set which is the intersection from <set> to <other>.
|
// Intersect returns a new set which is the intersection from `set` to `other`.
|
||||||
//Which means, all the items in <newSet> are in <set> and also in <other>.
|
// Which means, all the items in `newSet` are in `set` and also in `other`.
|
||||||
func ExampleStrSet_Intersect() {
|
func ExampleStrSet_Intersect() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c"}...)
|
s1.Add([]string{"a", "b", "c"}...)
|
||||||
var s2 gset.StrSet
|
var s2 gset.StrSet
|
||||||
s2.Add([]string{"a", "b", "c", "d"}...)
|
s2.Add([]string{"a", "b", "c", "d"}...)
|
||||||
// 交集
|
|
||||||
fmt.Println(s2.Intersect(s1).Slice())
|
fmt.Println(s2.Intersect(s1).Slice())
|
||||||
|
|
||||||
// May Output:
|
// May Output:
|
||||||
// [c a b]
|
// [c a b]
|
||||||
}
|
}
|
||||||
|
|
||||||
//IsSubsetOf checks whether the current set is a sub-set of <other>.
|
// IsSubsetOf checks whether the current set is a sub-set of `other`
|
||||||
func ExampleStrSet_IsSubsetOf() {
|
func ExampleStrSet_IsSubsetOf() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -184,8 +197,8 @@ func ExampleStrSet_IsSubsetOf() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
//Iterator iterates the set readonly with given callback function <f>,
|
// Iterator iterates the set readonly with given callback function `f`,
|
||||||
//if <f> returns true then continue iterating; or false to stop.
|
// if `f` returns true then continue iterating; or false to stop.
|
||||||
func ExampleStrSet_Iterator() {
|
func ExampleStrSet_Iterator() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -200,7 +213,7 @@ func ExampleStrSet_Iterator() {
|
|||||||
// Iterator d
|
// Iterator d
|
||||||
}
|
}
|
||||||
|
|
||||||
//Join joins items with a string <glue>.
|
// Join joins items with a string `glue`.
|
||||||
func ExampleStrSet_Join() {
|
func ExampleStrSet_Join() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -210,13 +223,35 @@ func ExampleStrSet_Join() {
|
|||||||
// b,c,d,a
|
// b,c,d,a
|
||||||
}
|
}
|
||||||
|
|
||||||
//LockFunc locks writing with callback function <f>.
|
// LockFunc locks writing with callback function `f`.
|
||||||
func ExampleStrSet_LockFunc() {
|
func ExampleStrSet_LockFunc() {
|
||||||
s := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"1", "2"}...)
|
||||||
s.LockFunc(func(m map[string]struct{}) {
|
go func() {
|
||||||
m["a"] = struct{}{}
|
s1.LockFunc(func(m map[string]struct{}) {
|
||||||
|
m["3"] = struct{}{}
|
||||||
})
|
})
|
||||||
|
fmt.Println("one:", s1.Slice())
|
||||||
|
}()
|
||||||
|
time.Sleep(time.Duration(2) * time.Second)
|
||||||
|
go func() {
|
||||||
|
s1.LockFunc(func(m map[string]struct{}) {
|
||||||
|
m["4"] = struct{}{}
|
||||||
|
})
|
||||||
|
fmt.Println("two:", s1.Slice())
|
||||||
|
}()
|
||||||
|
time.Sleep(time.Duration(2) * time.Second)
|
||||||
|
go func() {
|
||||||
|
s1.LockFunc(func(m map[string]struct{}) {
|
||||||
|
m["5"] = struct{}{}
|
||||||
|
})
|
||||||
|
fmt.Println("three:", s1.Slice())
|
||||||
|
}()
|
||||||
|
time.Sleep(time.Duration(2) * time.Second)
|
||||||
|
// May Output
|
||||||
|
// [2 3 1]
|
||||||
|
// [1 2 3 4]
|
||||||
|
// [1 2 3 4 5]
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||||
@ -238,7 +273,7 @@ func ExampleStrSet_MarshalJSON() {
|
|||||||
// {"Id":1,"Name":"john","Scores":["100","99","98"]}
|
// {"Id":1,"Name":"john","Scores":["100","99","98"]}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Merge adds items from <others> sets into <set>.
|
// Merge adds items from `others` sets into `set`.
|
||||||
func ExampleStrSet_Merge() {
|
func ExampleStrSet_Merge() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -261,7 +296,7 @@ func ExampleStrSet_Pop() {
|
|||||||
// a
|
// a
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pops randomly pops <size> items from set.
|
// Pops randomly pops `size` items from set.
|
||||||
// It returns all items if size == -1.
|
// It returns all items if size == -1.
|
||||||
func ExampleStrSet_Pops() {
|
func ExampleStrSet_Pops() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
@ -275,7 +310,7 @@ func ExampleStrSet_Pops() {
|
|||||||
// b
|
// b
|
||||||
}
|
}
|
||||||
|
|
||||||
//RLockFunc locks reading with callback function <f>.
|
// RLockFunc locks reading with callback function `f`.
|
||||||
func ExampleStrSet_RLockFunc() {
|
func ExampleStrSet_RLockFunc() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -287,7 +322,7 @@ func ExampleStrSet_RLockFunc() {
|
|||||||
// map[a:{} b:{} c:{} d:{}]
|
// map[a:{} b:{} c:{} d:{}]
|
||||||
}
|
}
|
||||||
|
|
||||||
//Remove deletes <item> from set.
|
// Remove deletes `item` from set.
|
||||||
func ExampleStrSet_Remove() {
|
func ExampleStrSet_Remove() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -339,8 +374,8 @@ func ExampleStrSet_Sum() {
|
|||||||
// 0
|
// 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//Union returns a new set which is the union of <set> and <other>.
|
// Union returns a new set which is the union of `set` and `other`.
|
||||||
//Which means, all the items in <newSet> are in <set> or in <other>.
|
// Which means, all the items in `newSet` are in `set` or in `other`.
|
||||||
func ExampleStrSet_Union() {
|
func ExampleStrSet_Union() {
|
||||||
s1 := gset.NewStrSet(true)
|
s1 := gset.NewStrSet(true)
|
||||||
s1.Add([]string{"a", "b", "c", "d"}...)
|
s1.Add([]string{"a", "b", "c", "d"}...)
|
||||||
@ -370,15 +405,21 @@ func ExampleStrSet_UnmarshalJSON() {
|
|||||||
|
|
||||||
// UnmarshalValue is an interface implement which sets any type of value for set.
|
// UnmarshalValue is an interface implement which sets any type of value for set.
|
||||||
func ExampleStrSet_UnmarshalValue() {
|
func ExampleStrSet_UnmarshalValue() {
|
||||||
s := gset.NewStrSetFrom([]string{"a"}, true)
|
b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
|
||||||
s.UnmarshalValue([]string{"b", "c"})
|
type Student struct {
|
||||||
fmt.Println(s.Slice())
|
Id int
|
||||||
|
Name string
|
||||||
|
Scores *gset.StrSet
|
||||||
|
}
|
||||||
|
s := Student{}
|
||||||
|
json.Unmarshal(b, &s)
|
||||||
|
fmt.Println(s)
|
||||||
|
|
||||||
// Output:
|
// May Output:
|
||||||
// [a b c]
|
// {1 john "99","98","100"}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Walk applies a user supplied function <f> to every item of set.
|
// Walk applies a user supplied function `f` to every item of set.
|
||||||
func ExampleStrSet_Walk() {
|
func ExampleStrSet_Walk() {
|
||||||
var (
|
var (
|
||||||
set gset.StrSet
|
set gset.StrSet
|
||||||
|
Loading…
Reference in New Issue
Block a user