Skip to content

Commit

Permalink
Make zero value useful
Browse files Browse the repository at this point in the history
  • Loading branch information
Zamony committed May 8, 2024
1 parent fa5195c commit 069c126
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
4 changes: 0 additions & 4 deletions sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,8 +24,6 @@ Wait()

**Singleflight**
```
New() Group
Group.Do(key K, fun func() V) V
```

Expand Down
21 changes: 10 additions & 11 deletions sync/muxmap/map.go → sync/hatmap/map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package muxmap
package hatmap

import "sync"

Expand All @@ -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.
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions sync/muxmap/map_test.go → sync/hatmap/map_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()

Expand Down
9 changes: 2 additions & 7 deletions sync/singleflight/singleflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sync/singleflight/singleflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 069c126

Please sign in to comment.