1 Star 1 Fork 0

Breeze/bzv2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
internal.go 722 Bytes
一键复制 编辑 原始数据 按行查看 历史
Breeze 提交于 2023-08-13 23:16 +08:00 . first commit
package bzgncmap
import "sync/atomic"
// 缩容
func (m *Map[K, V]) shrink() {
length := int32(len(m.m))
// 计算是否需要缩容
if capacity, ok := calcCapacity(m.count, length); ok {
m.putFunc(func(ms map[K]V) { m.newMap(capacity) })
}
}
// 计算是否需要缩容
func calcCapacity(c, l int32) (int32, bool) {
if c <= 64 {
return c, false
}
if c > 2048 && (c/l >= 2) {
factor := 0.625
return int32(float32(c) * float32(factor)), true
}
if c <= 2048 && (c/l >= 4) {
return c / 2, true
}
return c, false
}
// 创建新map
func (m *Map[K, V]) newMap(length int32) {
newM := make(map[K]V, length)
for k, v := range m.m {
newM[k] = v
}
m.m = newM
atomic.StoreInt32(&m.count, length)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/breezeHub/bzv2.git
git@gitee.com:breezeHub/bzv2.git
breezeHub
bzv2
bzv2
v0.0.5

搜索帮助