maps.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package csync
  2. import (
  3. "encoding/json"
  4. "iter"
  5. "maps"
  6. "sync"
  7. )
  8. // Map is a concurrent map implementation that provides thread-safe access.
  9. type Map[K comparable, V any] struct {
  10. inner map[K]V
  11. mu sync.RWMutex
  12. }
  13. // NewMap creates a new thread-safe map with the specified key and value types.
  14. func NewMap[K comparable, V any]() *Map[K, V] {
  15. return &Map[K, V]{
  16. inner: make(map[K]V),
  17. }
  18. }
  19. // NewMapFrom creates a new thread-safe map from an existing map.
  20. func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
  21. return &Map[K, V]{
  22. inner: m,
  23. }
  24. }
  25. // Set sets the value for the specified key in the map.
  26. func (m *Map[K, V]) Set(key K, value V) {
  27. m.mu.Lock()
  28. defer m.mu.Unlock()
  29. m.inner[key] = value
  30. }
  31. // Del deletes the specified key from the map.
  32. func (m *Map[K, V]) Del(key K) {
  33. m.mu.Lock()
  34. defer m.mu.Unlock()
  35. delete(m.inner, key)
  36. }
  37. // Get gets the value for the specified key from the map.
  38. func (m *Map[K, V]) Get(key K) (V, bool) {
  39. m.mu.RLock()
  40. defer m.mu.RUnlock()
  41. v, ok := m.inner[key]
  42. return v, ok
  43. }
  44. // Len returns the number of items in the map.
  45. func (m *Map[K, V]) Len() int {
  46. m.mu.RLock()
  47. defer m.mu.RUnlock()
  48. return len(m.inner)
  49. }
  50. // GetOrSet gets and returns the key if it exists, otherwise, it executes the
  51. // given function, set its return value for the given key, and returns it.
  52. func (m *Map[K, V]) GetOrSet(key K, fn func() V) V {
  53. got, ok := m.Get(key)
  54. if ok {
  55. return got
  56. }
  57. value := fn()
  58. m.Set(key, value)
  59. return value
  60. }
  61. // Take gets an item and then deletes it.
  62. func (m *Map[K, V]) Take(key K) (V, bool) {
  63. m.mu.Lock()
  64. defer m.mu.Unlock()
  65. v, ok := m.inner[key]
  66. delete(m.inner, key)
  67. return v, ok
  68. }
  69. // Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
  70. func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
  71. dst := make(map[K]V)
  72. m.mu.RLock()
  73. maps.Copy(dst, m.inner)
  74. m.mu.RUnlock()
  75. return func(yield func(K, V) bool) {
  76. for k, v := range dst {
  77. if !yield(k, v) {
  78. return
  79. }
  80. }
  81. }
  82. }
  83. // Seq returns an iter.Seq that yields values from the map.
  84. func (m *Map[K, V]) Seq() iter.Seq[V] {
  85. return func(yield func(V) bool) {
  86. for _, v := range m.Seq2() {
  87. if !yield(v) {
  88. return
  89. }
  90. }
  91. }
  92. }
  93. var (
  94. _ json.Unmarshaler = &Map[string, any]{}
  95. _ json.Marshaler = &Map[string, any]{}
  96. )
  97. func (Map[K, V]) JSONSchemaAlias() any { //nolint
  98. m := map[K]V{}
  99. return m
  100. }
  101. // UnmarshalJSON implements json.Unmarshaler.
  102. func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
  103. m.mu.Lock()
  104. defer m.mu.Unlock()
  105. m.inner = make(map[K]V)
  106. return json.Unmarshal(data, &m.inner)
  107. }
  108. // MarshalJSON implements json.Marshaler.
  109. func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
  110. m.mu.RLock()
  111. defer m.mu.RUnlock()
  112. return json.Marshal(m.inner)
  113. }