-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmap.go
More file actions
39 lines (28 loc) · 719 Bytes
/
map.go
File metadata and controls
39 lines (28 loc) · 719 Bytes
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
package lrc
import "github.com/csimplestring/go-left-right/primitive"
// LRMap utilises the left-right pattern to handle concurrent read-write.
type LRMap struct {
*primitive.LeftRightPrimitive[map[int]int]
left map[int]int
right map[int]int
}
func newIntMap() *LRMap {
m := &LRMap{
left: make(map[int]int),
right: make(map[int]int),
}
m.LeftRightPrimitive = primitive.New[map[int]int]()
return m
}
func (lr *LRMap) Get(k int) (val int, exist bool) {
lr.ApplyReadFn(lr.left, lr.right, func(instance map[int]int) {
val, exist = instance[k]
})
return
}
func (lr *LRMap) Put(key, val int) {
lr.ApplyWriteFn(lr.left, lr.right, func(instance map[int]int) {
m := instance
m[key] = val
})
}