diff --git a/sync/README.md b/sync/README.md index 73a698f..49cf534 100644 --- a/sync/README.md +++ b/sync/README.md @@ -6,8 +6,6 @@ Additional synchronization primitives: **Thread-safe map** ``` -New() Map - Map.Len() int Map.Get(key K) (V, bool) Map.Set(key K, value V) @@ -26,8 +24,6 @@ Wait() **Singleflight** ``` -New() Group - Group.Do(key K, fun func() V) V ``` diff --git a/sync/muxmap/map.go b/sync/hatmap/map.go similarity index 88% rename from sync/muxmap/map.go rename to sync/hatmap/map.go index 7520727..d82e6b3 100644 --- a/sync/muxmap/map.go +++ b/sync/hatmap/map.go @@ -1,4 +1,4 @@ -package muxmap +package hatmap import "sync" @@ -10,16 +10,9 @@ type Map[K comparable, V any] struct { mu sync.RWMutex // protects data } -// New creates a new map. Optional parameter is a size hint. -func New[K comparable, V any](size ...int) Map[K, V] { - switch len(size) { - case 0: - return Map[K, V]{data: make(map[K]V, 0)} - case 1: - return Map[K, V]{data: make(map[K]V, size[0])} - default: - panic("ambiguous map size") - } +// New creates a new map with size hint. +func New[K comparable, V any](size int) Map[K, V] { + return Map[K, V]{data: make(map[K]V, size)} } // Len returns the number of elements in the map. @@ -33,6 +26,9 @@ func (m *Map[K, V]) Len() int { // Set sets the value by the given key. func (m *Map[K, V]) Set(key K, value V) { m.mu.Lock() + if m.data == nil { + m.data = make(map[K]V) + } m.data[key] = value m.mu.Unlock() } @@ -63,6 +59,9 @@ func (m *Map[K, V]) SetIf(key K, cond func(value V, exists bool) bool, valfunc f } value = valfunc(value) + if m.data == nil { + m.data = make(map[K]V) + } m.data[key] = value return value, true } diff --git a/sync/muxmap/map_test.go b/sync/hatmap/map_test.go similarity index 93% rename from sync/muxmap/map_test.go rename to sync/hatmap/map_test.go index 26f553a..2fe74a6 100644 --- a/sync/muxmap/map_test.go +++ b/sync/hatmap/map_test.go @@ -1,17 +1,17 @@ -package muxmap_test +package hatmap_test import ( "reflect" "sync" "testing" - "github.com/Zamony/go/sync/muxmap" + "github.com/Zamony/go/sync/hatmap" ) func TestMapSetGet(t *testing.T) { t.Parallel() - m := muxmap.New[string, int]() + var m hatmap.Map[string, int] m.Set("a", 1) value, ok := m.SetIf("b", func(value int, exists bool) bool { @@ -59,7 +59,7 @@ func TestMapSetGet(t *testing.T) { func TestMapSetDelete(t *testing.T) { t.Parallel() - m := muxmap.New[string, int]() + m := hatmap.New[string, int](3) m.Set("a", 1) m.Set("b", 2) m.Set("c", 3) @@ -86,7 +86,7 @@ func TestMapSetDelete(t *testing.T) { func TestMapForEach(t *testing.T) { t.Parallel() - m := muxmap.New[string, int]() + var m hatmap.Map[string, int] m.Set("a", 1) m.Set("b", 2) m.Set("c", 3) @@ -111,7 +111,7 @@ func TestMapConcurrent(t *testing.T) { t.Parallel() const key = "a" - m := muxmap.New[string, int]() + var m hatmap.Map[string, int] var wg sync.WaitGroup defer wg.Wait() diff --git a/sync/singleflight/singleflight.go b/sync/singleflight/singleflight.go index 3b029a7..2b43090 100644 --- a/sync/singleflight/singleflight.go +++ b/sync/singleflight/singleflight.go @@ -3,7 +3,7 @@ package singleflight import ( "sync/atomic" - "github.com/Zamony/go/sync/muxmap" + "github.com/Zamony/go/sync/hatmap" ) type result[T any] struct { @@ -16,12 +16,7 @@ type result[T any] struct { // // Group must not be copied after first use. type Group[K comparable, V any] struct { - results muxmap.Map[K, *result[V]] -} - -// New creates a new singleflight Group. -func New[K comparable, V any]() Group[K, V] { - return Group[K, V]{muxmap.New[K, *result[V]]()} + results hatmap.Map[K, *result[V]] } // Do executes and returns the results of the given function, making diff --git a/sync/singleflight/singleflight_test.go b/sync/singleflight/singleflight_test.go index b340d12..4292860 100644 --- a/sync/singleflight/singleflight_test.go +++ b/sync/singleflight/singleflight_test.go @@ -15,7 +15,7 @@ func TestSingleFlight(t *testing.T) { var ncalls int64 var wg sync.WaitGroup defer wg.Wait() - single := singleflight.New[string, int64]() + single := singleflight.Group[string, int64]{} for i := 0; i < 5; i++ { wg.Add(1) go func() {