edit review

This commit is contained in:
timmy.hu 2021-11-03 22:49:30 +08:00
parent 5e34aee2d7
commit f2bb9d65c3

View File

@ -11,11 +11,12 @@ import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
"time"
)
//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,
//which is false in default.
// 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,
// which is false in default.
func ExampleStrSet_NewStrSet() {
strSet := gset.NewStrSet(true)
strSet.Add([]string{"str1", "str2", "str3"}...)
@ -27,7 +28,7 @@ func ExampleStrSet_NewStrSet() {
//Iterator str3
}
//NewStrSetFrom returns a new set from <items>.
// NewStrSetFrom returns a new set from `items`.
func ExampleStrSet_NewStrSetFrom() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Slice())
@ -36,70 +37,81 @@ func ExampleStrSet_NewStrSetFrom() {
// [str1 str2 str3]
}
//Add adds one or multiple items to the set.
// Add adds one or multiple items to the set.
func ExampleStrSet_Add() {
var strSet gset.StrSet
strSet.Add([]string{"str1", "str2", "str3"}...)
// Mya Output:
//Iterator str1
//Iterator str2
//Iterator str3
}
//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,
//or else it does nothing and returns false.
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"))
// Output:
// true
// Mya Output:
// [str str1 str2 str3]
// false
}
//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,
//or else it does nothing and returns false.
//Note that, the function <f> is executed without writing lock.
// 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,
// or else it does nothing and returns false.
func ExampleStrSet_AddIfNotExist() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// Mya Output:
// [str str1 str2 str3]
// false
}
// 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,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleStrSet_AddIfNotExistFunc() {
var strSet gset.StrSet
fmt.Println(strSet.AddIfNotExistFunc("str", func() bool {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExistFunc("str5", func() bool {
return true
}))
// Output:
// May Output:
// [str1 str2 str3 str]
// true
}
//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,
//or else it does nothing and returns false.
//Note that, the function <f> is executed without writing lock.
// 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,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleStrSet_AddIfNotExistFuncLock() {
var strSet gset.StrSet
fmt.Println(strSet.AddIfNotExistFuncLock("str", func() bool {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExistFuncLock("str4", func() bool {
return true
}))
// Output:
// May Output:
// [str1 str2 str3 str]
// true
}
//Clear deletes all items of the set.
// Clear deletes all items of the set.
func ExampleStrSet_Clear() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Size())
strSet.Clear()
fmt.Println(strSet.Size())
// Output:
// 3
// 0
}
//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>.
//It returns the difference between <full> and <set> if the given set <full> is not the full set of <set>.
// 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`.
// It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
func ExampleStrSet_Complement() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
@ -109,7 +121,7 @@ func ExampleStrSet_Complement() {
// [str4 str5]
}
//Contains checks whether the set contains <item>.
// Contains checks whether the set contains `item`.
func ExampleStrSet_Contains() {
var set gset.StrSet
set.Add("a")
@ -121,8 +133,8 @@ func ExampleStrSet_Contains() {
// false
}
//ContainsI checks whether a value exists in the set with case-insensitively.
//Note that it internally iterates the whole set to do the comparison with case-insensitively.
// ContainsI checks whether a value exists in the set with case-insensitively.
// Note that it internally iterates the whole set to do the comparison with case-insensitively.
func ExampleStrSet_ContainsI() {
var set gset.StrSet
set.Add("a")
@ -134,45 +146,46 @@ func ExampleStrSet_ContainsI() {
// true
}
//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>.
// 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`.
func ExampleStrSet_Diff() {
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
// 差集
fmt.Println(s2.Diff(s1).Slice())
// Output:
// [d]
}
//Equal checks whether the two sets equal.
// Equal checks whether the two sets equal.
func ExampleStrSet_Equal() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c"}...)
var s2 gset.StrSet
s2.Add([]string{"a", "b", "c", "d"}...)
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
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:
// false
// true
}
//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>.
// 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`.
func ExampleStrSet_Intersect() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c"}...)
var s2 gset.StrSet
s2.Add([]string{"a", "b", "c", "d"}...)
// 交集
fmt.Println(s2.Intersect(s1).Slice())
// May Output:
// [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() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -184,8 +197,8 @@ func ExampleStrSet_IsSubsetOf() {
// true
}
//Iterator iterates the set readonly with given callback function <f>,
//if <f> returns true then continue iterating; or false to stop.
// Iterator iterates the set readonly with given callback function `f`,
// if `f` returns true then continue iterating; or false to stop.
func ExampleStrSet_Iterator() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -200,7 +213,7 @@ func ExampleStrSet_Iterator() {
// Iterator d
}
//Join joins items with a string <glue>.
// Join joins items with a string `glue`.
func ExampleStrSet_Join() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -210,16 +223,38 @@ func ExampleStrSet_Join() {
// b,c,d,a
}
//LockFunc locks writing with callback function <f>.
// LockFunc locks writing with callback function `f`.
func ExampleStrSet_LockFunc() {
s := gset.NewStrSet(true)
s.Add([]string{"a", "b", "c", "d"}...)
s.LockFunc(func(m map[string]struct{}) {
m["a"] = struct{}{}
})
s1 := gset.NewStrSet(true)
s1.Add([]string{"1", "2"}...)
go func() {
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.
func ExampleStrSet_MarshalJSON() {
type Student struct {
Id int
@ -238,7 +273,7 @@ func ExampleStrSet_MarshalJSON() {
// {"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() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -250,7 +285,7 @@ func ExampleStrSet_Merge() {
// [d a b c]
}
//Pops randomly pops an item from set.
// Pops randomly pops an item from set.
func ExampleStrSet_Pop() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -261,8 +296,8 @@ func ExampleStrSet_Pop() {
// a
}
//Pops randomly pops <size> items from set.
//It returns all items if size == -1.
// Pops randomly pops `size` items from set.
// It returns all items if size == -1.
func ExampleStrSet_Pops() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -275,7 +310,7 @@ func ExampleStrSet_Pops() {
// b
}
//RLockFunc locks reading with callback function <f>.
// RLockFunc locks reading with callback function `f`.
func ExampleStrSet_RLockFunc() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -287,7 +322,7 @@ func ExampleStrSet_RLockFunc() {
// map[a:{} b:{} c:{} d:{}]
}
//Remove deletes <item> from set.
// Remove deletes `item` from set.
func ExampleStrSet_Remove() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -298,7 +333,7 @@ func ExampleStrSet_Remove() {
// [b c d]
}
//Size returns the size of the set.
// Size returns the size of the set.
func ExampleStrSet_Size() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -308,7 +343,7 @@ func ExampleStrSet_Size() {
// 4
}
//Slice returns the a of items of the set as slice.
// Slice returns the a of items of the set as slice.
func ExampleStrSet_Slice() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -318,7 +353,7 @@ func ExampleStrSet_Slice() {
// [a,b,c,d]
}
//String returns items as a string, which implements like json.Marshal does.
// String returns items as a string, which implements like json.Marshal does.
func ExampleStrSet_String() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -328,8 +363,8 @@ func ExampleStrSet_String() {
// "a","b","c","d"
}
//Sum sums items. Note: The items should be converted to int type,
//or you'd get a result that you unexpected.
// Sum sums items. Note: The items should be converted to int type,
// or you'd get a result that you unexpected.
func ExampleStrSet_Sum() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -339,8 +374,8 @@ func ExampleStrSet_Sum() {
// 0
}
//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>.
// 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`.
func ExampleStrSet_Union() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -352,7 +387,7 @@ func ExampleStrSet_Union() {
// [a b c d]
}
//UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func ExampleStrSet_UnmarshalJSON() {
b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
type Student struct {
@ -368,17 +403,23 @@ func ExampleStrSet_UnmarshalJSON() {
// {1 john "99","98","100"}
}
//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() {
s := gset.NewStrSetFrom([]string{"a"}, true)
s.UnmarshalValue([]string{"b", "c"})
fmt.Println(s.Slice())
b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
type Student struct {
Id int
Name string
Scores *gset.StrSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// Output:
// [a b c]
// May Output:
// {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() {
var (
set gset.StrSet