gf/g/container/gset/gset_int_set.go

116 lines
2.1 KiB
Go
Raw Normal View History

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"
"gitee.com/johng/gf/g/internal/rwmutex"
2017-11-23 10:21:28 +08:00
)
type IntSet struct {
mu *rwmutex.RWMutex
m map[int]struct{}
2017-11-23 10:21:28 +08:00
}
func NewIntSet(unsafe...bool) *IntSet {
return &IntSet{
m : make(map[int]struct{}),
mu : rwmutex.New(unsafe...),
}
2017-11-23 10:21:28 +08:00
}
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (set *IntSet) Iterator(f func (v int) bool) {
set.mu.RLock()
defer set.mu.RUnlock()
for k, _ := range set.m {
2018-04-11 16:06:45 +08:00
if !f(k) {
break
}
}
}
2017-11-23 10:21:28 +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
}
// 批量添加设置键
func (set *IntSet) BatchAdd(items []int) *IntSet {
set.mu.Lock()
2018-02-24 16:57:37 +08:00
for _, item := range items {
set.m[item] = struct{}{}
2018-02-24 16:57:37 +08:00
}
set.mu.Unlock()
return set
2017-11-23 10:21:28 +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
}
// 删除键值对
func (set *IntSet) Remove(key int) {
set.mu.Lock()
delete(set.m, key)
set.mu.Unlock()
2017-11-23 10:21:28 +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
func (set *IntSet) Clear() {
set.mu.Lock()
set.m = make(map[int]struct{})
set.mu.Unlock()
2017-11-23 10:21:28 +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
for item := range set.m {
2017-11-23 10:21:28 +08:00
ret[i] = item
i++
}
set.mu.RUnlock()
2017-11-23 10:21:28 +08:00
return ret
}
// 转换为字符串
func (set *IntSet) String() string {
return fmt.Sprint(set.Slice())
2017-11-23 10:21:28 +08:00
}
func (set *IntSet) LockFunc(f func(m map[int]struct{})) {
set.mu.Lock(true)
defer set.mu.Unlock(true)
f(set.m)
}
func (set *IntSet) RLockFunc(f func(m map[int]struct{})) {
set.mu.RLock(true)
defer set.mu.RUnlock(true)
f(set.m)
}