Ai
2 Star 2 Fork 9

王布衣/gox

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
treemap.go 835 Bytes
Copy Edit Raw Blame History
王布衣 authored 2023-10-28 05:24 +08:00 . treemap的clear方法增加互斥锁
package concurrent
import (
"cmp"
rbt "gitee.com/quant1x/gox/util/redblacktree"
)
type TreeMap[K comparable, V any] struct {
//treemap.Map
tree *rbt.Tree
}
func NewTreeMap[K cmp.Ordered, V any]() *TreeMap[K, V] {
tree := rbt.Tree{Comparator: func(a, b any) int {
A := a.(K)
B := b.(K)
return cmp.Compare(A, B)
}}
return &TreeMap[K, V]{tree: &tree}
}
func (m *TreeMap[K, V]) Size() int {
return m.tree.Size()
}
func (m *TreeMap[K, V]) Put(k K, v V) {
m.tree.Put(k, v)
}
func (m *TreeMap[K, V]) Get(k K) (v V, found bool) {
tmp, found := m.tree.Get(k)
if !found {
return
}
return tmp.(V), true
}
func (m *TreeMap[K, V]) Each(f func(key K, value V)) {
iterator := m.tree.Iterator()
for iterator.Next() {
f(iterator.Key().(K), iterator.Value().(V))
}
}
func (m *TreeMap[K, V]) Clear() {
m.tree.Clear()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.22.11

Search