Documentation ¶
Index ¶
- type IntSet
- func (set *IntSet) Add(item ...int)
- func (set *IntSet) AddIfNotExist(item int) bool
- func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool
- func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool
- func (set *IntSet) Clear()
- func (set *IntSet) Complement(full *IntSet) (newSet *IntSet)
- func (set *IntSet) Contains(item int) bool
- func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet)
- func (set *IntSet) Equal(other *IntSet) bool
- func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet)
- func (set *IntSet) IsSubsetOf(other *IntSet) bool
- func (set *IntSet) Iterator(f func(v int) bool)
- func (set *IntSet) Join(glue string) string
- func (set *IntSet) LockFunc(f func(m map[int]struct{}))
- func (set *IntSet) MarshalJSON() ([]byte, error)
- func (set *IntSet) Merge(others ...*IntSet) *IntSet
- func (set *IntSet) Pop() int
- func (set *IntSet) Pops(size int) []int
- func (set *IntSet) RLockFunc(f func(m map[int]struct{}))
- func (set *IntSet) Remove(item int)
- func (set *IntSet) Size() int
- func (set *IntSet) Slice() []int
- func (set *IntSet) String() string
- func (set *IntSet) Sum() (sum int)
- func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet)
- func (set *IntSet) UnmarshalJSON(b []byte) error
- func (set *IntSet) UnmarshalValue(value interface{}) (err error)
- func (set *IntSet) Walk(f func(item int) int) *IntSet
- type Set
- func (that *Set) Add(items ...interface{})
- func (that *Set) AddIfNotExist(item interface{}) bool
- func (that *Set) AddIfNotExistFunc(item interface{}, f func() bool) bool
- func (that *Set) AddIfNotExistFuncLock(item interface{}, f func() bool) bool
- func (that *Set) Clear()
- func (that *Set) Complement(full *Set) (newSet *Set)
- func (that *Set) Contains(item interface{}) bool
- func (that *Set) Diff(others ...*Set) (newSet *Set)
- func (that *Set) Equal(other *Set) bool
- func (that *Set) Intersect(others ...*Set) (newSet *Set)
- func (that *Set) IsSubsetOf(other *Set) bool
- func (that *Set) Iterator(f func(interface{}) bool)
- func (that *Set) Join(glue string) string
- func (that *Set) LockFunc(f func(m map[interface{}]struct{}))
- func (that *Set) MarshalJSON() ([]byte, error)
- func (that *Set) Merge(others ...*Set) *Set
- func (that *Set) Pop() interface{}
- func (that *Set) Pops(size int) []interface{}
- func (that *Set) RLockFunc(f func(m map[interface{}]struct{}))
- func (that *Set) Remove(item interface{})
- func (that *Set) Size() int
- func (that *Set) Slice() []interface{}
- func (that *Set) String() string
- func (that *Set) Sum() (sum int)
- func (that *Set) Union(others ...*Set) (newSet *Set)
- func (that *Set) UnmarshalJSON(b []byte) error
- func (that *Set) UnmarshalValue(value interface{}) (err error)
- func (that *Set) Walk(f func(item interface{}) interface{}) *Set
- type StrSet
- func (set *StrSet) Add(item ...string)
- func (set *StrSet) AddIfNotExist(item string) bool
- func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool
- func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool
- func (set *StrSet) Clear()
- func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)
- func (set *StrSet) Contains(item string) bool
- func (set *StrSet) ContainsI(item string) bool
- func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)
- func (set *StrSet) Equal(other *StrSet) bool
- func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)
- func (set *StrSet) IsSubsetOf(other *StrSet) bool
- func (set *StrSet) Iterator(f func(v string) bool)
- func (set *StrSet) Join(glue string) string
- func (set *StrSet) LockFunc(f func(m map[string]struct{}))
- func (set *StrSet) MarshalJSON() ([]byte, error)
- func (set *StrSet) Merge(others ...*StrSet) *StrSet
- func (set *StrSet) Pop() string
- func (set *StrSet) Pops(size int) []string
- func (set *StrSet) RLockFunc(f func(m map[string]struct{}))
- func (set *StrSet) Remove(item string)
- func (set *StrSet) Size() int
- func (set *StrSet) Slice() []string
- func (set *StrSet) String() string
- func (set *StrSet) Sum() (sum int)
- func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)
- func (set *StrSet) UnmarshalJSON(b []byte) error
- func (set *StrSet) UnmarshalValue(value interface{}) (err error)
- func (set *StrSet) Walk(f func(item string) string) *StrSet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IntSet ¶
type IntSet struct {
// contains filtered or unexported fields
}
func NewIntSetFrom ¶
NewIntSetFrom 从int类型的切片创建一个int类型的集合
func (*IntSet) AddIfNotExist ¶
AddIfNotExist 检查要添加的元素是否存在,不存在则添加,成功后返回true 如果已存在这个元素,则返回false
func (*IntSet) AddIfNotExistFunc ¶
AddIfNotExistFunc 检查要添加的元素是否存在,不存在则执行f方法,f方法返回成功则继续添加,成功后返回true 如果已存在这个元素或f方法执行返回false,则返回false 注意执行f方法不占用锁
func (*IntSet) AddIfNotExistFuncLock ¶
AddIfNotExistFuncLock 检查要添加的元素是否存在,不存在则执行f方法,f方法返回成功则继续添加,成功后返回true 如果已存在这个元素或f方法执行返回false,则返回false 注意执行f方法占用锁
func (*IntSet) Complement ¶
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>. 求指定full集合对与当前集合的补集
func (*IntSet) Contains ¶
Contains 判断集合中是否存在元素item
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.IntSet set.Add(1) fmt.Println(set.Contains(1)) fmt.Println(set.Contains(2)) }
Output: true false
func (*IntSet) Diff ¶
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 (*IntSet) Intersect ¶
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 (*IntSet) IsSubsetOf ¶
IsSubsetOf 检查当前集合是否为other集合的子集
func (*IntSet) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*IntSet) Sum ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected. 计算集合的和
func (*IntSet) Union ¶
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 (*IntSet) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*IntSet) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for set.
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
func (*Set) AddIfNotExist ¶
AddIfNotExist 如果元素不存在则添加
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.Set fmt.Println(set.AddIfNotExist(1)) fmt.Println(set.AddIfNotExist(1)) fmt.Println(set.Slice()) }
Output: true false [1]
func (*Set) AddIfNotExistFunc ¶
AddIfNotExistFunc 如果元素不存在,且执行f方法,返回成功,则添加元素到set 执行f方法的时候不加锁
func (*Set) AddIfNotExistFuncLock ¶
AddIfNotExistFuncLock 如果元素不存在,且执行f方法,返回成功,则添加元素到set // 执行f方法的时候加锁
func (*Set) Complement ¶
Complement 求指定full集合对与当前集合的补集
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" "github.com/osgochina/donkeygo/frame/d" ) func main() { s1 := dset.NewFrom(d.Slice{1, 2, 3}) s2 := dset.NewFrom(d.Slice{4, 5, 6}) s3 := dset.NewFrom(d.Slice{1, 2, 3, 4, 5, 6, 7}) fmt.Println(s3.Intersect(s1).Slice()) fmt.Println(s3.Diff(s1).Slice()) fmt.Println(s1.Union(s2).Slice()) fmt.Println(s1.Complement(s3).Slice()) // May Output: // [2 3 1] // [5 6 7 4] // [6 1 2 3 4 5] // [4 5 6 7] }
Output:
func (*Set) Contains ¶
Contains 判断集合中是否存在item元素
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.StrSet set.Add("a") fmt.Println(set.Contains("a")) fmt.Println(set.Contains("A")) fmt.Println(set.ContainsI("A")) }
Output: true false true
func (*Set) Diff ¶
Diff 求多个集合差集
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" "github.com/osgochina/donkeygo/frame/d" ) func main() { s1 := dset.NewFrom(d.Slice{1, 2, 3}) s2 := dset.NewFrom(d.Slice{4, 5, 6}) s3 := dset.NewFrom(d.Slice{1, 2, 3, 4, 5, 6, 7}) fmt.Println(s3.Intersect(s1).Slice()) fmt.Println(s3.Diff(s1).Slice()) fmt.Println(s1.Union(s2).Slice()) fmt.Println(s1.Complement(s3).Slice()) // May Output: // [2 3 1] // [5 6 7 4] // [6 1 2 3 4 5] // [4 5 6 7] }
Output:
func (*Set) Intersect ¶
Intersect 求多个集合的交集
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" "github.com/osgochina/donkeygo/frame/d" ) func main() { s1 := dset.NewFrom(d.Slice{1, 2, 3}) s2 := dset.NewFrom(d.Slice{4, 5, 6}) s3 := dset.NewFrom(d.Slice{1, 2, 3, 4, 5, 6, 7}) fmt.Println(s3.Intersect(s1).Slice()) fmt.Println(s3.Diff(s1).Slice()) fmt.Println(s1.Union(s2).Slice()) fmt.Println(s1.Complement(s3).Slice()) // May Output: // [2 3 1] // [5 6 7 4] // [6 1 2 3 4 5] // [4 5 6 7] }
Output:
func (*Set) IsSubsetOf ¶
IsSubsetOf 检查当前集合是否为other集合的子集
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" "github.com/osgochina/donkeygo/frame/d" ) func main() { var s1, s2 dset.Set s1.Add(d.Slice{1, 2, 3}...) s2.Add(d.Slice{2, 3}...) fmt.Println(s1.IsSubsetOf(&s2)) fmt.Println(s2.IsSubsetOf(&s1)) }
Output: false true
func (*Set) Join ¶
Join 把集合的元素转换成字符串,然后以glue作为分隔符链接起来,以字符的形式返回
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.Set set.Add("a", "b", "c", "d") fmt.Println(set.Join(",")) // May Output: // a,b,c,d }
Output:
func (*Set) LockFunc ¶
func (that *Set) LockFunc(f func(m map[interface{}]struct{}))
LockFunc 加锁执行自定义方法
func (*Set) Pop ¶
func (that *Set) Pop() interface{}
Pop 集合中取出一个元素
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.Set set.Add(1, 2, 3, 4) fmt.Println(set.Pop()) fmt.Println(set.Pops(2)) fmt.Println(set.Size()) // May Output: // 1 // [2 3] // 1 }
Output:
func (*Set) Pops ¶
Pops 从集合中取出指定size的元素
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.Set set.Add(1, 2, 3, 4) fmt.Println(set.Pop()) fmt.Println(set.Pops(2)) fmt.Println(set.Size()) // May Output: // 1 // [2 3] // 1 }
Output:
func (*Set) RLockFunc ¶
func (that *Set) RLockFunc(f func(m map[interface{}]struct{}))
RLockFunc 加读锁执行自定义方法
func (*Set) Union ¶
Union 求多个集合的并集
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" "github.com/osgochina/donkeygo/frame/d" ) func main() { s1 := dset.NewFrom(d.Slice{1, 2, 3}) s2 := dset.NewFrom(d.Slice{4, 5, 6}) s3 := dset.NewFrom(d.Slice{1, 2, 3, 4, 5, 6, 7}) fmt.Println(s3.Intersect(s1).Slice()) fmt.Println(s3.Diff(s1).Slice()) fmt.Println(s1.Union(s2).Slice()) fmt.Println(s1.Complement(s3).Slice()) // May Output: // [2 3 1] // [5 6 7 4] // [6 1 2 3 4 5] // [4 5 6 7] }
Output:
func (*Set) UnmarshalJSON ¶
UnmarshalJSON 把json格式转换成集合
func (*Set) UnmarshalValue ¶
UnmarshalValue 把整个值从对象中转换成集合能识别的值
type StrSet ¶
type StrSet struct {
// contains filtered or unexported fields
}
func NewStrSet ¶
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 NewStrSetFrom ¶
NewStrSetFrom returns a new set from <items>.
func (*StrSet) AddIfNotExist ¶
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 (*StrSet) AddIfNotExistFunc ¶
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 (*StrSet) AddIfNotExistFuncLock ¶
AddIfNotExistFuncLock 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 (*StrSet) Complement ¶
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 (*StrSet) Contains ¶
Contains checks whether the set contains <item>.
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" ) func main() { var set dset.StrSet set.Add("a") fmt.Println(set.Contains("a")) fmt.Println(set.Contains("A")) fmt.Println(set.ContainsI("A")) }
Output: true false true
func (*StrSet) ContainsI ¶
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 (*StrSet) Diff ¶
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 (*StrSet) Intersect ¶
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 (*StrSet) IsSubsetOf ¶
IsSubsetOf checks whether the current set is a sub-set of <other>.
func (*StrSet) Iterator ¶
Iterator iterates the set readonly with given callback function <f>, if <f> returns true then continue iterating; or false to stop.
func (*StrSet) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*StrSet) Sum ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
func (*StrSet) Union ¶
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 (*StrSet) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*StrSet) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for set.
func (*StrSet) Walk ¶
Walk applies a user supplied function <f> to every item of set.
Example ¶
package main import ( "fmt" "github.com/osgochina/donkeygo/container/dset" "github.com/osgochina/donkeygo/frame/d" ) func main() { var ( set dset.StrSet names = d.SliceStr{"user", "user_detail"} prefix = "gf_" ) set.Add(names...) // Add prefix for given table names. set.Walk(func(item string) string { return prefix + item }) fmt.Println(set.Slice()) // May Output: // [gf_user gf_user_detail] }
Output: