Skip to content

Commit d55a46b

Browse files
committed
更新
1 parent 3043e41 commit d55a46b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ allKeys := m.Keys() //返回所有的key
183183
allValues := m.Values()// 返回所有的value
184184
```
185185
## 十二、`cmap`
186-
cmap是用锁分区的方式实现的,(TODO与sync.Map测试下性能对比,从sync.Map的源代码上看只能用于读多写少,如果写读对半分,或者写多读少?)
186+
cmap是用锁分区的方式实现的,(TODO优化,目前只有几个指标比sync.Map快)
187187
```go
188188
var m cmap.CMap[string, string] // 声明一个string, string的map
189189
m.Store("hello", "1") // 保存

mapex/mapex.go

+22
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
package mapex
22

3+
import (
4+
"sort"
5+
6+
"golang.org/x/exp/constraints"
7+
)
8+
39
type Map[K comparable, V any] map[K]V
410

511
func 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+
923
func 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+
1335
func (m Map[K, V]) Keys() (keys []K) {
1436
keys = make([]K, 0, len(m))
1537
for k := range m {

0 commit comments

Comments
 (0)