mukan-consensus/libs/cmap/cmap.go
Mukan Erkin Törük ef24c0b67e
Some checks are pending
docker-build-cometbft / vars (push) Waiting to run
docker-build-cometbft / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-cometbft / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-cometbft / merge-images (push) Blocked by required conditions
docker-build-e2e-node / vars (push) Waiting to run
docker-build-e2e-node / build-images (amd64, ubuntu-24.04) (push) Blocked by required conditions
docker-build-e2e-node / build-images (arm64, ubuntu-24.04-arm) (push) Blocked by required conditions
docker-build-e2e-node / merge-images (push) Blocked by required conditions
initial: sovereign Mukan Network fork
2026-05-11 03:18:27 +03:00

77 lines
1.2 KiB
Go

package cmap
import (
cmtsync "github.com/cometbft/cometbft/libs/sync"
)
// CMap is a goroutine-safe map
type CMap struct {
m map[string]interface{}
l cmtsync.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
}