File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -183,7 +183,7 @@ allKeys := m.Keys() //返回所有的key
183
183
allValues := m.Values ()// 返回所有的value
184
184
```
185
185
## 十二、` cmap `
186
- cmap是用锁分区的方式实现的,(TODO与sync.Map测试下性能对比,从sync.Map的源代码上看只能用于读多写少,如果写读对半分,或者写多读少? )
186
+ cmap是用锁分区的方式实现的,(TODO优化,目前只有几个指标比sync.Map快 )
187
187
``` go
188
188
var m cmap.CMap [string , string ] // 声明一个string, string的map
189
189
m.Store (" hello" , " 1" ) // 保存
Original file line number Diff line number Diff line change 1
1
package mapex
2
2
3
+ import (
4
+ "sort"
5
+
6
+ "golang.org/x/exp/constraints"
7
+ )
8
+
3
9
type Map [K comparable , V any ] map [K ]V
4
10
5
11
func Keys [K comparable , V any ](m map [K ]V ) (keys []K ) {
6
12
return Map [K , V ](m ).Keys ()
7
13
}
8
14
15
+ func SortKeys [K constraints.Ordered , V any ](m map [K ]V ) (keys []K ) {
16
+ keys = Keys (m )
17
+ sort .Slice (keys , func (i , j int ) bool {
18
+ return keys [i ] < keys [j ]
19
+ })
20
+ return keys
21
+ }
22
+
9
23
func Values [K comparable , V any ](m map [K ]V ) (values []V ) {
10
24
return Map [K , V ](m ).Values ()
11
25
}
12
26
27
+ func SortValues [K comparable , V constraints.Ordered ](m map [K ]V ) (values []V ) {
28
+ values = Values (m )
29
+ sort .Slice (values , func (i , j int ) bool {
30
+ return values [i ] < values [j ]
31
+ })
32
+ return values
33
+ }
34
+
13
35
func (m Map [K , V ]) Keys () (keys []K ) {
14
36
keys = make ([]K , 0 , len (m ))
15
37
for k := range m {
You can’t perform that action at this time.
0 commit comments