@@ -7,35 +7,41 @@ import (
77 "sync"
88)
99
10+// Map is a concurrent map implementation that provides thread-safe access.
1011type Map[K comparable, V any] struct {
1112 inner map[K]V
1213 mu sync.RWMutex
1314}
1415
16+// NewMap creates a new thread-safe map with the specified key and value types.
1517func NewMap[K comparable, V any]() *Map[K, V] {
1618 return &Map[K, V]{
1719 inner: make(map[K]V),
1820 }
1921}
2022
23+// NewMapFrom creates a new thread-safe map from an existing map.
2124func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
2225 return &Map[K, V]{
2326 inner: m,
2427 }
2528}
2629
30+// Set sets the value for the specified key in the map.
2731func (m *Map[K, V]) Set(key K, value V) {
2832 m.mu.Lock()
2933 defer m.mu.Unlock()
3034 m.inner[key] = value
3135}
3236
37+// Del deletes the specified key from the map.
3338func (m *Map[K, V]) Del(key K) {
3439 m.mu.Lock()
3540 defer m.mu.Unlock()
3641 delete(m.inner, key)
3742}
3843
44+// Get gets the value for the specified key from the map.
3945func (m *Map[K, V]) Get(key K) (V, bool) {
4046 m.mu.RLock()
4147 defer m.mu.RUnlock()
@@ -43,12 +49,14 @@ func (m *Map[K, V]) Get(key K) (V, bool) {
4349 return v, ok
4450}
4551
52+// Len returns the number of items in the map.
4653func (m *Map[K, V]) Len() int {
4754 m.mu.RLock()
4855 defer m.mu.RUnlock()
4956 return len(m.inner)
5057}
5158
59+// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
5260func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
5361 dst := make(map[K]V)
5462 m.mu.RLock()