1 Star 1 Fork 0

Breeze/bzv2

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
rand.go 481 Bytes
Copy Edit Raw Blame History
Breeze authored 2023-08-13 23:16 +08:00 . first commit
package bzgncmap
import "sync/atomic"
// GetRand 随机获取 map
func (m *Map[K, V]) GetRand(count int) map[K]V {
if count <= 0 {
return nil
}
if int(m.Len()) < count {
count = int(m.Len())
}
// 根据 key 匹配 value
newM := make(map[K]V, count)
var i int32
m.getFunc(func(ms map[K]V) {
for key, value := range ms {
it := atomic.LoadInt32(&i)
if it > int32(count-1) {
break
}
atomic.AddInt32(&i, 1)
newM[key] = value
}
})
return newM
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/breezeHub/bzv2.git
git@gitee.com:breezeHub/bzv2.git
breezeHub
bzv2
bzv2
v0.0.5

Search