example over

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

View File

@ -7,21 +7,36 @@
package gset_test
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
//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"}...)
fmt.Println(strSet.Slice())
// Mya Output:
// May Output:
//Iterator str1
//Iterator str2
//Iterator str3
}
//NewStrSetFrom returns a new set from <items>.
func ExampleStrSet_NewStrSetFrom() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Slice())
// Output:
// [str1 str2 str3]
}
//Add adds one or multiple items to the set.
func ExampleStrSet_Add() {
var strSet gset.StrSet
strSet.Add([]string{"str1", "str2", "str3"}...)
@ -32,6 +47,9 @@ func ExampleStrSet_Add() {
//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
fmt.Println(strSet.AddIfNotExist("str"))
@ -40,6 +58,10 @@ func ExampleStrSet_AddIfNotExist() {
// 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.
func ExampleStrSet_AddIfNotExistFunc() {
var strSet gset.StrSet
fmt.Println(strSet.AddIfNotExistFunc("str", func() bool {
@ -50,6 +72,10 @@ func ExampleStrSet_AddIfNotExistFunc() {
// 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.
func ExampleStrSet_AddIfNotExistFuncLock() {
var strSet gset.StrSet
fmt.Println(strSet.AddIfNotExistFuncLock("str", func() bool {
@ -60,10 +86,9 @@ func ExampleStrSet_AddIfNotExistFuncLock() {
// true
}
//Clear deletes all items of the set.
func ExampleStrSet_Clear() {
var strSet gset.StrSet
strSet.Add([]string{"str1", "str2", "str3"}...)
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Clear()
fmt.Println(strSet.Size())
@ -72,19 +97,19 @@ func ExampleStrSet_Clear() {
// 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>.
func ExampleStrSet_Complement() {
strSet := gset.NewStrSet(true)
strSet.Add([]string{"str1", "str2", "str3", "str4", "str5"}...)
var s gset.StrSet
s.Add([]string{"str1", "str2", "str3"}...)
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(s.Complement(strSet).Slice())
// May Output:
// [str4 str5]
}
//Contains checks whether the set contains <item>.
func ExampleStrSet_Contains() {
var set gset.StrSet
set.Add("a")
@ -96,6 +121,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.
func ExampleStrSet_ContainsI() {
var set gset.StrSet
set.Add("a")
@ -107,11 +134,11 @@ 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>.
func ExampleStrSet_Diff() {
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.Diff(s1).Slice())
@ -119,6 +146,7 @@ func ExampleStrSet_Diff() {
// [d]
}
//Equal checks whether the two sets equal.
func ExampleStrSet_Equal() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c"}...)
@ -130,6 +158,8 @@ func ExampleStrSet_Equal() {
// false
}
//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"}...)
@ -142,6 +172,7 @@ func ExampleStrSet_Intersect() {
// [c a b]
}
//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"}...)
@ -153,6 +184,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.
func ExampleStrSet_Iterator() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -167,6 +200,7 @@ func ExampleStrSet_Iterator() {
// Iterator d
}
//Join joins items with a string <glue>.
func ExampleStrSet_Join() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
@ -176,10 +210,175 @@ func ExampleStrSet_Join() {
// b,c,d,a
}
//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{}{}
})
}
//MarshalJSON implements the interface MarshalJSON for json.Marshal.
func ExampleStrSet_MarshalJSON() {
type Student struct {
Id int
Name string
Scores *gset.StrSet
}
s := Student{
Id: 1,
Name: "john",
Scores: gset.NewStrSetFrom([]string{"100", "99", "98"}, true),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// May Output:
// {"Id":1,"Name":"john","Scores":["100","99","98"]}
}
//Merge adds items from <others> sets into <set>.
func ExampleStrSet_Merge() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s2 := gset.NewStrSet(true)
fmt.Println(s1.Merge(s2).Slice())
// May Output:
// [d a b c]
}
//Pops randomly pops an item from set.
func ExampleStrSet_Pop() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Pop())
// May Output:
// a
}
//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"}...)
for _, v := range s1.Pops(2) {
fmt.Println(v)
}
// May Output:
// a
// b
}
//RLockFunc locks reading with callback function <f>.
func ExampleStrSet_RLockFunc() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s1.RLockFunc(func(m map[string]struct{}) {
fmt.Println(m)
})
// Output:
// map[a:{} b:{} c:{} d:{}]
}
//Remove deletes <item> from set.
func ExampleStrSet_Remove() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s1.Remove("a")
fmt.Println(s1.Slice())
// Output:
// [b c d]
}
//Size returns the size of the set.
func ExampleStrSet_Size() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Size())
// Output:
// 4
}
//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"}...)
fmt.Println(s1.Slice())
// May Output:
// [a,b,c,d]
}
//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"}...)
fmt.Println(s1.String())
// Output:
// "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.
func ExampleStrSet_Sum() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Sum())
// Output:
// 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>.
func ExampleStrSet_Union() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s2 := gset.NewStrSet(true)
s2.Add([]string{"a", "b", "d"}...)
fmt.Println(s1.Union(s2).Slice())
// May Output:
// [a b c d]
}
//UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func ExampleStrSet_UnmarshalJSON() {
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)
// May Output:
// {1 john "99","98","100"}
}
//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())
// Output:
// [a b c]
}
//Walk applies a user supplied function <f> to every item of set.
func ExampleStrSet_Walk() {
var (
set gset.StrSet