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 gmap
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type IntBoolMap struct {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
m map[int]bool
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewIntBoolMap() *IntBoolMap {
|
|
|
|
|
return &IntBoolMap{
|
|
|
|
|
m: make(map[int]bool),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 哈希表克隆
|
|
|
|
|
func (this *IntBoolMap) Clone() *map[int]bool {
|
|
|
|
|
m := make(map[int]bool)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for k, v := range this.m {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return &m
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 22:02:06 +08:00
|
|
|
|
// 给定回调函数对原始内容进行遍历,回调函数返回true表示继续遍历,否则停止遍历
|
2018-04-11 16:06:45 +08:00
|
|
|
|
func (this *IntBoolMap) Iterator(f func (k int, v bool) bool) {
|
2018-02-07 17:55:52 +08:00
|
|
|
|
this.mu.RLock()
|
|
|
|
|
for k, v := range this.m {
|
2018-04-11 16:06:45 +08:00
|
|
|
|
if !f(k, v) {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-02-07 17:55:52 +08:00
|
|
|
|
}
|
|
|
|
|
this.mu.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 设置键值对
|
|
|
|
|
func (this *IntBoolMap) Set(key int, val bool) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
this.m[key] = val
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量设置键值对
|
|
|
|
|
func (this *IntBoolMap) BatchSet(m map[int]bool) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for k, v := range m {
|
|
|
|
|
this.m[k] = v
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取键值
|
2018-03-13 17:57:41 +08:00
|
|
|
|
func (this *IntBoolMap) Get(key int) bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
val, _ := this.m[key]
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 17:57:41 +08:00
|
|
|
|
// 获取键值,如果键值不存在则写入默认值
|
|
|
|
|
func (this *IntBoolMap) GetWithDefault(key int, value bool) bool {
|
|
|
|
|
this.mu.Lock()
|
|
|
|
|
val, ok := this.m[key]
|
|
|
|
|
if !ok {
|
|
|
|
|
this.m[key] = value
|
|
|
|
|
val = value
|
|
|
|
|
}
|
|
|
|
|
this.mu.Unlock()
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// 删除键值对
|
|
|
|
|
func (this *IntBoolMap) Remove(key int) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
delete(this.m, key)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 批量删除键值对
|
|
|
|
|
func (this *IntBoolMap) BatchRemove(keys []int) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
for _, key := range keys {
|
|
|
|
|
delete(this.m, key)
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回对应的键值,并删除该键值
|
|
|
|
|
func (this *IntBoolMap) GetAndRemove(key int) (bool) {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
val, exists := this.m[key]
|
|
|
|
|
if exists {
|
|
|
|
|
delete(this.m, key)
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回键列表
|
|
|
|
|
func (this *IntBoolMap) Keys() []int {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
keys := make([]int, 0)
|
|
|
|
|
for key, _ := range this.m {
|
|
|
|
|
keys = append(keys, key)
|
|
|
|
|
}
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return keys
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回值列表(注意是随机排序)
|
|
|
|
|
//func (this *IntBoolMap) Values() []bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
// this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// vals := make([]bool, 0)
|
|
|
|
|
// for _, val := range this.m {
|
|
|
|
|
// vals = append(vals, val)
|
|
|
|
|
// }
|
2018-01-16 15:38:53 +08:00
|
|
|
|
// this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
// return vals
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
// 是否存在某个键
|
|
|
|
|
func (this *IntBoolMap) Contains(key int) bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
_, exists := this.m[key]
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return exists
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 哈希表大小
|
|
|
|
|
func (this *IntBoolMap) Size() int {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2018-03-29 13:46:05 +08:00
|
|
|
|
length := len(this.m)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2018-03-29 13:46:05 +08:00
|
|
|
|
return length
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 哈希表是否为空
|
|
|
|
|
func (this *IntBoolMap) IsEmpty() bool {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RLock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
empty := (len(this.m) == 0)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.RUnlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return empty
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空哈希表
|
|
|
|
|
func (this *IntBoolMap) Clear() {
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Lock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
this.m = make(map[int]bool)
|
2018-01-16 15:38:53 +08:00
|
|
|
|
this.mu.Unlock()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 22:02:06 +08:00
|
|
|
|
// 使用自定义方法执行加锁修改操作
|
|
|
|
|
func (this *IntBoolMap) LockFunc(f func(m map[int]bool)) {
|
|
|
|
|
this.mu.Lock()
|
|
|
|
|
f(this.m)
|
|
|
|
|
this.mu.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 使用自定义方法执行加锁读取操作
|
|
|
|
|
func (this *IntBoolMap) RLockFunc(f func(m map[int]bool)) {
|
|
|
|
|
this.mu.RLock()
|
|
|
|
|
f(this.m)
|
|
|
|
|
this.mu.RUnlock()
|
|
|
|
|
}
|