Skip to content

Commit

Permalink
Add DeleteIf to CopyOnWriteMap
Browse files Browse the repository at this point in the history
  • Loading branch information
plorenz committed Nov 12, 2024
1 parent 2ee5fb4 commit 1603635
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions concurrenz/copy_on_write_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ func (self *CopyOnWriteMap[K, V]) Clear() {
defer self.lock.Unlock()
self.value.Store(map[K]V{})
}

func (self *CopyOnWriteMap[K, V]) DeleteIf(f func(key K, val V) bool) bool {
self.lock.Lock()
defer self.lock.Unlock()

matched := false
var current = self.value.Load()
mapCopy := map[K]V{}
for k, v := range current {
if !f(k, v) {
mapCopy[k] = v
} else {
matched = true
}
}
self.value.Store(mapCopy)
return matched
}

0 comments on commit 1603635

Please sign in to comment.