@@ -7,35 +7,41 @@ import (
10+// Map is a concurrent map implementation that provides thread-safe access.
10type Map[K comparable, V any] struct {
11type Map[K comparable, V any] struct {
11 inner map[K]V
12 inner map[K]V
12 mu sync.RWMutex
13 mu sync.RWMutex
16+// NewMap creates a new thread-safe map with the specified key and value types.
15func NewMap[K comparable, V any]() *Map[K, V] {
17func NewMap[K comparable, V any]() *Map[K, V] {
16 return &Map[K, V]{
18 return &Map[K, V]{
17 inner: make(map[K]V),
19 inner: make(map[K]V),
23+// NewMapFrom creates a new thread-safe map from an existing map.
21func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
24func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
22 return &Map[K, V]{
25 return &Map[K, V]{
30+// Set sets the value for the specified key in the map.
27func (m *Map[K, V]) Set(key K, value V) {
31func (m *Map[K, V]) Set(key K, value V) {
28 m.mu.Lock()
32 m.mu.Lock()
29 defer m.mu.Unlock()
33 defer m.mu.Unlock()
30 m.inner[key] = value
34 m.inner[key] = value
37+// Del deletes the specified key from the map.
33func (m *Map[K, V]) Del(key K) {
38func (m *Map[K, V]) Del(key K) {
34 m.mu.Lock()
39 m.mu.Lock()
35 defer m.mu.Unlock()
40 defer m.mu.Unlock()
36 delete(m.inner, key)
41 delete(m.inner, key)
44+// Get gets the value for the specified key from the map.
39func (m *Map[K, V]) Get(key K) (V, bool) {
45func (m *Map[K, V]) Get(key K) (V, bool) {
40 m.mu.RLock()
46 m.mu.RLock()
41 defer m.mu.RUnlock()
47 defer m.mu.RUnlock()
@@ -43,12 +49,14 @@ func (m *Map[K, V]) Get(key K) (V, bool) {
43 return v, ok
49 return v, ok
52+// Len returns the number of items in the map.
46func (m *Map[K, V]) Len() int {
53func (m *Map[K, V]) Len() int {
47 m.mu.RLock()
54 m.mu.RLock()
48 defer m.mu.RUnlock()
55 defer m.mu.RUnlock()
49 return len(m.inner)
56 return len(m.inner)
59+// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
52func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
60func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
53 dst := make(map[K]V)
61 dst := make(map[K]V)
54 m.mu.RLock()
62 m.mu.RLock()