map迭代支持终止迭代控制

This commit is contained in:
白茶清欢 2023-03-07 17:48:41 +08:00
parent e87648974d
commit e437ba87af
6 changed files with 25 additions and 9 deletions

View File

@ -7,5 +7,5 @@
// Date : 2023-03-07 17:27
package easymap
// IteratorFunc 迭代函数
type IteratorFunc func(key interface{}, value interface{})
// IteratorFunc 迭代函数 返回值为是否继续迭代true 继续迭代 false 终止后续迭代
type IteratorFunc func(key interface{}, value interface{}) bool

View File

@ -299,6 +299,9 @@ func (n *normal) Iterator(handleFunc IteratorFunc) {
n.RLock()
defer n.RUnlock()
for key, val := range n.data {
handleFunc(key, val)
if !handleFunc(key, val) {
// 终止迭代
break
}
}
}

View File

@ -165,9 +165,16 @@ func (s *segment) Iterator(handleFunc IteratorFunc) {
if nil == handleFunc {
return
}
isBreak := false
for i := 0; i < s.segment; i++ {
for k, v := range s.dataTable[i].GetAll() {
handleFunc(k, v)
if !handleFunc(k, v) {
isBreak = true
break
}
}
if isBreak {
break
}
}
}

View File

@ -244,7 +244,6 @@ func (s *syncMap) Iterator(handleFunc IteratorFunc) {
return
}
s.data.Range(func(key, value interface{}) bool {
handleFunc(key, value)
return true
return handleFunc(key, value)
})
}

View File

@ -141,9 +141,16 @@ func (s *segmentSync) Iterator(handleFunc IteratorFunc) {
if nil == handleFunc {
return
}
isBreak := false
for i := 0; i < s.segment; i++ {
for k, v := range s.dataTable[i].GetAll() {
handleFunc(k, v)
if !handleFunc(k, v) {
isBreak = true
break
}
}
if isBreak {
break
}
}
}