1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2022-11-18 16:57:15 +08:00
parent cf30b4eb4c
commit 278c8b4cb7
7 changed files with 124 additions and 85 deletions

View File

@ -2,23 +2,21 @@ package maps
import (
"sync"
"golang.org/x/exp/constraints"
)
var _ Map[string, any] = &rw_map[string, any]{}
type rw_map[K constraints.Ordered, V any] struct {
type rw_map[K hashable, V any] struct {
m Map[K, V]
mu sync.RWMutex
}
func NewRWMap[K constraints.Ordered, V any](maps ...map[K]V) *rw_map[K, V] {
func NewRWMap[K hashable, V any](maps ...map[K]V) *rw_map[K, V] {
merged := Merge(maps...)
return &rw_map[K, V]{m: NewHashMap(merged)}
}
func newRWMap[K constraints.Ordered, V any](m Map[K, V]) *rw_map[K, V] {
func newRWMap[K hashable, V any](m Map[K, V]) *rw_map[K, V] {
return &rw_map[K, V]{m: m}
}
@ -74,28 +72,13 @@ func (m *rw_map[K, V]) Count() int {
return m.m.Count()
}
func (m *rw_map[K, V]) Iter() <-chan *Entry[K, V] {
m.mu.RLock()
defer m.mu.RUnlock()
// func (m *rw_map[K, V]) Iter() <-chan *Entry[K, V] {
// m.mu.RLock()
// defer m.mu.RUnlock()
return m.m.Iter()
}
// return m.m.Iter()
// }
func (m *rw_map[K, V]) ForEach(f func(K, V) bool) {
m.mu.RLock()
cloned := m.m.Clone()
m.mu.RUnlock()
cloned.ForEach(f)
}
func (m *rw_map[K, V]) Clone() Map[K, V] {
return newRWMap(m.m.Clone())
}
func (m *rw_map[K, V]) Clear() {
m.mu.Lock()
m.m.Clear()
m.mu.Unlock()
}