on main help sign in
docs: godoc
7e7a69fCarlos Alexandro Becker
internal/csync/maps.go+8
@@ -7,35 +7,41 @@ import (
7 "sync"
7 "sync"
8)
8)
9
9
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
13}
14}
14
15
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),
18 }
20 }
19}
21}
20
22
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]{
23 inner: m,
26 inner: m,
24 }
27 }
25}
28}
26
29
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
31}
35}
32
36
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)
37}
42}
38
43
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
44}
50}
45
51
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)
50}
57}
51
58
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()
internal/csync/slices.go+4
@@ -5,11 +5,14 @@ import (
5 "sync"
5 "sync"
6)
6)
7
7
8+// LazySlice is a thread-safe lazy-loaded slice.
8type LazySlice[K any] struct {
9type LazySlice[K any] struct {
9 inner []K
10 inner []K
10 mu sync.Mutex
11 mu sync.Mutex
11}
12}
12
13
14+// NewLazySlice creates a new slice and runs the [load] function in a goroutine
15+// to populate it.
13func NewLazySlice[K any](load func() []K) *LazySlice[K] {
16func NewLazySlice[K any](load func() []K) *LazySlice[K] {
14 s := &LazySlice[K]{}
17 s := &LazySlice[K]{}
15 s.mu.Lock()
18 s.mu.Lock()
@@ -20,6 +23,7 @@ func NewLazySlice[K any](load func() []K) *LazySlice[K] {
20 return s
23 return s
21}
24}
22
25
26+// Iter returns an iterator that yields elements from the slice.
23func (s *LazySlice[K]) Iter() iter.Seq[K] {
27func (s *LazySlice[K]) Iter() iter.Seq[K] {
24 s.mu.Lock()
28 s.mu.Lock()
25 inner := s.inner
29 inner := s.inner