diff --git a/g/container/gset/int_set.go b/g/container/gset/int_set.go index a6206999c..348df67d6 100644 --- a/g/container/gset/int_set.go +++ b/g/container/gset/int_set.go @@ -102,3 +102,15 @@ func (this *IntSet) Slice() []int { func (this *IntSet) String() string { return fmt.Sprint(this.Slice()) } + +func (this *IntSet) LockFunc(f func(map[int]struct{})) { + this.mu.Lock(true) + defer this.mu.Unlock(true) + f(this.m) +} + +func (this *IntSet) RLockFunc(f func(map[int]struct{})) { + this.mu.RLock(true) + defer this.mu.RUnlock(true) + f(this.m) +} \ No newline at end of file diff --git a/g/container/gset/interface_set.go b/g/container/gset/interface_set.go index 4901b71e4..ae837e812 100644 --- a/g/container/gset/interface_set.go +++ b/g/container/gset/interface_set.go @@ -100,3 +100,15 @@ func (this *InterfaceSet) Slice() []interface{} { func (this *InterfaceSet) String() string { return fmt.Sprint(this.Slice()) } + +func (this *InterfaceSet) LockFunc(f func(map[interface{}]struct{})) { + this.mu.Lock(true) + defer this.mu.Unlock(true) + f(this.m) +} + +func (this *InterfaceSet) RLockFunc(f func(map[interface{}]struct{})) { + this.mu.RLock(true) + defer this.mu.RUnlock(true) + f(this.m) +} diff --git a/g/container/gset/string_set.go b/g/container/gset/string_set.go index 0b3ff6104..1819508a5 100644 --- a/g/container/gset/string_set.go +++ b/g/container/gset/string_set.go @@ -101,3 +101,15 @@ func (this *StringSet) Slice() []string { func (this *StringSet) String() string { return fmt.Sprint(this.Slice()) } + +func (this *StringSet) LockFunc(f func(map[string]struct{})) { + this.mu.Lock(true) + defer this.mu.Unlock(true) + f(this.m) +} + +func (this *StringSet) RLockFunc(f func(map[string]struct{})) { + this.mu.RLock(true) + defer this.mu.RUnlock(true) + f(this.m) +} diff --git a/g/container/gset/uint_set.go b/g/container/gset/uint_set.go deleted file mode 100644 index 28b1610dc..000000000 --- a/g/container/gset/uint_set.go +++ /dev/null @@ -1,102 +0,0 @@ -// 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. -// - -package gset - -import ( - "fmt" - "gitee.com/johng/gf/g/container/internal/rwmutex" -) - -type UintSet struct { - mu *rwmutex.RWMutex - m map[uint]struct{} -} - -func NewUintSet(safe...bool) *UintSet { - return &UintSet{ - m : make(map[uint]struct{}), - mu : rwmutex.New(safe...), - } -} - -// 给定回调函数对原始内容进行遍历,回调函数返回true表示继续遍历,否则停止遍历 -func (this *UintSet) Iterator(f func (v uint) bool) { - this.mu.RLock() - for k, _ := range this.m { - if !f(k) { - break - } - } - this.mu.RUnlock() -} - -// 添加 -func (this *UintSet) Add(item uint) *UintSet { - this.mu.Lock() - this.m[item] = struct{}{} - this.mu.Unlock() - return this -} - -// 批量添加 -func (this *UintSet) BatchAdd(items []uint) *UintSet { - this.mu.Lock() - for _, item := range items { - this.m[item] = struct{}{} - } - this.mu.Unlock() - return this -} - -// 键是否存在 -func (this *UintSet) Contains(item uint) bool { - this.mu.RLock() - _, exists := this.m[item] - this.mu.RUnlock() - return exists -} - -// 删除键值对 -func (this *UintSet) Remove(key uint) { - this.mu.Lock() - delete(this.m, key) - this.mu.Unlock() -} - -// 大小 -func (this *UintSet) Size() int { - this.mu.RLock() - l := len(this.m) - this.mu.RUnlock() - return l -} - -// 清空set -func (this *UintSet) Clear() { - this.mu.Lock() - this.m = make(map[uint]struct{}) - this.mu.Unlock() -} - -// 转换为数组 -func (this *UintSet) Slice() []uint { - this.mu.RLock() - i := 0 - ret := make([]uint, len(this.m)) - for item := range this.m { - ret[i] = item - i++ - } - this.mu.RUnlock() - return ret -} - -// 转换为字符串 -func (this *UintSet) String() string { - return fmt.Sprint(this.Slice()) -}