maps.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Take gets an item and then deletes it.
  51. func (m *Map[K, V]) Take(key K) (V, bool) {
  52. m.mu.Lock()
  53. defer m.mu.Unlock()
  54. v, ok := m.inner[key]
  55. delete(m.inner, key)
  56. return v, ok
  57. }
  58. // Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
  59. func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
  60. dst := make(map[K]V)
  61. m.mu.RLock()
  62. maps.Copy(dst, m.inner)
  63. m.mu.RUnlock()
  64. return func(yield func(K, V) bool) {
  65. for k, v := range dst {
  66. if !yield(k, v) {
  67. return
  68. }
  69. }
  70. }
  71. }
  72. // Seq returns an iter.Seq that yields values from the map.
  73. func (m *Map[K, V]) Seq() iter.Seq[V] {
  74. return func(yield func(V) bool) {
  75. for _, v := range m.Seq2() {
  76. if !yield(v) {
  77. return
  78. }
  79. }
  80. }
  81. }
  82. var (
  83. _ json.Unmarshaler = &Map[string, any]{}
  84. _ json.Marshaler = &Map[string, any]{}
  85. )
  86. // UnmarshalJSON implements json.Unmarshaler.
  87. func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
  88. m.mu.Lock()
  89. defer m.mu.Unlock()
  90. m.inner = make(map[K]V)
  91. return json.Unmarshal(data, &m.inner)
  92. }
  93. // MarshalJSON implements json.Marshaler.
  94. func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
  95. m.mu.RLock()
  96. defer m.mu.RUnlock()
  97. return json.Marshal(m.inner)
  98. }