github.com/vipernet-xyz/tendermint-core@v0.32.0/libs/cmap/cmap.go (about) 1 package cmap 2 3 import "sync" 4 5 // CMap is a goroutine-safe map 6 type CMap struct { 7 m map[string]interface{} 8 l sync.Mutex 9 } 10 11 func NewCMap() *CMap { 12 return &CMap{ 13 m: make(map[string]interface{}), 14 } 15 } 16 17 func (cm *CMap) Set(key string, value interface{}) { 18 cm.l.Lock() 19 cm.m[key] = value 20 cm.l.Unlock() 21 } 22 23 func (cm *CMap) Get(key string) interface{} { 24 cm.l.Lock() 25 val := cm.m[key] 26 cm.l.Unlock() 27 return val 28 } 29 30 func (cm *CMap) Has(key string) bool { 31 cm.l.Lock() 32 _, ok := cm.m[key] 33 cm.l.Unlock() 34 return ok 35 } 36 37 func (cm *CMap) Delete(key string) { 38 cm.l.Lock() 39 delete(cm.m, key) 40 cm.l.Unlock() 41 } 42 43 func (cm *CMap) Size() int { 44 cm.l.Lock() 45 size := len(cm.m) 46 cm.l.Unlock() 47 return size 48 } 49 50 func (cm *CMap) Clear() { 51 cm.l.Lock() 52 cm.m = make(map[string]interface{}) 53 cm.l.Unlock() 54 } 55 56 func (cm *CMap) Keys() []string { 57 cm.l.Lock() 58 59 keys := make([]string, 0, len(cm.m)) 60 for k := range cm.m { 61 keys = append(keys, k) 62 } 63 cm.l.Unlock() 64 return keys 65 } 66 67 func (cm *CMap) Values() []interface{} { 68 cm.l.Lock() 69 items := make([]interface{}, 0, len(cm.m)) 70 for _, v := range cm.m { 71 items = append(items, v) 72 } 73 cm.l.Unlock() 74 return items 75 }