gf: github.com/gogf/gf/container/gset Index | Examples | Files

package gset

import "github.com/gogf/gf/container/gset"

Package gset provides kinds of concurrent-safe/unsafe sets.

Index

Examples

Package Files

gset_any_set.go gset_int_set.go gset_str_set.go

type IntSet Uses

type IntSet struct {
    // contains filtered or unexported fields
}

func NewIntSet Uses

func NewIntSet(safe ...bool) *IntSet

New 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 NewIntSetFrom Uses

func NewIntSetFrom(items []int, safe ...bool) *IntSet

NewIntSetFrom returns a new set from <items>.

func (*IntSet) Add Uses

func (set *IntSet) Add(item ...int)

Add adds one or multiple items to the set.

func (*IntSet) AddIfNotExist Uses

func (set *IntSet) AddIfNotExist(item int) bool

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.

Note that, if <item> is nil, it does nothing and returns false.

func (*IntSet) AddIfNotExistFunc Uses

func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool

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 (*IntSet) AddIfNotExistFuncLock Uses

func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool

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 (*IntSet) Clear Uses

func (set *IntSet) Clear()

Clear deletes all items of the set.

func (*IntSet) Complement Uses

func (set *IntSet) Complement(full *IntSet) (newSet *IntSet)

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 (*IntSet) Contains Uses

func (set *IntSet) Contains(item int) bool

Contains checks whether the set contains <item>.

Code:

var set gset.IntSet
set.Add(1)
fmt.Println(set.Contains(1))
fmt.Println(set.Contains(2))

Output:

true
false

func (*IntSet) Diff Uses

func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet)

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) Equal Uses

func (set *IntSet) Equal(other *IntSet) bool

Equal checks whether the two sets equal.

func (*IntSet) Intersect Uses

func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet)

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 Uses

func (set *IntSet) IsSubsetOf(other *IntSet) bool

IsSubsetOf checks whether the current set is a sub-set of <other>.

func (*IntSet) Iterator Uses

func (set *IntSet) Iterator(f func(v int) bool)

Iterator iterates the set readonly with given callback function <f>, if <f> returns true then continue iterating; or false to stop.

func (*IntSet) Join Uses

func (set *IntSet) Join(glue string) string

Join joins items with a string <glue>.

func (*IntSet) LockFunc Uses

func (set *IntSet) LockFunc(f func(m map[int]struct{}))

LockFunc locks writing with callback function <f>.

func (*IntSet) MarshalJSON Uses

func (set *IntSet) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*IntSet) Merge Uses

func (set *IntSet) Merge(others ...*IntSet) *IntSet

Merge adds items from <others> sets into <set>.

func (*IntSet) Pop Uses

func (set *IntSet) Pop() int

Pops randomly pops an item from set.

func (*IntSet) Pops Uses

func (set *IntSet) Pops(size int) []int

Pops randomly pops <size> items from set. It returns all items if size == -1.

func (*IntSet) RLockFunc Uses

func (set *IntSet) RLockFunc(f func(m map[int]struct{}))

RLockFunc locks reading with callback function <f>.

func (*IntSet) Remove Uses

func (set *IntSet) Remove(item int)

Remove deletes <item> from set.

func (*IntSet) Size Uses

func (set *IntSet) Size() int

Size returns the size of the set.

func (*IntSet) Slice Uses

func (set *IntSet) Slice() []int

Slice returns the a of items of the set as slice.

func (*IntSet) String Uses

func (set *IntSet) String() string

String returns items as a string, which implements like json.Marshal does.

func (*IntSet) Sum Uses

func (set *IntSet) Sum() (sum int)

Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.

func (*IntSet) Union Uses

func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet)

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 Uses

func (set *IntSet) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*IntSet) UnmarshalValue Uses

func (set *IntSet) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for set.

func (*IntSet) Walk Uses

func (set *IntSet) Walk(f func(item int) int) *IntSet

Walk applies a user supplied function <f> to every item of set.

type Set Uses

type Set struct {
    // contains filtered or unexported fields
}

func New Uses

func New(safe ...bool) *Set

New 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 NewFrom Uses

func NewFrom(items interface{}, safe ...bool) *Set

NewFrom returns a new set from <items>. Parameter <items> can be either a variable of any type, or a slice.

func NewSet Uses

func NewSet(safe ...bool) *Set

See New.

func (*Set) Add Uses

func (set *Set) Add(items ...interface{})

Add adds one or multiple items to the set.

func (*Set) AddIfNotExist Uses

func (set *Set) AddIfNotExist(item interface{}) bool

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.

Note that, if <item> is nil, it does nothing and returns false.

Code:

var set gset.Set
fmt.Println(set.AddIfNotExist(1))
fmt.Println(set.AddIfNotExist(1))
fmt.Println(set.Slice())

Output:

true
false
[1]

func (*Set) AddIfNotExistFunc Uses

func (set *Set) AddIfNotExistFunc(item interface{}, f func() bool) bool

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, if <item> is nil, it does nothing and returns false. The function <f> is executed without writing lock.

func (*Set) AddIfNotExistFuncLock Uses

func (set *Set) AddIfNotExistFuncLock(item interface{}, f func() bool) bool

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, if <item> is nil, it does nothing and returns false. The function <f> is executed within writing lock.

func (*Set) Clear Uses

func (set *Set) Clear()

Clear deletes all items of the set.

func (*Set) Complement Uses

func (set *Set) Complement(full *Set) (newSet *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>.

Code:

s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.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]

func (*Set) Contains Uses

func (set *Set) Contains(item interface{}) bool

Contains checks whether the set contains <item>.

Code:

var set gset.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 Uses

func (set *Set) Diff(others ...*Set) (newSet *Set)

Diff returns a new set which is the difference set from <set> to <others>. Which means, all the items in <newSet> are in <set> but not in <others>.

Code:

s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.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]

func (*Set) Equal Uses

func (set *Set) Equal(other *Set) bool

Equal checks whether the two sets equal.

func (*Set) Intersect Uses

func (set *Set) Intersect(others ...*Set) (newSet *Set)

Intersect returns a new set which is the intersection from <set> to <others>. Which means, all the items in <newSet> are in <set> and also in <others>.

Code:

s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.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]

func (*Set) IsSubsetOf Uses

func (set *Set) IsSubsetOf(other *Set) bool

IsSubsetOf checks whether the current set is a sub-set of <other>.

Code:

var s1, s2 gset.Set
s1.Add(g.Slice{1, 2, 3}...)
s2.Add(g.Slice{2, 3}...)
fmt.Println(s1.IsSubsetOf(&s2))
fmt.Println(s2.IsSubsetOf(&s1))

Output:

false
true

func (*Set) Iterator Uses

func (set *Set) Iterator(f func(v interface{}) bool)

Iterator iterates the set readonly with given callback function <f>, if <f> returns true then continue iterating; or false to stop.

func (*Set) Join Uses

func (set *Set) Join(glue string) string

Join joins items with a string <glue>.

Code:

var set gset.Set
set.Add("a", "b", "c", "d")
fmt.Println(set.Join(","))

// May Output:
// a,b,c,d

func (*Set) LockFunc Uses

func (set *Set) LockFunc(f func(m map[interface{}]struct{}))

LockFunc locks writing with callback function <f>.

func (*Set) MarshalJSON Uses

func (set *Set) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*Set) Merge Uses

func (set *Set) Merge(others ...*Set) *Set

Merge adds items from <others> sets into <set>.

func (*Set) Pop Uses

func (set *Set) Pop() interface{}

Pops randomly pops an item from set.

Code:

var set gset.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

func (*Set) Pops Uses

func (set *Set) Pops(size int) []interface{}

Pops randomly pops <size> items from set. It returns all items if size == -1.

Code:

var set gset.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

func (*Set) RLockFunc Uses

func (set *Set) RLockFunc(f func(m map[interface{}]struct{}))

RLockFunc locks reading with callback function <f>.

func (*Set) Remove Uses

func (set *Set) Remove(item interface{})

Remove deletes <item> from set.

func (*Set) Size Uses

func (set *Set) Size() int

Size returns the size of the set.

func (*Set) Slice Uses

func (set *Set) Slice() []interface{}

Slice returns the a of items of the set as slice.

func (*Set) String Uses

func (set *Set) String() string

String returns items as a string, which implements like json.Marshal does.

func (*Set) Sum Uses

func (set *Set) Sum() (sum int)

Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.

func (*Set) Union Uses

func (set *Set) Union(others ...*Set) (newSet *Set)

Union returns a new set which is the union of <set> and <others>. Which means, all the items in <newSet> are in <set> or in <others>.

Code:

s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.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]

func (*Set) UnmarshalJSON Uses

func (set *Set) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*Set) UnmarshalValue Uses

func (set *Set) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for set.

func (*Set) Walk Uses

func (set *Set) Walk(f func(item interface{}) interface{}) *Set

Walk applies a user supplied function <f> to every item of set.

type StrSet Uses

type StrSet struct {
    // contains filtered or unexported fields
}

func NewStrSet Uses

func NewStrSet(safe ...bool) *StrSet

New 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 Uses

func NewStrSetFrom(items []string, safe ...bool) *StrSet

NewStrSetFrom returns a new set from <items>.

func (*StrSet) Add Uses

func (set *StrSet) Add(item ...string)

Add adds one or multiple items to the set.

func (*StrSet) AddIfNotExist Uses

func (set *StrSet) AddIfNotExist(item string) bool

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 Uses

func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool

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 Uses

func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool

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) Clear Uses

func (set *StrSet) Clear()

Clear deletes all items of the set.

func (*StrSet) Complement Uses

func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)

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 Uses

func (set *StrSet) Contains(item string) bool

Contains checks whether the set contains <item>.

Code:

var set gset.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 Uses

func (set *StrSet) ContainsI(item string) bool

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 Uses

func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)

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) Equal Uses

func (set *StrSet) Equal(other *StrSet) bool

Equal checks whether the two sets equal.

func (*StrSet) Intersect Uses

func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)

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 Uses

func (set *StrSet) IsSubsetOf(other *StrSet) bool

IsSubsetOf checks whether the current set is a sub-set of <other>.

func (*StrSet) Iterator Uses

func (set *StrSet) Iterator(f func(v string) bool)

Iterator iterates the set readonly with given callback function <f>, if <f> returns true then continue iterating; or false to stop.

func (*StrSet) Join Uses

func (set *StrSet) Join(glue string) string

Join joins items with a string <glue>.

func (*StrSet) LockFunc Uses

func (set *StrSet) LockFunc(f func(m map[string]struct{}))

LockFunc locks writing with callback function <f>.

func (*StrSet) MarshalJSON Uses

func (set *StrSet) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*StrSet) Merge Uses

func (set *StrSet) Merge(others ...*StrSet) *StrSet

Merge adds items from <others> sets into <set>.

func (*StrSet) Pop Uses

func (set *StrSet) Pop() string

Pops randomly pops an item from set.

func (*StrSet) Pops Uses

func (set *StrSet) Pops(size int) []string

Pops randomly pops <size> items from set. It returns all items if size == -1.

func (*StrSet) RLockFunc Uses

func (set *StrSet) RLockFunc(f func(m map[string]struct{}))

RLockFunc locks reading with callback function <f>.

func (*StrSet) Remove Uses

func (set *StrSet) Remove(item string)

Remove deletes <item> from set.

func (*StrSet) Size Uses

func (set *StrSet) Size() int

Size returns the size of the set.

func (*StrSet) Slice Uses

func (set *StrSet) Slice() []string

Slice returns the a of items of the set as slice.

func (*StrSet) String Uses

func (set *StrSet) String() string

String returns items as a string, which implements like json.Marshal does.

func (*StrSet) Sum Uses

func (set *StrSet) Sum() (sum int)

Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.

func (*StrSet) Union Uses

func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)

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 Uses

func (set *StrSet) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*StrSet) UnmarshalValue Uses

func (set *StrSet) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for set.

func (*StrSet) Walk Uses

func (set *StrSet) Walk(f func(item string) string) *StrSet

Walk applies a user supplied function <f> to every item of set.

Code:

var (
    set    gset.StrSet
    names  = g.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]

Package gset imports 6 packages (graph) and is imported by 5 packages. Updated 2020-06-18. Refresh now. Tools for package owners.