add generic SyncMap lib
This commit is contained in:
parent
a67c186cae
commit
94669f9b04
1 changed files with 40 additions and 0 deletions
40
lib/syncmap/syncmap.go
Normal file
40
lib/syncmap/syncmap.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package syncmap
|
||||
|
||||
import "sync"
|
||||
|
||||
type SyncMap[K comparable, V any] struct {
|
||||
_map *sync.Map
|
||||
}
|
||||
|
||||
func New[K comparable, V any]() SyncMap[K, V] {
|
||||
return SyncMap[K, V]{
|
||||
_map: &sync.Map{},
|
||||
}
|
||||
}
|
||||
|
||||
func (sm SyncMap[K, V]) Set(key K, value V) {
|
||||
sm._map.Store(key, value)
|
||||
}
|
||||
|
||||
func (sm SyncMap[K, V]) GetWithStatus(key K) (value V, ok bool) {
|
||||
v, has := sm._map.Load(key)
|
||||
if !has {
|
||||
return value, false
|
||||
}
|
||||
return v.(V), true
|
||||
}
|
||||
|
||||
func (sm SyncMap[K, V]) Get(key K) (value V) {
|
||||
v, _ := sm.GetWithStatus(key)
|
||||
return v
|
||||
}
|
||||
|
||||
func (sm SyncMap[K, V]) Delete(key K) {
|
||||
sm._map.Delete(key)
|
||||
}
|
||||
|
||||
func (sm SyncMap[K, V]) Range(f func(key K, value V) bool) {
|
||||
sm._map.Range(func(k, v any) bool {
|
||||
return f(k.(K), v.(V))
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue