Compare commits

..

No commits in common. "main" and "v0.0.4" have entirely different histories.
main ... v0.0.4

2 changed files with 10 additions and 16 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module git.tordarus.net/tordarus/cmap module git.tordarus.net/tordarus/cmap
go 1.24.1 go 1.22.0

24
map.go
View File

@ -1,9 +1,6 @@
package cmap package cmap
import ( import "sync"
"iter"
"sync"
)
// Map represents a map safe for concurrent use // Map represents a map safe for concurrent use
type Map[K comparable, V any] struct { type Map[K comparable, V any] struct {
@ -58,18 +55,15 @@ func (m *Map[K, V]) Count() int {
return len(m.data) return len(m.data)
} }
// Iterate returns a Go iterator to make for-range loops possible. // Iter calls f for every key-value pair stored in m.
// Be aware that the whole map is locked during iteration. // Be aware that this locks m for write access
func (m *Map[K, V]) Iterate() iter.Seq2[K, V] { // as pointer types could be modified in f
return func(yield func(K, V) bool) { func (m *Map[K, V]) Iter(f func(key K, value V)) {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
for key, value := range m.data { for key, value := range m.data {
if !yield(key, value) { f(key, value)
return
}
}
} }
} }