...

Source file src/oss.terrastruct.com/d2/lib/syncmap/syncmap.go

Documentation: oss.terrastruct.com/d2/lib/syncmap

     1  package syncmap
     2  
     3  import "sync"
     4  
     5  type SyncMap[K comparable, V any] struct {
     6  	_map *sync.Map
     7  }
     8  
     9  func New[K comparable, V any]() SyncMap[K, V] {
    10  	return SyncMap[K, V]{
    11  		_map: &sync.Map{},
    12  	}
    13  }
    14  
    15  func (sm SyncMap[K, V]) Set(key K, value V) {
    16  	sm._map.Store(key, value)
    17  }
    18  
    19  func (sm SyncMap[K, V]) Lookup(key K) (value V, ok bool) {
    20  	v, has := sm._map.Load(key)
    21  	if !has {
    22  		return value, false
    23  	}
    24  	return v.(V), true
    25  }
    26  
    27  func (sm SyncMap[K, V]) Get(key K) (value V) {
    28  	v, _ := sm.Lookup(key)
    29  	return v
    30  }
    31  
    32  func (sm SyncMap[K, V]) Delete(key K) {
    33  	sm._map.Delete(key)
    34  }
    35  
    36  func (sm SyncMap[K, V]) Range(f func(key K, value V) bool) {
    37  	sm._map.Range(func(k, v any) bool {
    38  		return f(k.(K), v.(V))
    39  	})
    40  }
    41  

View as plain text