gitee.com/quant1x/gox@v1.21.2/concurrent/treemap.go (about) 1 package concurrent 2 3 import ( 4 "cmp" 5 rbt "gitee.com/quant1x/gox/util/redblacktree" 6 ) 7 8 type TreeMap[K comparable, V any] struct { 9 //treemap.Map 10 tree *rbt.Tree 11 } 12 13 func NewTreeMap[K cmp.Ordered, V any]() *TreeMap[K, V] { 14 tree := rbt.Tree{Comparator: func(a, b any) int { 15 A := a.(K) 16 B := b.(K) 17 return cmp.Compare(A, B) 18 }} 19 return &TreeMap[K, V]{tree: &tree} 20 } 21 22 func (m *TreeMap[K, V]) Size() int { 23 return m.tree.Size() 24 } 25 26 func (m *TreeMap[K, V]) Put(k K, v V) { 27 m.tree.Put(k, v) 28 } 29 30 func (m *TreeMap[K, V]) Get(k K) (v V, found bool) { 31 tmp, found := m.tree.Get(k) 32 if !found { 33 return 34 } 35 return tmp.(V), true 36 } 37 38 func (m *TreeMap[K, V]) Each(f func(key K, value V)) { 39 iterator := m.tree.Iterator() 40 for iterator.Next() { 41 f(iterator.Key().(K), iterator.Value().(V)) 42 } 43 } 44 45 func (m *TreeMap[K, V]) Clear() { 46 m.tree.Clear() 47 }