2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package gset
|
|
|
|
|
|
|
|
|
|
import (
|
2019-02-02 16:18:25 +08:00
|
|
|
|
"github.com/gogf/gf/g/internal/rwmutex"
|
|
|
|
|
"github.com/gogf/gf/g/util/gconv"
|
2019-02-02 15:16:45 +08:00
|
|
|
|
"strings"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IntSet struct {
|
2018-09-05 18:34:41 +08:00
|
|
|
|
mu *rwmutex.RWMutex
|
2018-01-16 15:38:53 +08:00
|
|
|
|
m map[int]struct{}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Create a set, which contains un-repeated items.
|
|
|
|
|
// The param <unsafe> used to specify whether using array with un-concurrent-safety,
|
|
|
|
|
// which is false in default, means concurrent-safe in default.
|
|
|
|
|
//
|
|
|
|
|
// 创建一个空的集合对象,参数unsafe用于指定是否用于非并发安全场景,默认为false,表示并发安全。
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func NewIntSet(unsafe...bool) *IntSet {
|
2018-09-05 18:34:41 +08:00
|
|
|
|
return &IntSet{
|
|
|
|
|
m : make(map[int]struct{}),
|
2019-01-12 23:36:22 +08:00
|
|
|
|
mu : rwmutex.New(unsafe...),
|
2018-09-05 18:34:41 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Iterate the set by given callback <f>,
|
|
|
|
|
// if <f> returns true then continue iterating; or false to stop.
|
|
|
|
|
//
|
|
|
|
|
// 给定回调函数对原始内容进行遍历,回调函数返回true表示继续遍历,否则停止遍历。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (set *IntSet) Iterator(f func (v int) bool) *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
for k, _ := range set.m {
|
2018-04-11 16:06:45 +08:00
|
|
|
|
if !f(k) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-01-15 17:23:22 +08:00
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return set
|
2018-01-15 17:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Add one or multiple items to the set.
|
|
|
|
|
//
|
|
|
|
|
// 添加元素项到集合中(支持多个).
|
|
|
|
|
func (set *IntSet) Add(item...int) *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Lock()
|
2019-02-02 15:16:45 +08:00
|
|
|
|
for _, v := range item {
|
|
|
|
|
set.m[v] = struct{}{}
|
|
|
|
|
}
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Unlock()
|
|
|
|
|
return set
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Check whether the set contains <item>.
|
|
|
|
|
//
|
|
|
|
|
// 键是否存在.
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) Contains(item int) bool {
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
_, exists := set.m[item]
|
|
|
|
|
set.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return exists
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Remove <item> from set.
|
|
|
|
|
//
|
|
|
|
|
// 删除元素项。
|
|
|
|
|
func (set *IntSet) Remove(item int) *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Lock()
|
2019-02-02 15:16:45 +08:00
|
|
|
|
delete(set.m, item)
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Unlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return set
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Get size of the set.
|
|
|
|
|
//
|
|
|
|
|
// 获得集合大小。
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) Size() int {
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
l := len(set.m)
|
|
|
|
|
set.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return l
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Clear the set.
|
|
|
|
|
//
|
|
|
|
|
// 清空集合。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (set *IntSet) Clear() *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Lock()
|
|
|
|
|
set.m = make(map[int]struct{})
|
|
|
|
|
set.mu.Unlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return set
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Get the copy of items from set as slice.
|
|
|
|
|
//
|
|
|
|
|
// 获得集合元素项列表.
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) Slice() []int {
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
ret := make([]int, len(set.m))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
i := 0
|
2019-01-31 21:15:39 +08:00
|
|
|
|
for k, _ := range set.m {
|
|
|
|
|
ret[i] = k
|
2017-11-23 10:21:28 +08:00
|
|
|
|
i++
|
|
|
|
|
}
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Join set items with a string.
|
|
|
|
|
//
|
|
|
|
|
// 使用glue字符串串连当前集合的元素项,构造成新的字符串返回。
|
|
|
|
|
func (set *IntSet) Join(glue string) string {
|
|
|
|
|
return strings.Join(gconv.Strings(set.Slice()), ",")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return set items as a string, which are joined by char ','.
|
|
|
|
|
//
|
|
|
|
|
// 使用glue字符串串连当前集合的元素项,构造成新的字符串返回。
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) String() string {
|
2019-02-02 15:16:45 +08:00
|
|
|
|
return set.Join(",")
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-09-06 18:52:33 +08:00
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Lock writing by callback function f.
|
|
|
|
|
//
|
|
|
|
|
// 使用自定义方法执行加锁修改操作。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (set *IntSet) LockFunc(f func(m map[int]struct{})) *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Lock(true)
|
|
|
|
|
defer set.mu.Unlock(true)
|
|
|
|
|
f(set.m)
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return set
|
2018-09-06 18:52:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Lock reading by callback function f.
|
|
|
|
|
//
|
|
|
|
|
// 使用自定义方法执行加锁读取操作。
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (set *IntSet) RLockFunc(f func(m map[int]struct{})) *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.RLock(true)
|
|
|
|
|
defer set.mu.RUnlock(true)
|
|
|
|
|
f(set.m)
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return set
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Check whether the two sets equal.
|
|
|
|
|
//
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// 判断两个集合是否相等.
|
|
|
|
|
func (set *IntSet) Equal(other *IntSet) bool {
|
|
|
|
|
if set == other {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
defer other.mu.RUnlock()
|
|
|
|
|
if len(set.m) != len(other.m) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for key := range set.m {
|
|
|
|
|
if _, ok := other.m[key]; !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Check whether the current set is sub-set of <other>.
|
|
|
|
|
//
|
2019-01-31 21:15:39 +08:00
|
|
|
|
// 判断当前集合是否为other集合的子集.
|
|
|
|
|
func (set *IntSet) IsSubsetOf(other *IntSet) bool {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
if set == other {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
defer other.mu.RUnlock()
|
2019-01-31 21:15:39 +08:00
|
|
|
|
for key := range set.m {
|
|
|
|
|
if _, ok := other.m[key]; !ok {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Returns a new set which is the union of <set> and <other>.
|
|
|
|
|
// Which means, all the items in <newSet> is in <set> or in <other>.
|
|
|
|
|
//
|
2019-02-01 17:30:23 +08:00
|
|
|
|
// 并集, 返回新的集合:属于set或属于others的元素为元素的集合.
|
|
|
|
|
func (set *IntSet) Union(others ... *IntSet) (newSet *IntSet) {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
2019-02-01 17:30:23 +08:00
|
|
|
|
for _, other := range others {
|
|
|
|
|
if set != other {
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
}
|
|
|
|
|
for k, v := range set.m {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
2019-02-01 17:30:23 +08:00
|
|
|
|
if set != other {
|
|
|
|
|
for k, v := range other.m {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if set != other {
|
|
|
|
|
other.mu.RUnlock()
|
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
2019-02-01 17:30:23 +08:00
|
|
|
|
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Returns a new set which is the difference set from <set> to <other>.
|
|
|
|
|
// Which means, all the items in <newSet> is in <set> and not in <other>.
|
|
|
|
|
//
|
2019-02-01 17:30:23 +08:00
|
|
|
|
// 差集, 返回新的集合: 属于set且不属于others的元素为元素的集合.
|
|
|
|
|
func (set *IntSet) Diff(others...*IntSet) (newSet *IntSet) {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
2019-02-01 17:30:23 +08:00
|
|
|
|
for _, other := range others {
|
|
|
|
|
if set == other {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
for k, v := range set.m {
|
|
|
|
|
if _, ok := other.m[k]; !ok {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
2019-02-01 17:30:23 +08:00
|
|
|
|
other.mu.RUnlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Returns a new set which is the intersection from <set> to <other>.
|
|
|
|
|
// Which means, all the items in <newSet> is in <set> and also in <other>.
|
|
|
|
|
//
|
2019-02-01 17:30:23 +08:00
|
|
|
|
// 交集, 返回新的集合: 属于set且属于others的元素为元素的集合.
|
|
|
|
|
func (set *IntSet) Intersect(others...*IntSet) (newSet *IntSet) {
|
2019-01-30 21:27:03 +08:00
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
2019-02-01 17:30:23 +08:00
|
|
|
|
for _, other := range others {
|
|
|
|
|
if set != other {
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
}
|
|
|
|
|
for k, v := range set.m {
|
|
|
|
|
if _, ok := other.m[k]; ok {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if set != other {
|
|
|
|
|
other.mu.RUnlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 15:16:45 +08:00
|
|
|
|
// Returns a new set which is the complement from <set> to <full>.
|
|
|
|
|
// Which means, all the items in <newSet> is in <full> and not in <set>.
|
|
|
|
|
//
|
2019-01-30 21:27:03 +08:00
|
|
|
|
// 补集, 返回新的集合: (前提: set应当为full的子集)属于全集full不属于集合set的元素组成的集合.
|
2019-01-31 21:15:39 +08:00
|
|
|
|
// 如果给定的full集合不是set的全集时,返回full与set的差集.
|
2019-01-30 21:27:03 +08:00
|
|
|
|
func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
|
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
2019-02-01 17:30:23 +08:00
|
|
|
|
if set != full {
|
|
|
|
|
full.mu.RLock()
|
|
|
|
|
defer full.mu.RUnlock()
|
|
|
|
|
}
|
2019-01-30 21:27:03 +08:00
|
|
|
|
for k, v := range full.m {
|
|
|
|
|
if _, ok := set.m[k]; !ok {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2019-02-01 17:30:23 +08:00
|
|
|
|
}
|