-
Notifications
You must be signed in to change notification settings - Fork 5
/
map_int.go
136 lines (117 loc) · 2.71 KB
/
map_int.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package syncutils
import (
"runtime"
"sync"
)
type KType interface {
int | int16 | int32 | int64 | uint | uint16 | uint32 | uint64 | uintptr
}
type VType interface {
any
}
type IntMap[K KType, V VType] struct {
count int
m []map[K]V
lockers []*sync.RWMutex
}
func NewIntMap[K KType, V VType]() *IntMap[K, V] {
var count = runtime.NumCPU() * 8
if count <= 0 {
count = 32
}
var m = []map[K]V{}
var lockers = []*sync.RWMutex{}
for i := 0; i < count; i++ {
m = append(m, map[K]V{})
lockers = append(lockers, &sync.RWMutex{})
}
return &IntMap[K, V]{
count: count,
m: m,
lockers: lockers,
}
}
func (this *IntMap[K, V]) Put(k K, v V) {
var index = this.index(k)
this.lockers[index].Lock()
this.m[index][k] = v
this.lockers[index].Unlock()
}
func (this *IntMap[K, V]) PutCompact(k K, v V, compactFunc func(oldV V, newV V) V) {
var index = this.index(k)
this.lockers[index].Lock()
// 再次检查是否已经存在,如果已经存在则合并
oldV, ok := this.m[index][k]
if ok {
this.m[index][k] = compactFunc(oldV, v)
} else {
this.m[index][k] = v
}
this.lockers[index].Unlock()
}
func (this *IntMap[K, V]) Has(k K) bool {
var index = this.index(k)
this.lockers[index].RLock()
_, ok := this.m[index][k]
this.lockers[index].RUnlock()
return ok
}
func (this *IntMap[K, V]) Get(k K) (value V) {
var index = this.index(k)
this.lockers[index].RLock()
value = this.m[index][k]
this.lockers[index].RUnlock()
return
}
func (this *IntMap[K, V]) GetOk(k K) (value V, ok bool) {
var index = this.index(k)
this.lockers[index].RLock()
value, ok = this.m[index][k]
this.lockers[index].RUnlock()
return
}
func (this *IntMap[K, V]) Delete(k K) {
var index = this.index(k)
this.lockers[index].Lock()
delete(this.m[index], k)
this.lockers[index].Unlock()
}
func (this *IntMap[K, V]) DeleteUnsafe(k K) {
var index = this.index(k)
delete(this.m[index], k)
}
func (this *IntMap[K, V]) Len() int {
var l int
for i := 0; i < this.count; i++ {
this.lockers[i].RLock()
l += len(this.m[i])
this.lockers[i].RUnlock()
}
return l
}
func (this *IntMap[K, V]) ForEachRead(iterator func(k K, v V)) {
for i := 0; i < this.count; i++ {
this.lockers[i].RLock()
for k, v := range this.m[i] {
iterator(k, v)
}
this.lockers[i].RUnlock()
}
}
func (this *IntMap[K, V]) ForEachWrite(iterator func(k K, v V)) {
for i := 0; i < this.count; i++ {
this.lockers[i].Lock()
for k, v := range this.m[i] {
iterator(k, v)
}
this.lockers[i].Unlock()
}
}
func (this *IntMap[K, V]) index(k K) int {
var index = int(k % K(this.count))
if index < 0 {
index = -index
}
return index
}