集合类

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

包gset提供了多种并发安全/不安全的集合(sets)。 集合,即不可重复的一组元素,元素项可以为任意类型。 同时,gset支持可选的并发安全参数选项,支持并发安全的场景。

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 X创建整数

func X创建整数(并发安全 ...bool) *IntSet

NewIntSet 创建并返回一个新的集合,其中包含不重复的项。 参数`safe`用于指定是否在并发安全的情况下使用集合,默认为false。

func X创建整数并按值

func X创建整数并按值(整数数组 []int, 并发安全 ...bool) *IntSet

NewIntSetFrom 从 `items` 返回一个新的集合。

func (*IntSet) DeepCopy

func (set *IntSet) DeepCopy() interface{}

DeepCopy 实现接口,用于当前类型的深度复制。

func (IntSet) MarshalJSON

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

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

Example

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

type Student struct {
	Id     int
	Name   string
	Scores *集合类.IntSet
}
s := Student{
	Id:     1,
	Name:   "john",
	Scores: 集合类.X创建整数并按值([]int{100, 99, 98}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))

// May Output:
// {"Id":1,"Name":"john","Scores":[100,99,98]}
Output:

func (*IntSet) String

func (set *IntSet) String() string

String 返回 items 作为字符串,其实现方式类似于 json.Marshal。

Example

String 返回 items 作为字符串,其实现方式类似于 json.Marshal。

s1 := 集合类.X创建整数()
s1.X加入([]int{1, 2, 3, 4}...)
fmt.Println(s1.String())

// May Output:
// [1,2,3,4]
Output:

func (*IntSet) UnmarshalJSON

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

UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。

Example

UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。

b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
	Id     int
	Name   string
	Scores *集合类.IntSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)

// May Output:
// {1 john [100,99,98]}
Output:

func (*IntSet) UnmarshalValue

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

UnmarshalValue 是一个接口实现,用于为 set 设置任意类型的值。

Example

UnmarshalValue 是一个接口实现,用于为 set 设置任意类型的值。

b := []byte(`{"Id":1,"Name":"john","Scores":100,99,98}`)
type Student struct {
	Id     int
	Name   string
	Scores *集合类.IntSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)

// May Output:
// {1 john [100,99,98]}
Output:

func (*IntSet) X写锁定_函数

func (set *IntSet) X写锁定_函数(f func(m map[int]struct{}))

LockFunc 使用回调函数`f`进行写入锁定。

func (*IntSet) X出栈

func (set *IntSet) X出栈() int

Pop 随机地从集合中弹出一个元素。

func (*IntSet) X出栈多个

func (set *IntSet) X出栈多个(数量 int) []int

Pops 随机地从集合中弹出 `size` 个元素。 如果 size == -1,则返回所有元素。

func (*IntSet) X删除

func (set *IntSet) X删除(值 int)

Remove 从集合中删除`item`。

func (*IntSet) X加入

func (set *IntSet) X加入(值s ...int)

Add 向集合中添加一个或多个项目。

func (*IntSet) X加入值并跳过已存在

func (set *IntSet) X加入值并跳过已存在(值 int) bool

AddIfNotExist 检查项是否已存在于集合中, 如果项不存在于集合中,则将其添加到集合并返回 true; 否则,不执行任何操作并返回 false。

注意:如果 `item` 为 nil,则不执行任何操作并返回 false。

func (*IntSet) X加入值并跳过已存在_函数

func (set *IntSet) X加入值并跳过已存在_函数(值 int, f func() bool) bool

AddIfNotExistFunc 检查项是否已存在于集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。

注意,函数 `f` 在无写入锁的情况下执行。

func (*IntSet) X加入值并跳过已存在_并发安全函数

func (set *IntSet) X加入值并跳过已存在_并发安全函数(值 int, f func() bool) bool

AddIfNotExistFuncLock 检查项是否存在集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。

注意,函数 `f` 在无写入锁的情况下执行。

func (*IntSet) X取交集

func (set *IntSet) X取交集(集合 ...*IntSet) (新集合 *IntSet)

Intersect 返回一个新的集合,它是从 `set` 到 `other` 的交集。 这意味着,`newSet` 中的所有元素都在 `set` 中,并且也在 `other` 中。

func (*IntSet) X取差集

func (set *IntSet) X取差集(集合 ...*IntSet) (新集合 *IntSet)

Diff 返回一个新的集合,这个集合是 `set` 与 `other` 的差集。 这意味着,新集合 `newSet` 中的所有元素都在 `set` 中,但不在 `other` 中。

func (*IntSet) X取并集

func (set *IntSet) X取并集(集合 ...*IntSet) (新集合 *IntSet)

Union 返回一个新的集合,该集合是 `set` 和 `other` 的并集。 这意味着,`newSet` 中的所有元素都在 `set` 或者 `other` 中。

func (*IntSet) X取数量

func (set *IntSet) X取数量() int

Size 返回集合的大小。

func (*IntSet) X取补集

func (set *IntSet) X取补集(集合 *IntSet) (新集合 *IntSet)

Complement 返回一个新的集合,该集合为 `set` 在 `full` 中的补集。 这意味着,新集合 `newSet` 中的所有元素都在 `full` 中但不在 `set` 中。

如果给定的集合 `full` 不是 `set` 的全集,则返回 `full` 与 `set` 之间的差集。

func (*IntSet) X取集合数组

func (set *IntSet) X取集合数组() []int

Slice 返回集合中项目的切片形式。

func (*IntSet) X取集合文本

func (set *IntSet) X取集合文本(连接符 string) string

Join通过字符串`glue`连接items。

func (*IntSet) X合并

func (set *IntSet) X合并(集合s ...*IntSet) *IntSet

Merge 将 `others` 中的元素合并到 `set` 中。

func (*IntSet) X是否为子集

func (set *IntSet) X是否为子集(父集 *IntSet) bool

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

func (*IntSet) X是否存在

func (set *IntSet) X是否存在(值 int) bool

Contains 检查集合中是否包含 `item`。

func (*IntSet) X是否相等

func (set *IntSet) X是否相等(待比较集合 *IntSet) bool

Equal 检查两个集合是否相等。

func (*IntSet) X求和

func (set *IntSet) X求和() (总和 int)

Sum 计算项的总和。 注意:项应转换为 int 类型, 否则你将得到一个意想不到的结果。

func (*IntSet) X清空

func (set *IntSet) X清空()

清除删除集合中的所有项。

func (*IntSet) X读锁定_函数

func (set *IntSet) X读锁定_函数(f func(m map[int]struct{}))

RLockFunc 通过回调函数 `f` 对读取进行加锁。

func (*IntSet) X遍历

func (set *IntSet) X遍历(f func(v int) bool)

Iterator 使用给定的回调函数`f`对集合进行只读遍历, 如果`f`返回true,则继续遍历;若返回false,则停止遍历。

func (*IntSet) X遍历修改

func (set *IntSet) X遍历修改(f func(值 int) int) *IntSet

Walk 对集合中的每一个元素应用用户提供的函数 `f`。

type Set

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

func NewSet别名

func NewSet别名(safe ...bool) *Set

NewSet 为New别名, 创建并返回一个新的集合,其中包含不重复的项目。 也可以参考 New。

func X创建

func X创建(并发安全 ...bool) *Set

New 创建并返回一个新的集合,其中包含无重复项。 参数`safe`用于指定是否在并发安全的情况下使用集合,默认为false。

func X创建并按值

func X创建并按值(值 interface{}, 并发安全 ...bool) *Set

NewFrom 函数根据 `items` 返回一个新的集合。 参数 `items` 可以是任何类型的变量,也可以是一个切片。

func (*Set) DeepCopy

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

DeepCopy 实现接口,用于当前类型的深度复制。

func (Set) MarshalJSON

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

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

func (*Set) String

func (set *Set) String() string

String 返回 items 作为字符串,其实现方式类似于 json.Marshal。

func (*Set) UnmarshalJSON

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

UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。

func (*Set) UnmarshalValue

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

UnmarshalValue 是一个接口实现,用于为 set 设置任意类型的值。

func (*Set) X写锁定_函数

func (set *Set) X写锁定_函数(f func(m map[interface{}]struct{}))

LockFunc 使用回调函数`f`进行写入锁定。

func (*Set) X出栈

func (set *Set) X出栈() interface{}

Pop 随机地从集合中弹出一个元素。

func (*Set) X出栈多个

func (set *Set) X出栈多个(数量 int) []interface{}

Pops 随机地从集合中弹出 `size` 个元素。 如果 size == -1,则返回所有元素。

func (*Set) X删除

func (set *Set) X删除(值 interface{})

Remove 从集合中删除`item`。

func (*Set) X加入

func (set *Set) X加入(值s ...interface{})

Add 向集合中添加一个或多个项目。

func (*Set) X加入值并跳过已存在

func (set *Set) X加入值并跳过已存在(值 interface{}) bool

AddIfNotExist 检查项是否已存在于集合中, 如果项不存在于集合中,则将其添加到集合并返回 true; 否则,不执行任何操作并返回 false。

注意:如果 `item` 为 nil,则不执行任何操作并返回 false。

func (*Set) X加入值并跳过已存在_函数

func (set *Set) X加入值并跳过已存在_函数(值 interface{}, f func() bool) bool

AddIfNotExistFunc 检查项是否存在集合中, 如果项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不做任何操作并返回 false。

注意,如果 `item` 为 nil,将不做任何操作并返回 false。函数 `f` 在无写入锁的情况下执行。

func (*Set) X加入值并跳过已存在_并发安全函数

func (set *Set) X加入值并跳过已存在_并发安全函数(值 interface{}, f func() bool) bool

AddIfNotExistFuncLock 检查项目是否已存在于集合中, 如果该项目不存在于集合中且函数 `f` 返回 true,则将项目添加到集合并返回 true。 否则,不执行任何操作并返回 false。

注意,如果 `item` 为 nil,则不执行任何操作并返回 false。函数 `f` 在写入锁的保护下执行。

func (*Set) X取交集

func (set *Set) X取交集(集合 ...*Set) (新集合 *Set)

Intersect 返回一个新的集合,它是从 `set` 到 `others` 的交集。 这意味着,新集合 `newSet` 中的所有元素都同时存在于 `set` 和 `others` 中。

func (*Set) X取差集

func (set *Set) X取差集(集合 ...*Set) (新集合 *Set)

Diff 返回一个新的集合,它是从 `set` 到 `others` 的差集。 这意味着,新集合 `newSet` 中的所有元素都在 `set` 中,但不在 `others` 中。

func (*Set) X取并集

func (set *Set) X取并集(集合 ...*Set) (新集合 *Set)

Union 返回一个新的集合,该集合是 `set` 和 `others` 的并集。 这意味着,`newSet` 中的所有元素要么在 `set` 中,要么在 `others` 中。

func (*Set) X取数量

func (set *Set) X取数量() int

Size 返回集合的大小。

func (*Set) X取补集

func (set *Set) X取补集(集合 *Set) (新集合 *Set)

Complement 返回一个新的集合,该集合为 `set` 在 `full` 中的补集。 这意味着,新集合 `newSet` 中的所有元素都在 `full` 中但不在 `set` 中。

如果给定的集合 `full` 不是 `set` 的全集,则返回 `full` 与 `set` 之间的差集。

func (*Set) X取集合数组

func (set *Set) X取集合数组() []interface{}

Slice 返回集合中所有项作为一个切片。

func (*Set) X取集合文本

func (set *Set) X取集合文本(连接符 string) string

Join通过字符串`glue`连接items。

func (*Set) X合并

func (set *Set) X合并(集合s ...*Set) *Set

Merge 将 `others` 中的元素合并到 `set` 中。

func (*Set) X是否为子集

func (set *Set) X是否为子集(父集 *Set) bool

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

func (*Set) X是否存在

func (set *Set) X是否存在(值 interface{}) bool

Contains 检查集合中是否包含 `item`。

func (*Set) X是否相等

func (set *Set) X是否相等(待比较集合 *Set) bool

Equal 检查两个集合是否相等。

func (*Set) X求和

func (set *Set) X求和() (总和 int)

Sum 计算项的总和。 注意:项应转换为 int 类型, 否则你将得到一个意想不到的结果。

func (*Set) X清空

func (set *Set) X清空()

清除删除集合中的所有项。

func (*Set) X读锁定_函数

func (set *Set) X读锁定_函数(f func(m map[interface{}]struct{}))

RLockFunc 通过回调函数 `f` 对读取进行加锁。

func (*Set) X遍历

func (set *Set) X遍历(f func(v interface{}) bool)

Iterator 使用给定的回调函数`f`对集合进行只读遍历, 如果`f`返回true,则继续遍历;若返回false,则停止遍历。

func (*Set) X遍历修改

func (set *Set) X遍历修改(f func(值 interface{}) interface{}) *Set

Walk 对集合中的每一个元素应用用户提供的函数 `f`。

type StrSet

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

func X创建文本

func X创建文本(并发安全 ...bool) *StrSet

NewStrSet 创建并返回一个新的不包含重复项的集合。 参数`safe`用于指定是否在并发安全的情况下使用集合,默认为false。 这里,NewStrSet函数用于创建一个字符串集合,并确保其中的元素互不重复。该函数接受一个可选参数`safe`,它是一个布尔值,表示是否需要保证在并发环境下的安全性。如果不特别指定,那么默认情况下这个集合是不保证并发安全的。

func X创建文本并按值

func X创建文本并按值(值 []string, 并发安全 ...bool) *StrSet

NewStrSetFrom 从 `items` 中创建并返回一个新的集合。

func (*StrSet) DeepCopy

func (set *StrSet) DeepCopy() interface{}

DeepCopy 实现接口,用于当前类型的深度复制。

func (StrSet) MarshalJSON

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

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

Example

MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。

type Student struct {
	Id     int
	Name   string
	Scores *集合类.StrSet
}
s := Student{
	Id:     1,
	Name:   "john",
	Scores: 集合类.X创建文本并按值([]string{"100", "99", "98"}, true),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))

// May Output:
// {"Id":1,"Name":"john","Scores":["100","99","98"]}
Output:

func (*StrSet) String

func (set *StrSet) String() string

String 返回 items 作为字符串,其实现方式类似于 json.Marshal。

Example

String 返回 items 作为字符串,其实现方式类似于 json.Marshal。

s1 := 集合类.X创建文本(true)
s1.X加入([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.String())

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

func (*StrSet) UnmarshalJSON

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

UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。

Example

UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。

b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
type Student struct {
	Id     int
	Name   string
	Scores *集合类.StrSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)

// May Output:
// {1 john "99","98","100"}
Output:

func (*StrSet) UnmarshalValue

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

UnmarshalValue 是一个接口实现,用于为 set 设置任意类型的值。

Example

UnmarshalValue 是一个接口实现,用于为 set 设置任意类型的值。

b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
type Student struct {
	Id     int
	Name   string
	Scores *集合类.StrSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)

// May Output:
// {1 john "99","98","100"}
Output:

func (*StrSet) X写锁定_函数

func (set *StrSet) X写锁定_函数(f func(m map[string]struct{}))

LockFunc 使用回调函数`f`进行写入锁定。

func (*StrSet) X出栈

func (set *StrSet) X出栈() string

Pop 随机地从集合中弹出一个元素。

func (*StrSet) X出栈多个

func (set *StrSet) X出栈多个(数量 int) []string

Pops 随机地从集合中弹出 `size` 个元素。 如果 size == -1,则返回所有元素。

func (*StrSet) X删除

func (set *StrSet) X删除(值 string)

Remove 从集合中删除`item`。

func (*StrSet) X加入

func (set *StrSet) X加入(值s ...string)

Add 向集合中添加一个或多个项目。

func (*StrSet) X加入值并跳过已存在

func (set *StrSet) X加入值并跳过已存在(值 string) bool

AddIfNotExist 检查项目是否已存在于集合中, 若该项目不在集合中,则将其添加到集合并返回 true; 否则,不做任何操作并返回 false。

func (*StrSet) X加入值并跳过已存在_函数

func (set *StrSet) X加入值并跳过已存在_函数(值 string, f func() bool) bool

AddIfNotExistFunc 检查项是否已存在于集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。

注意,函数 `f` 在无写入锁的情况下执行。

func (*StrSet) X加入值并跳过已存在_并发安全函数

func (set *StrSet) X加入值并跳过已存在_并发安全函数(值 string, f func() bool) bool

AddIfNotExistFuncLock 检查项是否存在集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。

注意,函数 `f` 在无写入锁的情况下执行。

func (*StrSet) X取交集

func (set *StrSet) X取交集(集合 ...*StrSet) (新集合 *StrSet)

Intersect 返回一个新的集合,它是从 `set` 到 `other` 的交集。 这意味着,`newSet` 中的所有元素都在 `set` 中,并且也在 `other` 中。

func (*StrSet) X取差集

func (set *StrSet) X取差集(集合 ...*StrSet) (新集合 *StrSet)

Diff 返回一个新的集合,这个集合是 `set` 与 `other` 的差集。 这意味着,新集合 `newSet` 中的所有元素都在 `set` 中,但不在 `other` 中。

func (*StrSet) X取并集

func (set *StrSet) X取并集(集合 ...*StrSet) (新集合 *StrSet)

Union 返回一个新的集合,该集合是 `set` 和 `other` 的并集。 这意味着,`newSet` 中的所有元素都在 `set` 或者 `other` 中。

func (*StrSet) X取数量

func (set *StrSet) X取数量() int

Size 返回集合的大小。

func (*StrSet) X取补集

func (set *StrSet) X取补集(集合 *StrSet) (新集合 *StrSet)

Complement 返回一个新的集合,该集合为 `set` 在 `full` 中的补集。 这意味着,新集合 `newSet` 中的所有元素都在 `full` 中但不在 `set` 中。

如果给定的集合 `full` 不是 `set` 的全集,则返回 `full` 与 `set` 之间的差集。

func (*StrSet) X取集合数组

func (set *StrSet) X取集合数组() []string

Slice 返回集合中项目的切片形式。

func (*StrSet) X取集合文本

func (set *StrSet) X取集合文本(连接符 string) string

Join通过字符串`glue`连接items。

func (*StrSet) X合并

func (set *StrSet) X合并(集合s ...*StrSet) *StrSet

Merge 将 `others` 中的元素合并到 `set` 中。

func (*StrSet) X是否为子集

func (set *StrSet) X是否为子集(父集 *StrSet) bool

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

func (*StrSet) X是否存在

func (set *StrSet) X是否存在(值 string) bool

Contains 检查集合中是否包含 `item`。

func (*StrSet) X是否存在并忽略大小写

func (set *StrSet) X是否存在并忽略大小写(值 string) bool

ContainsI 检查某个值是否以不区分大小写的方式存在于集合中。 注意:它内部会遍历整个集合,以不区分大小写的方式进行比较。

func (*StrSet) X是否相等

func (set *StrSet) X是否相等(待比较集合 *StrSet) bool

Equal 检查两个集合是否相等。

func (*StrSet) X求和

func (set *StrSet) X求和() (总和 int)

Sum 计算项的总和。 注意:项应转换为 int 类型, 否则你将得到一个意想不到的结果。

func (*StrSet) X清空

func (set *StrSet) X清空()

清除删除集合中的所有项。

func (*StrSet) X读锁定_函数

func (set *StrSet) X读锁定_函数(f func(m map[string]struct{}))

RLockFunc 通过回调函数 `f` 对读取进行加锁。

func (*StrSet) X遍历

func (set *StrSet) X遍历(f func(v string) bool)

Iterator 使用给定的回调函数`f`对集合进行只读遍历, 如果`f`返回true,则继续遍历;若返回false,则停止遍历。

func (*StrSet) X遍历修改

func (set *StrSet) X遍历修改(f func(值 string) string) *StrSet

Walk 对集合中的每一个元素应用用户提供的函数 `f`。

Jump to

Keyboard shortcuts

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