-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsyncmap.go
More file actions
103 lines (90 loc) · 2.41 KB
/
Copy pathsyncmap.go
File metadata and controls
103 lines (90 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"sync"
"time"
)
// SyncMap is a generic hash-map usable from multiple goroutines simultaneously.
//
// The API imitates the API of sync.Map.
//
// This is a dummy implementation to demonstrate the typing capabilities. This is not
// an example of an efficient implementation of a SyncMap.
type SyncMap(type K eq, type V) struct {
mu sync.Mutex
m map[K]V
}
// MakeSyncMap creates a new, empty SyncMap.
//
// I know it's better to make the zero value useful, this is just to better demonstrate
// the unnamed type parmeters syntax.
func MakeSyncMap(type K eq, type V) *SyncMap(K, V) {
return &SyncMap(K, V){
m: make(map[K]V),
}
}
// Delete deletes the value for a key.
func (sm *SyncMap(type K eq, type V)) Delete(key K) {
sm.mu.Lock()
delete(sm.m, key)
sm.mu.Unlock()
}
// Load returns the value stored in the map for a key, or nil if no value is present.
// The ok result indicates whether value was found in the map.
func (sm *SyncMap(type K eq, type V)) Load(key K) (value V, ok bool) {
sm.mu.Lock()
value, ok = sm.m[key]
sm.mu.Unlock()
return
}
// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
func (sm *SyncMap(type K eq, type V)) LoadOrStore(key K, value V) (actual V, loaded bool) {
sm.mu.Lock()
actual, loaded = sm.m[key]
if !loaded {
sm.m[key] = value
actual = value
}
sm.mu.Unlock()
return
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
func (sm *SyncMap(type K eq, type V)) Range(f func(key K, value V) bool) {
sm.mu.Lock()
for k, v := range sm.m {
if !f(k, v) {
break
}
}
sm.mu.Unlock()
}
// Store sets the value for a key.
func (sm *SyncMap(type K eq, type V)) Store(key K, value V) {
sm.mu.Lock()
sm.m[key] = value
sm.mu.Unlock()
}
func MarkAll(done chan<- bool, sm *SyncMap(type T eq, bool), values ...T) {
for _, val := range values {
time.Sleep(time.Second / 10)
sm.Store(val, true)
}
done <- true
}
func main() {
marked := MakeSyncMap(string, bool)
done := make(chan bool)
go MarkAll(done, marked, "A", "B", "C", "D")
go MarkAll(done, marked, "E", "F", "G", "H")
go MarkAll(done, marked, "I", "J", "K", "L")
for i := 0; i < 3; i++ {
<-done
}
marked.Range(func(key string, value bool) bool {
fmt.Println(key)
return true
})
}