2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// 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,
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
//
|
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package gset
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2019-01-12 23:36:22 +08:00
|
|
|
|
"gitee.com/johng/gf/g/internal/rwmutex"
|
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-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
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 22:02:06 +08:00
|
|
|
|
// 给定回调函数对原始内容进行遍历,回调函数返回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
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 设置键
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) Add(item int) *IntSet {
|
|
|
|
|
set.mu.Lock()
|
|
|
|
|
set.m[item] = struct{}{}
|
|
|
|
|
set.mu.Unlock()
|
|
|
|
|
return set
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量添加设置键
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) BatchAdd(items []int) *IntSet {
|
|
|
|
|
set.mu.Lock()
|
2018-02-24 16:57:37 +08:00
|
|
|
|
for _, item := range items {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.m[item] = struct{}{}
|
2018-02-24 16:57:37 +08:00
|
|
|
|
}
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Unlock()
|
|
|
|
|
return set
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 键是否存在
|
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-01-30 21:27:03 +08:00
|
|
|
|
// 删除元素项
|
|
|
|
|
func (set *IntSet) Remove(key int) *IntSet {
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.Lock()
|
|
|
|
|
delete(set.m, key)
|
|
|
|
|
set.mu.Unlock()
|
2019-01-30 21:27:03 +08:00
|
|
|
|
return set
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 大小
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空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-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-12 23:36:22 +08:00
|
|
|
|
for item := range set.m {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
ret[i] = item
|
|
|
|
|
i++
|
|
|
|
|
}
|
2019-01-12 23:36:22 +08:00
|
|
|
|
set.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为字符串
|
2019-01-12 23:36:22 +08:00
|
|
|
|
func (set *IntSet) String() string {
|
|
|
|
|
return fmt.Sprint(set.Slice())
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-09-06 18:52:33 +08:00
|
|
|
|
|
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-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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断两个集合是否相等.
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断other集合是否为当前集合的子集.
|
|
|
|
|
func (set *IntSet) IsSubset(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 other.m {
|
|
|
|
|
if _, ok := set.m[key]; !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 并集, 返回新的集合:属于set或属于other的元素为元素的集合.
|
|
|
|
|
func (set *IntSet) Union(other *IntSet) (newSet *IntSet) {
|
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
if set != other {
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
defer other.mu.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
for k, v := range set.m {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
if set != other {
|
|
|
|
|
for k, v := range other.m {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 差集, 返回新的集合: 属于set且不属于other的元素为元素的集合.
|
|
|
|
|
func (set *IntSet) Diff(other *IntSet) (newSet *IntSet) {
|
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
if set == other {
|
|
|
|
|
return newSet
|
|
|
|
|
}
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
defer other.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
for k, v := range set.m {
|
|
|
|
|
if _, ok := other.m[k]; !ok {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 交集, 返回新的集合: 属于set且属于other的元素为元素的集合.
|
|
|
|
|
func (set *IntSet) Inter(other *IntSet) (newSet *IntSet) {
|
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
if set != other {
|
|
|
|
|
other.mu.RLock()
|
|
|
|
|
defer other.mu.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
for k, v := range set.m {
|
|
|
|
|
if _, ok := other.m[k]; ok {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 补集, 返回新的集合: (前提: set应当为full的子集)属于全集full不属于集合set的元素组成的集合.
|
|
|
|
|
func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
|
|
|
|
|
newSet = NewIntSet(true)
|
|
|
|
|
set.mu.RLock()
|
|
|
|
|
defer set.mu.RUnlock()
|
|
|
|
|
full.mu.RLock()
|
|
|
|
|
defer full.mu.RUnlock()
|
|
|
|
|
for k, v := range full.m {
|
|
|
|
|
if _, ok := set.m[k]; !ok {
|
|
|
|
|
newSet.m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2018-09-06 18:52:33 +08:00
|
|
|
|
}
|