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"
|
2018-09-05 18:34:41 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/internal/rwmutex"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type StringSet struct {
|
2018-09-05 18:34:41 +08:00
|
|
|
|
mu *rwmutex.RWMutex
|
2018-01-16 15:38:53 +08:00
|
|
|
|
m map[string]struct{}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-05 18:34:41 +08:00
|
|
|
|
func NewStringSet(safe...bool) *StringSet {
|
|
|
|
|
return &StringSet{
|
|
|
|
|
m : make(map[string]struct{}),
|
|
|
|
|
mu : rwmutex.New(safe...),
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 22:02:06 +08:00
|
|
|
|
// 给定回调函数对原始内容进行遍历,回调函数返回true表示继续遍历,否则停止遍历
|
2018-04-11 16:06:45 +08:00
|
|
|
|
func (this *StringSet) Iterator(f func (v string) bool) {
|
2018-09-06 19:16:08 +08:00
|
|
|
|
this.mu.RLock()
|
|
|
|
|
defer this.mu.RUnlock()
|
2018-01-16 15:38:53 +08:00
|
|
|
|
for k, _ := range this.m {
|
2018-04-11 16:06:45 +08:00
|
|
|
|
if !f(k) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-01-15 17:23:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 设置键
|
|
|
|
|
func (this *StringSet) Add(item string) *StringSet {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
|
|
|
|
this.m[item] = struct{}{}
|
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量添加设置键
|
|
|
|
|
func (this *StringSet) BatchAdd(items []string) *StringSet {
|
2018-02-24 16:57:37 +08:00
|
|
|
|
this.mu.Lock()
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
this.m[item] = struct{}{}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-02-24 16:57:37 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 键是否存在
|
|
|
|
|
func (this *StringSet) Contains(item string) bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
|
|
|
|
_, exists := this.m[item]
|
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return exists
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除键值对
|
|
|
|
|
func (this *StringSet) Remove(key string) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
|
|
|
|
delete(this.m, key)
|
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 大小
|
|
|
|
|
func (this *StringSet) Size() int {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
|
|
|
|
l := len(this.m)
|
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return l
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空set
|
|
|
|
|
func (this *StringSet) Clear() {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
|
|
|
|
this.m = make(map[string]struct{})
|
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为数组
|
|
|
|
|
func (this *StringSet) Slice() []string {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
|
|
|
|
ret := make([]string, len(this.m))
|
2017-11-23 10:21:28 +08:00
|
|
|
|
i := 0
|
2018-01-16 15:38:53 +08:00
|
|
|
|
for item := range this.m {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
ret[i] = item
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 转换为字符串
|
|
|
|
|
func (this *StringSet) String() string {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
return fmt.Sprint(this.Slice())
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-09-06 18:52:33 +08:00
|
|
|
|
|
2018-09-06 19:16:08 +08:00
|
|
|
|
func (this *StringSet) LockFunc(f func(m map[string]struct{})) {
|
2018-09-06 18:52:33 +08:00
|
|
|
|
this.mu.Lock(true)
|
|
|
|
|
defer this.mu.Unlock(true)
|
|
|
|
|
f(this.m)
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 19:16:08 +08:00
|
|
|
|
func (this *StringSet) RLockFunc(f func(m map[string]struct{})) {
|
2018-09-06 18:52:33 +08:00
|
|
|
|
this.mu.RLock(true)
|
|
|
|
|
defer this.mu.RUnlock(true)
|
|
|
|
|
f(this.m)
|
|
|
|
|
}
|