Compare commits

..

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

29
map.go
View File

@ -96,29 +96,24 @@ func (m *Map[K, V]) Values() []V {
// Do calls f with the underlying primitive map. // Do calls f with the underlying primitive map.
// Be aware that this locks m for write access // Be aware that this locks m for write access
// so Do can be used for reading as well as modifiying m. // so Do can be used for reading as well as modifiying m.
// After f returns, the map will be shallow-copied in order to prevent
// clueless programmers from storing the map and modifying it afterwards.
// If you need that little bit of optimization, use DoUnsafe instead
func (m *Map[K, V]) Do(f func(m map[K]V)) { func (m *Map[K, V]) Do(f func(m map[K]V)) {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
f(m.data) f(m.data)
nm := map[K]V{}
for key, value := range m.data {
nm[key] = value
}
m.data = nm
} }
// DoWithError calls f with the underlying primitive map and returns its error. // DoUnsafe behaves like Do but does not create a shallow-copy of m
// Be aware that this locks m for write access func (m *Map[K, V]) DoUnsafe(f func(m map[K]V)) {
// so Do can be used for reading as well as modifiying m.
func (m *Map[K, V]) DoWithError(f func(m map[K]V) error) error {
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
return f(m.data) f(m.data)
}
// Clone returns a copy of the underlying map data
func (m *Map[K, V]) Clone() map[K]V {
m.mutex.RLock()
defer m.mutex.RUnlock()
c := map[K]V{}
for key, value := range m.data {
c[key] = value
}
return c
} }