改进并发安全数据结构,完善示例程序

This commit is contained in:
john
2018-09-06 19:16:08 +08:00
parent f71f142912
commit 68a1a59f3a
15 changed files with 80 additions and 28 deletions

View File

@ -37,12 +37,12 @@ func (this *IntBoolMap) Clone() map[int]bool {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *IntBoolMap) Iterator(f func (k int, v bool) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 设置键值对

View File

@ -26,12 +26,12 @@ func NewIntIntMap(safe...bool) *IntIntMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *IntIntMap) Iterator(f func (k int, v int) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -24,12 +24,12 @@ func NewIntInterfaceMap(safe...bool) *IntInterfaceMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *IntInterfaceMap) Iterator(f func (k int, v interface{}) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -26,12 +26,12 @@ func NewIntStringMap(safe...bool) *IntStringMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *IntStringMap) Iterator(f func (k int, v string) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -26,12 +26,12 @@ func NewInterfaceInterfaceMap(safe...bool) *InterfaceInterfaceMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *InterfaceInterfaceMap) Iterator(f func (k interface{}, v interface{}) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -25,13 +25,13 @@ func NewStringBoolMap(safe...bool) *StringBoolMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringBoolMap) Iterator(f func (k string, v bool) bool) {
this.mu.RLock()
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -24,12 +24,12 @@ func NewStringIntMap(safe...bool) *StringIntMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringIntMap) Iterator(f func (k string, v int) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -26,12 +26,12 @@ func NewStringInterfaceMap(safe...bool) *StringInterfaceMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringInterfaceMap) Iterator(f func (k string, v interface{}) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -24,12 +24,12 @@ func NewStringStringMap(safe...bool) *StringStringMap {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringStringMap) Iterator(f func (k string, v string) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -25,12 +25,12 @@ func NewUintInterfaceMap(safe...bool) *UintInterfaceMap {
func (this *UintInterfaceMap) Iterator(f func (k uint, v interface{}) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, v := range this.m {
if !f(k, v) {
break
}
}
this.mu.RUnlock()
}
// 哈希表克隆

View File

@ -148,6 +148,7 @@ func (r *Ring) Unlink(n int) *Ring {
// 往后只读遍历回调函数返回true表示继续遍历否则退出遍历
func (r *Ring) RLockIteratorNext(f func(value interface{}) bool) {
r.mu.RLock()
defer r.mu.RUnlock()
if !f(r.ring.Value) {
return
}
@ -156,12 +157,12 @@ func (r *Ring) RLockIteratorNext(f func(value interface{}) bool) {
break
}
}
r.mu.RUnlock()
}
// 往前只读遍历回调函数返回true表示继续遍历否则退出遍历
func (r *Ring) RLockIteratorPrev(f func(value interface{}) bool) {
r.mu.RLock()
defer r.mu.RUnlock()
if !f(r.ring.Value) {
return
}
@ -170,12 +171,12 @@ func (r *Ring) RLockIteratorPrev(f func(value interface{}) bool) {
break
}
}
r.mu.RUnlock()
}
// 往后写遍历回调函数返回true表示继续遍历否则退出遍历
func (r *Ring) LockIteratorNext(f func(item *ring.Ring) bool) {
r.mu.Lock()
r.mu.RLock()
defer r.mu.RUnlock()
if !f(r.ring) {
return
}
@ -184,12 +185,12 @@ func (r *Ring) LockIteratorNext(f func(item *ring.Ring) bool) {
break
}
}
r.mu.Unlock()
}
// 往前写遍历回调函数返回true表示继续遍历否则退出遍历
func (r *Ring) LockIteratorPrev(f func(item *ring.Ring) bool) {
r.mu.Lock()
r.mu.RLock()
defer r.mu.RUnlock()
if !f(r.ring) {
return
}
@ -198,7 +199,6 @@ func (r *Ring) LockIteratorPrev(f func(item *ring.Ring) bool) {
break
}
}
r.mu.Unlock()
}
// 从当前位置,往后只读完整遍历,返回非空数据项值构成的数组

View File

@ -27,13 +27,13 @@ func NewIntSet(safe...bool) *IntSet {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *IntSet) Iterator(f func (v int) bool) {
this.mu.RLock()
this.mu.RLock()
defer this.mu.RUnlock()
for k, _ := range this.m {
if !f(k) {
break
}
}
this.mu.RUnlock()
}
// 设置键
@ -103,13 +103,13 @@ func (this *IntSet) String() string {
return fmt.Sprint(this.Slice())
}
func (this *IntSet) LockFunc(f func(map[int]struct{})) {
func (this *IntSet) LockFunc(f func(m map[int]struct{})) {
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
func (this *IntSet) RLockFunc(f func(map[int]struct{})) {
func (this *IntSet) RLockFunc(f func(m map[int]struct{})) {
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)

View File

@ -27,12 +27,12 @@ func NewInterfaceSet(safe...bool) *InterfaceSet {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *InterfaceSet) Iterator(f func (v interface{}) bool) {
this.mu.RLock()
defer this.mu.RUnlock()
for k, _ := range this.m {
if !f(k) {
break
}
}
this.mu.RUnlock()
}
// 添加
@ -101,13 +101,13 @@ func (this *InterfaceSet) String() string {
return fmt.Sprint(this.Slice())
}
func (this *InterfaceSet) LockFunc(f func(map[interface{}]struct{})) {
func (this *InterfaceSet) LockFunc(f func(m map[interface{}]struct{})) {
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
func (this *InterfaceSet) RLockFunc(f func(map[interface{}]struct{})) {
func (this *InterfaceSet) RLockFunc(f func(m map[interface{}]struct{})) {
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)

View File

@ -26,13 +26,13 @@ func NewStringSet(safe...bool) *StringSet {
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringSet) Iterator(f func (v string) bool) {
this.mu.RLock()
this.mu.RLock()
defer this.mu.RUnlock()
for k, _ := range this.m {
if !f(k) {
break
}
}
this.mu.RUnlock()
}
// 设置键
@ -102,13 +102,13 @@ func (this *StringSet) String() string {
return fmt.Sprint(this.Slice())
}
func (this *StringSet) LockFunc(f func(map[string]struct{})) {
func (this *StringSet) LockFunc(f func(m map[string]struct{})) {
this.mu.Lock(true)
defer this.mu.Unlock(true)
f(this.m)
}
func (this *StringSet) RLockFunc(f func(map[string]struct{})) {
func (this *StringSet) RLockFunc(f func(m map[string]struct{})) {
this.mu.RLock(true)
defer this.mu.RUnlock(true)
f(this.m)

View File

@ -0,0 +1,52 @@
package main
import (
"gitee.com/johng/gf/g/container/gset"
"fmt"
)
func main() {
// 创建一个非并发安全的集合对象
s := gset.New(false)
// 添加数据项
s.Add(1)
// 批量添加数据项
s.BatchAdd([]interface{}{1, 2, 3})
// 集合数据项大小
fmt.Println(s.Size())
// 集合中是否存在指定数据项
fmt.Println(s.Contains(2))
// 返回数据项slice
fmt.Println(s.Slice())
// 删除数据项
s.Remove(3)
// 遍历数据项
s.Iterator(func(v interface{}) bool {
fmt.Println("Iterator:", v)
return true
})
// 将集合转换为字符串
fmt.Println(s.String())
// 并发安全写锁操作
s.LockFunc(func(m map[interface{}]struct{}) {
m[4] = struct{}{}
})
// 并发安全读锁操作
s.RLockFunc(func(m map[interface{}]struct{}) {
fmt.Println(m)
})
// 清空集合
s.Clear()
fmt.Println(s.Size())
}