File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 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
183183allValues := m.Values ()// 返回所有的value
184184```
185185## 十二、` cmap `
186- cmap是用锁分区的方式实现的,(TODO与sync.Map测试下性能对比,从sync.Map的源代码上看只能用于读多写少,如果写读对半分,或者写多读少? )
186+ cmap是用锁分区的方式实现的,(TODO优化,目前只有几个指标比sync.Map快 )
187187``` go
188188var m cmap.CMap [string , string ] // 声明一个string, string的map
189189m.Store (" hello" , " 1" ) // 保存
Original file line number Diff line number Diff line change 11package mapex
22
3+ import (
4+ "sort"
5+
6+ "golang.org/x/exp/constraints"
7+ )
8+
39type Map [K comparable , V any ] map [K ]V
410
511func Keys [K comparable , V any ](m map [K ]V ) (keys []K ) {
612 return Map [K , V ](m ).Keys ()
713}
814
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+
923func Values [K comparable , V any ](m map [K ]V ) (values []V ) {
1024 return Map [K , V ](m ).Values ()
1125}
1226
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+
1335func (m Map [K , V ]) Keys () (keys []K ) {
1436 keys = make ([]K , 0 , len (m ))
1537 for k := range m {
You can’t perform that action at this time.
0 commit comments