-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmap.go
More file actions
176 lines (154 loc) · 2.96 KB
/
cmap.go
File metadata and controls
176 lines (154 loc) · 2.96 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cmap
import (
"cmp"
"strconv"
"strings"
"sync"
"github.com/emirpasic/gods/v2/maps"
)
// Map 并发安全的Map实现
type Map[K cmp.Ordered, V any] struct {
shards []shard[K, V]
mask uint32
dirty bool
mu *sync.RWMutex // 用于保护dirty字段
hasher hasher[K] // 哈希器
opts *Options
}
// shard 分片结构
type shard[K cmp.Ordered, V any] struct {
m maps.Map[K, V]
mu *sync.RWMutex
opts *Options
}
// Put 插入键值对
func (m *Map[K, V]) Put(key K, value V) {
sh := m.getShard(key)
sh.mu.Lock()
sh.m.Put(key, value)
sh.mu.Unlock()
m.mu.Lock()
m.dirty = true
m.mu.Unlock()
}
// Get 获取值
func (m *Map[K, V]) Get(key K) (value V, found bool) {
sh := m.getShard(key)
sh.mu.RLock()
value, found = sh.m.Get(key)
sh.mu.RUnlock()
return
}
// Remove 删除键
func (m *Map[K, V]) Remove(key K) {
sh := m.getShard(key)
sh.mu.Lock()
_, ok := sh.m.Get(key)
if ok {
sh.m.Remove(key)
}
sh.mu.Unlock()
if ok {
m.mu.Lock()
m.dirty = true
m.mu.Unlock()
}
}
// Empty 检查是否为空
func (m *Map[K, V]) Empty() bool {
for i := range m.shards {
m.shards[i].mu.RLock()
empty := m.shards[i].m.Empty()
m.shards[i].mu.RUnlock()
if !empty {
return false
}
}
return true
}
// Size 获取大小
func (m *Map[K, V]) Size() int {
size := 0
for i := range m.shards {
m.shards[i].mu.RLock()
size += m.shards[i].m.Size()
m.shards[i].mu.RUnlock()
}
return size
}
// Clear 清空映射
func (m *Map[K, V]) Clear() {
for i := range m.shards {
m.shards[i].mu.Lock()
m.shards[i].m.Clear()
m.shards[i].mu.Unlock()
}
m.mu.Lock()
m.dirty = true
m.mu.Unlock()
}
// Keys 获取所有键
func (m *Map[K, V]) Keys() []K {
var keys []K
for i := range m.shards {
m.shards[i].mu.RLock()
shardKeys := m.shards[i].m.Keys()
keys = append(keys, shardKeys...)
m.shards[i].mu.RUnlock()
}
return keys
}
// Values 获取所有值
func (m *Map[K, V]) Values() []V {
var values []V
for i := range m.shards {
m.shards[i].mu.RLock()
shardValues := m.shards[i].m.Values()
values = append(values, shardValues...)
m.shards[i].mu.RUnlock()
}
return values
}
// String 字符串表示
func (m *Map[K, V]) String() string {
var b strings.Builder
b.WriteString("CMap:\n")
for i, sh := range m.shards {
sh.mu.RLock()
if !sh.m.Empty() {
b.WriteString("- Shard ")
b.WriteString(strconv.Itoa(i))
b.WriteString(": ")
b.WriteString(sh.m.String())
b.WriteString("\n")
}
sh.mu.RUnlock()
}
return b.String()
}
// IsDirty 检查映射是否被修改过
func (m *Map[K, V]) IsDirty() (dirty bool) {
m.mu.RLock()
defer m.mu.RUnlock()
dirty = m.dirty
return
}
// getShard 获取键对应的分片
func (m *Map[K, V]) getShard(key K) *shard[K, V] {
hash := m.hasher.Hash(key)
return &m.shards[hash&m.mask]
}
// roundUpToPowerOf2 向上取整到2的幂
func roundUpToPowerOf2(n uint32) uint32 {
if n == 0 {
return 1
}
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n++
return n
}