add generic SyncMap lib

This commit is contained in:
Gavin Nishizawa 2023-10-30 12:02:29 -07:00
parent a67c186cae
commit 94669f9b04
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

40
lib/syncmap/syncmap.go Normal file
View 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))
})
}