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
go 1.24.1
go 1.22.0

18
map.go
View File

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