-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.go
More file actions
148 lines (123 loc) · 4.49 KB
/
constructors.go
File metadata and controls
148 lines (123 loc) · 4.49 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
package cmap
import (
"cmp"
"runtime"
"sync"
"github.com/emirpasic/gods/v2/maps"
"github.com/emirpasic/gods/v2/maps/hashmap"
"github.com/emirpasic/gods/v2/maps/linkedhashmap"
"github.com/emirpasic/gods/v2/maps/treemap"
)
// ---------------------------------------------------------------------------------------------------------------------
// New 创建默认的并发映射(使用HashMap作为底层实现)
func New[K cmp.Ordered, V any](options ...Option) *Map[K, V] {
return NewHashMap[K, V](options...)
}
// ---------------------------------------------------------------------------------------------------------------------
// NewHashMap 创建HashMap类型的并发映射
func NewHashMap[K cmp.Ordered, V any](options ...Option) *Map[K, V] {
return createMap(func() maps.Map[K, V] {
return hashmap.New[K, V]()
}, options...)
}
// NewTreeMap 创建TreeMap类型的并发映射
func NewTreeMap[K cmp.Ordered, V any](options ...Option) *Map[K, V] {
return createMap(func() maps.Map[K, V] {
return treemap.New[K, V]()
}, options...)
}
// NewLinkedHashMap 创建LinkedHashMap类型的并发映射
func NewLinkedHashMap[K cmp.Ordered, V any](options ...Option) *Map[K, V] {
return createMap(func() maps.Map[K, V] {
return linkedhashmap.New[K, V]()
}, options...)
}
// ---------------------------------------------------------------------------------------------------------------------
// NewStringHashMap 创建使用string键的HashMap
func NewStringHashMap[V any](options ...Option) *Map[string, V] {
return createMap(func() maps.Map[string, V] {
return hashmap.New[string, V]()
}, options...)
}
// NewStringTreeMap 创建使用string键的TreeMap
func NewStringTreeMap[V any](options ...Option) *Map[string, V] {
return createMap(func() maps.Map[string, V] {
return treemap.New[string, V]()
}, options...)
}
// NewStringLinkedHashMap 创建使用string键的LinkedHashMap
func NewStringLinkedHashMap[V any](options ...Option) *Map[string, V] {
return createMap(func() maps.Map[string, V] {
return linkedhashmap.New[string, V]()
}, options...)
}
// ---------------------------------------------------------------------------------------------------------------------
// NewIntHashMap 创建使用int键的HashMap
func NewIntHashMap[V any](options ...Option) *Map[int, V] {
return createMap(func() maps.Map[int, V] {
return hashmap.New[int, V]()
}, options...)
}
// NewIntTreeMap 创建使用int键的TreeMap
func NewIntTreeMap[V any](options ...Option) *Map[int, V] {
return createMap(func() maps.Map[int, V] {
return treemap.New[int, V]()
}, options...)
}
// NewIntLinkedHashMap 创建使用int键的LinkedHashMap
func NewIntLinkedHashMap[V any](options ...Option) *Map[int, V] {
return createMap(func() maps.Map[int, V] {
return linkedhashmap.New[int, V]()
}, options...)
}
// ---------------------------------------------------------------------------------------------------------------------
// NewInt64HashMap 创建使用int64键的HashMap
func NewInt64HashMap[V any](options ...Option) *Map[int64, V] {
return createMap(func() maps.Map[int64, V] {
return hashmap.New[int64, V]()
}, options...)
}
// NewInt64TreeMap 创建使用int64键的TreeMap
func NewInt64TreeMap[V any](options ...Option) *Map[int64, V] {
return createMap(func() maps.Map[int64, V] {
return treemap.New[int64, V]()
}, options...)
}
// NewInt64LinkedHashMap 创建使用int64键的LinkedHashMap
func NewInt64LinkedHashMap[V any](options ...Option) *Map[int64, V] {
return createMap(func() maps.Map[int64, V] {
return linkedhashmap.New[int64, V]()
}, options...)
}
// ---------------------------------------------------------------------------------------------------------------------
// createMap is a generic helper function that creates a new Map instance
func createMap[K cmp.Ordered, V any](
createUnderlyingMap func() maps.Map[K, V],
options ...Option,
) *Map[K, V] {
opts := &Options{
ShardCount: uint32(runtime.NumCPU() * 16),
Serializer: JsonSerializer(),
}
for _, option := range options {
option(opts)
}
shardCount := roundUpToPowerOf2(opts.ShardCount)
shards := make([]shard[K, V], shardCount)
for i := range shards {
shards[i] = shard[K, V]{
m: createUnderlyingMap(),
mu: &sync.RWMutex{},
opts: opts,
}
}
return &Map[K, V]{
shards: shards,
mask: shardCount - 1,
dirty: false,
mu: &sync.RWMutex{},
hasher: getHasher[K](),
opts: opts,
}
}
// ---------------------------------------------------------------------------------------------------------------------