dset

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

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 NewIntSet

func NewIntSet(safe ...bool) *IntSet

NewIntSet 创建一个int类型的集合

func NewIntSetFrom

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

NewIntSetFrom 从int类型的切片创建一个int类型的集合

func (*IntSet) Add

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

Add 添加一个或多个int元素到集合

func (*IntSet) AddIfNotExist

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

AddIfNotExist 检查要添加的元素是否存在,不存在则添加,成功后返回true 如果已存在这个元素,则返回false

func (*IntSet) AddIfNotExistFunc

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

AddIfNotExistFunc 检查要添加的元素是否存在,不存在则执行f方法,f方法返回成功则继续添加,成功后返回true 如果已存在这个元素或f方法执行返回false,则返回false 注意执行f方法不占用锁

func (*IntSet) AddIfNotExistFuncLock

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

AddIfNotExistFuncLock 检查要添加的元素是否存在,不存在则执行f方法,f方法返回成功则继续添加,成功后返回true 如果已存在这个元素或f方法执行返回false,则返回false 注意执行f方法占用锁

func (*IntSet) Clear

func (set *IntSet) Clear()

Clear deletes all items of the set.

func (*IntSet) Complement

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>. 求指定full集合对与当前集合的补集

func (*IntSet) Contains

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

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

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

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

Equal 判断一个或多个集合是否相等

func (*IntSet) Intersect

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

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

IsSubsetOf 检查当前集合是否为other集合的子集

func (*IntSet) Iterator

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

Iterator 对这个集合迭代

func (*IntSet) Join

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

Join 使用<glue>连接集合,并返回连接后的字符串

func (*IntSet) LockFunc

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

LockFunc 加锁执行f

func (*IntSet) MarshalJSON

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

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*IntSet) Merge

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

Merge adds items from <others> sets into <set>. 合并多个集合

func (*IntSet) Pop

func (set *IntSet) Pop() int

Pop randomly pops an item from set.

func (*IntSet) Pops

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

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

func (*IntSet) RLockFunc

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

RLockFunc 加读锁执行f

func (*IntSet) Remove

func (set *IntSet) Remove(item int)

Remove 从集合中删除元素item

func (*IntSet) Size

func (set *IntSet) Size() int

Size returns the size of the set.

func (*IntSet) Slice

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

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

func (*IntSet) String

func (set *IntSet) String() string

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

func (*IntSet) Sum

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

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

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

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*IntSet) UnmarshalValue

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

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

func (*IntSet) Walk

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

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

type Set

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

func New

func New(safe ...bool) *Set

New 创建并返回一个Set

func NewFrom

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

NewFrom 从items创建一个set

func NewSet

func NewSet(safe ...bool) *Set

func (*Set) Add

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

Add 添加一个元素到集合

func (*Set) AddIfNotExist

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

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

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

AddIfNotExistFunc 如果元素不存在,且执行f方法,返回成功,则添加元素到set 执行f方法的时候不加锁

func (*Set) AddIfNotExistFuncLock

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

AddIfNotExistFuncLock 如果元素不存在,且执行f方法,返回成功,则添加元素到set // 执行f方法的时候加锁

func (*Set) Clear

func (that *Set) Clear()

Clear 清空集合

func (*Set) Complement

func (that *Set) Complement(full *Set) (newSet *Set)

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

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

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

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

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

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

Equal 判断两个集合是否相等

func (*Set) Intersect

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

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

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

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) Iterator

func (that *Set) Iterator(f func(interface{}) bool)

Iterator 迭代集合

func (*Set) Join

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

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) MarshalJSON

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

MarshalJSON 把集合格式化成json格式

func (*Set) Merge

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

Merge 合并一个或多个集合

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

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

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) Remove

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

Remove 从集合中移除元素

func (*Set) Size

func (that *Set) Size() int

Size 获取集合的长度

func (*Set) Slice

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

Slice 返回集合内的元素,以切片的形式返回

func (*Set) String

func (that *Set) String() string

把集合转换成字符串

func (*Set) Sum

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

Sum 集合所有元素累加

func (*Set) Union

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

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

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

UnmarshalJSON 把json格式转换成集合

func (*Set) UnmarshalValue

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

UnmarshalValue 把整个值从对象中转换成集合能识别的值

func (*Set) Walk

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

Walk 针对集合中的每个元素执行一次k方法

type StrSet

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

func NewStrSet

func NewStrSet(safe ...bool) *StrSet

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

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

NewStrSetFrom returns a new set from <items>.

func (*StrSet) Add

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

Add adds one or multiple items to the set.

func (*StrSet) AddIfNotExist

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

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

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

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

func (set *StrSet) Clear()

Clear deletes all items of the set.

func (*StrSet) Complement

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

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

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

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

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

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

Equal checks whether the two sets equal.

func (*StrSet) Intersect

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

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

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

func (*StrSet) Iterator

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

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

Join joins items with a string <glue>.

func (*StrSet) LockFunc

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

LockFunc locks writing with callback function <f>.

func (*StrSet) MarshalJSON

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

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*StrSet) Merge

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

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

func (*StrSet) Pop

func (set *StrSet) Pop() string

Pop randomly pops an item from set.

func (*StrSet) Pops

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

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

func (*StrSet) RLockFunc

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

RLockFunc locks reading with callback function <f>.

func (*StrSet) Remove

func (set *StrSet) Remove(item string)

Remove deletes <item> from set.

func (*StrSet) Size

func (set *StrSet) Size() int

Size returns the size of the set.

func (*StrSet) Slice

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

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

func (*StrSet) String

func (set *StrSet) String() string

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

func (*StrSet) Sum

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

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

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

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*StrSet) UnmarshalValue

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

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

func (*StrSet) Walk

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

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:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL