Documentation ¶
Overview ¶
包gset提供了多种并发安全/不安全的集合(sets)。 集合,即不可重复的一组元素,元素项可以为任意类型。 同时,gset支持可选的并发安全参数选项,支持并发安全的场景。
Index ¶
- type IntSet
- func (set *IntSet) DeepCopy() interface{}
- func (set IntSet) MarshalJSON() ([]byte, error)
- func (set *IntSet) String() string
- func (set *IntSet) UnmarshalJSON(b []byte) error
- func (set *IntSet) UnmarshalValue(value interface{}) (err error)
- func (set *IntSet) X写锁定_函数(f func(m map[int]struct{}))
- func (set *IntSet) X出栈() int
- func (set *IntSet) X出栈多个(数量 int) []int
- func (set *IntSet) X删除(值 int)
- func (set *IntSet) X加入(值s ...int)
- func (set *IntSet) X加入值并跳过已存在(值 int) bool
- func (set *IntSet) X加入值并跳过已存在_函数(值 int, f func() bool) bool
- func (set *IntSet) X加入值并跳过已存在_并发安全函数(值 int, f func() bool) bool
- func (set *IntSet) X取交集(集合 ...*IntSet) (新集合 *IntSet)
- func (set *IntSet) X取差集(集合 ...*IntSet) (新集合 *IntSet)
- func (set *IntSet) X取并集(集合 ...*IntSet) (新集合 *IntSet)
- func (set *IntSet) X取数量() int
- func (set *IntSet) X取补集(集合 *IntSet) (新集合 *IntSet)
- func (set *IntSet) X取集合数组() []int
- func (set *IntSet) X取集合文本(连接符 string) string
- func (set *IntSet) X合并(集合s ...*IntSet) *IntSet
- func (set *IntSet) X是否为子集(父集 *IntSet) bool
- func (set *IntSet) X是否存在(值 int) bool
- func (set *IntSet) X是否相等(待比较集合 *IntSet) bool
- func (set *IntSet) X求和() (总和 int)
- func (set *IntSet) X清空()
- func (set *IntSet) X读锁定_函数(f func(m map[int]struct{}))
- func (set *IntSet) X遍历(f func(v int) bool)
- func (set *IntSet) X遍历修改(f func(值 int) int) *IntSet
- type Set
- func (set *Set) DeepCopy() interface{}
- func (set Set) MarshalJSON() ([]byte, error)
- func (set *Set) String() string
- func (set *Set) UnmarshalJSON(b []byte) error
- func (set *Set) UnmarshalValue(value interface{}) (err error)
- func (set *Set) X写锁定_函数(f func(m map[interface{}]struct{}))
- func (set *Set) X出栈() interface{}
- func (set *Set) X出栈多个(数量 int) []interface{}
- func (set *Set) X删除(值 interface{})
- func (set *Set) X加入(值s ...interface{})
- func (set *Set) X加入值并跳过已存在(值 interface{}) bool
- func (set *Set) X加入值并跳过已存在_函数(值 interface{}, f func() bool) bool
- func (set *Set) X加入值并跳过已存在_并发安全函数(值 interface{}, f func() bool) bool
- func (set *Set) X取交集(集合 ...*Set) (新集合 *Set)
- func (set *Set) X取差集(集合 ...*Set) (新集合 *Set)
- func (set *Set) X取并集(集合 ...*Set) (新集合 *Set)
- func (set *Set) X取数量() int
- func (set *Set) X取补集(集合 *Set) (新集合 *Set)
- func (set *Set) X取集合数组() []interface{}
- func (set *Set) X取集合文本(连接符 string) string
- func (set *Set) X合并(集合s ...*Set) *Set
- func (set *Set) X是否为子集(父集 *Set) bool
- func (set *Set) X是否存在(值 interface{}) bool
- func (set *Set) X是否相等(待比较集合 *Set) bool
- func (set *Set) X求和() (总和 int)
- func (set *Set) X清空()
- func (set *Set) X读锁定_函数(f func(m map[interface{}]struct{}))
- func (set *Set) X遍历(f func(v interface{}) bool)
- func (set *Set) X遍历修改(f func(值 interface{}) interface{}) *Set
- type StrSet
- func (set *StrSet) DeepCopy() interface{}
- func (set StrSet) MarshalJSON() ([]byte, error)
- func (set *StrSet) String() string
- func (set *StrSet) UnmarshalJSON(b []byte) error
- func (set *StrSet) UnmarshalValue(value interface{}) (err error)
- func (set *StrSet) X写锁定_函数(f func(m map[string]struct{}))
- func (set *StrSet) X出栈() string
- func (set *StrSet) X出栈多个(数量 int) []string
- func (set *StrSet) X删除(值 string)
- func (set *StrSet) X加入(值s ...string)
- func (set *StrSet) X加入值并跳过已存在(值 string) bool
- func (set *StrSet) X加入值并跳过已存在_函数(值 string, f func() bool) bool
- func (set *StrSet) X加入值并跳过已存在_并发安全函数(值 string, f func() bool) bool
- func (set *StrSet) X取交集(集合 ...*StrSet) (新集合 *StrSet)
- func (set *StrSet) X取差集(集合 ...*StrSet) (新集合 *StrSet)
- func (set *StrSet) X取并集(集合 ...*StrSet) (新集合 *StrSet)
- func (set *StrSet) X取数量() int
- func (set *StrSet) X取补集(集合 *StrSet) (新集合 *StrSet)
- func (set *StrSet) X取集合数组() []string
- func (set *StrSet) X取集合文本(连接符 string) string
- func (set *StrSet) X合并(集合s ...*StrSet) *StrSet
- func (set *StrSet) X是否为子集(父集 *StrSet) bool
- func (set *StrSet) X是否存在(值 string) bool
- func (set *StrSet) X是否存在并忽略大小写(值 string) bool
- func (set *StrSet) X是否相等(待比较集合 *StrSet) bool
- func (set *StrSet) X求和() (总和 int)
- func (set *StrSet) X清空()
- func (set *StrSet) X读锁定_函数(f func(m map[string]struct{}))
- func (set *StrSet) X遍历(f func(v string) bool)
- func (set *StrSet) X遍历修改(f func(值 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 (IntSet) MarshalJSON ¶
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 ¶
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 ¶
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 ¶
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加入值并跳过已存在 ¶
AddIfNotExist 检查项是否已存在于集合中, 如果项不存在于集合中,则将其添加到集合并返回 true; 否则,不执行任何操作并返回 false。
注意:如果 `item` 为 nil,则不执行任何操作并返回 false。
func (*IntSet) X加入值并跳过已存在_函数 ¶
AddIfNotExistFunc 检查项是否已存在于集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。
注意,函数 `f` 在无写入锁的情况下执行。
func (*IntSet) X加入值并跳过已存在_并发安全函数 ¶
AddIfNotExistFuncLock 检查项是否存在集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。
注意,函数 `f` 在无写入锁的情况下执行。
func (*IntSet) X取交集 ¶
Intersect 返回一个新的集合,它是从 `set` 到 `other` 的交集。 这意味着,`newSet` 中的所有元素都在 `set` 中,并且也在 `other` 中。
func (*IntSet) X取差集 ¶
Diff 返回一个新的集合,这个集合是 `set` 与 `other` 的差集。 这意味着,新集合 `newSet` 中的所有元素都在 `set` 中,但不在 `other` 中。
func (*IntSet) X取并集 ¶
Union 返回一个新的集合,该集合是 `set` 和 `other` 的并集。 这意味着,`newSet` 中的所有元素都在 `set` 或者 `other` 中。
func (*IntSet) X取补集 ¶
Complement 返回一个新的集合,该集合为 `set` 在 `full` 中的补集。 这意味着,新集合 `newSet` 中的所有元素都在 `full` 中但不在 `set` 中。
如果给定的集合 `full` 不是 `set` 的全集,则返回 `full` 与 `set` 之间的差集。
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
func (Set) MarshalJSON ¶
MarshalJSON 实现了 json.Marshal 接口所需的 MarshalJSON 方法。
func (*Set) UnmarshalJSON ¶
UnmarshalJSON 实现了 json.Unmarshal 接口的 UnmarshalJSON 方法。
func (*Set) UnmarshalValue ¶
UnmarshalValue 是一个接口实现,用于为 set 设置任意类型的值。
func (*Set) X写锁定_函数 ¶
func (set *Set) X写锁定_函数(f func(m map[interface{}]struct{}))
LockFunc 使用回调函数`f`进行写入锁定。
func (*Set) X加入值并跳过已存在 ¶
AddIfNotExist 检查项是否已存在于集合中, 如果项不存在于集合中,则将其添加到集合并返回 true; 否则,不执行任何操作并返回 false。
注意:如果 `item` 为 nil,则不执行任何操作并返回 false。
func (*Set) X加入值并跳过已存在_函数 ¶
AddIfNotExistFunc 检查项是否存在集合中, 如果项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不做任何操作并返回 false。
注意,如果 `item` 为 nil,将不做任何操作并返回 false。函数 `f` 在无写入锁的情况下执行。
func (*Set) X加入值并跳过已存在_并发安全函数 ¶
AddIfNotExistFuncLock 检查项目是否已存在于集合中, 如果该项目不存在于集合中且函数 `f` 返回 true,则将项目添加到集合并返回 true。 否则,不执行任何操作并返回 false。
注意,如果 `item` 为 nil,则不执行任何操作并返回 false。函数 `f` 在写入锁的保护下执行。
func (*Set) X取交集 ¶
Intersect 返回一个新的集合,它是从 `set` 到 `others` 的交集。 这意味着,新集合 `newSet` 中的所有元素都同时存在于 `set` 和 `others` 中。
func (*Set) X取差集 ¶
Diff 返回一个新的集合,它是从 `set` 到 `others` 的差集。 这意味着,新集合 `newSet` 中的所有元素都在 `set` 中,但不在 `others` 中。
func (*Set) X取并集 ¶
Union 返回一个新的集合,该集合是 `set` 和 `others` 的并集。 这意味着,`newSet` 中的所有元素要么在 `set` 中,要么在 `others` 中。
func (*Set) X取补集 ¶
Complement 返回一个新的集合,该集合为 `set` 在 `full` 中的补集。 这意味着,新集合 `newSet` 中的所有元素都在 `full` 中但不在 `set` 中。
如果给定的集合 `full` 不是 `set` 的全集,则返回 `full` 与 `set` 之间的差集。
func (*Set) X读锁定_函数 ¶
func (set *Set) X读锁定_函数(f func(m map[interface{}]struct{}))
RLockFunc 通过回调函数 `f` 对读取进行加锁。
type StrSet ¶
type StrSet struct {
// contains filtered or unexported fields
}
func X创建文本 ¶
NewStrSet 创建并返回一个新的不包含重复项的集合。 参数`safe`用于指定是否在并发安全的情况下使用集合,默认为false。 这里,NewStrSet函数用于创建一个字符串集合,并确保其中的元素互不重复。该函数接受一个可选参数`safe`,它是一个布尔值,表示是否需要保证在并发环境下的安全性。如果不特别指定,那么默认情况下这个集合是不保证并发安全的。
func (StrSet) MarshalJSON ¶
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 ¶
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 ¶
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 ¶
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加入值并跳过已存在 ¶
AddIfNotExist 检查项目是否已存在于集合中, 若该项目不在集合中,则将其添加到集合并返回 true; 否则,不做任何操作并返回 false。
func (*StrSet) X加入值并跳过已存在_函数 ¶
AddIfNotExistFunc 检查项是否已存在于集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。
注意,函数 `f` 在无写入锁的情况下执行。
func (*StrSet) X加入值并跳过已存在_并发安全函数 ¶
AddIfNotExistFuncLock 检查项是否存在集合中, 如果该项不存在于集合中,并且函数 `f` 返回 true,则将该项添加到集合并返回 true; 否则,不执行任何操作并返回 false。
注意,函数 `f` 在无写入锁的情况下执行。
func (*StrSet) X取交集 ¶
Intersect 返回一个新的集合,它是从 `set` 到 `other` 的交集。 这意味着,`newSet` 中的所有元素都在 `set` 中,并且也在 `other` 中。
func (*StrSet) X取差集 ¶
Diff 返回一个新的集合,这个集合是 `set` 与 `other` 的差集。 这意味着,新集合 `newSet` 中的所有元素都在 `set` 中,但不在 `other` 中。
func (*StrSet) X取并集 ¶
Union 返回一个新的集合,该集合是 `set` 和 `other` 的并集。 这意味着,`newSet` 中的所有元素都在 `set` 或者 `other` 中。
func (*StrSet) X取补集 ¶
Complement 返回一个新的集合,该集合为 `set` 在 `full` 中的补集。 这意味着,新集合 `newSet` 中的所有元素都在 `full` 中但不在 `set` 中。
如果给定的集合 `full` 不是 `set` 的全集,则返回 `full` 与 `set` 之间的差集。
func (*StrSet) X是否存在并忽略大小写 ¶
ContainsI 检查某个值是否以不区分大小写的方式存在于集合中。 注意:它内部会遍历整个集合,以不区分大小写的方式进行比较。