Files
tendermint/libs/cmap/cmap.go
Marko 7b52f51700 libs/common: Refactor libs/common 5 (#4240)
* libs/common: Refactor libs/common 5

- move mathematical functions and types out of `libs/common` to math pkg
- move net functions out of `libs/common` to net pkg
- move string functions out of `libs/common` to strings pkg
- move async functions out of `libs/common` to async pkg
- move bit functions out of `libs/common` to bits pkg
- move cmap functions out of `libs/common` to cmap pkg
- move os functions out of `libs/common` to os pkg

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* fix testing issues

* fix tests

closes #41417

woooooooooooooooooo kill the cmn pkg

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* add changelog entry

* fix goimport issues

* run gofmt
2019-12-12 09:33:27 +01:00

76 lines
1.1 KiB
Go

package cmap
import "sync"
// CMap is a goroutine-safe map
type CMap struct {
m map[string]interface{}
l sync.Mutex
}
func NewCMap() *CMap {
return &CMap{
m: make(map[string]interface{}),
}
}
func (cm *CMap) Set(key string, value interface{}) {
cm.l.Lock()
cm.m[key] = value
cm.l.Unlock()
}
func (cm *CMap) Get(key string) interface{} {
cm.l.Lock()
val := cm.m[key]
cm.l.Unlock()
return val
}
func (cm *CMap) Has(key string) bool {
cm.l.Lock()
_, ok := cm.m[key]
cm.l.Unlock()
return ok
}
func (cm *CMap) Delete(key string) {
cm.l.Lock()
delete(cm.m, key)
cm.l.Unlock()
}
func (cm *CMap) Size() int {
cm.l.Lock()
size := len(cm.m)
cm.l.Unlock()
return size
}
func (cm *CMap) Clear() {
cm.l.Lock()
cm.m = make(map[string]interface{})
cm.l.Unlock()
}
func (cm *CMap) Keys() []string {
cm.l.Lock()
keys := make([]string, 0, len(cm.m))
for k := range cm.m {
keys = append(keys, k)
}
cm.l.Unlock()
return keys
}
func (cm *CMap) Values() []interface{} {
cm.l.Lock()
items := make([]interface{}, 0, len(cm.m))
for _, v := range cm.m {
items = append(items, v)
}
cm.l.Unlock()
return items
}