mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
118 lines
2.4 KiB
Go
118 lines
2.4 KiB
Go
package csync
|
|
|
|
import (
|
|
"encoding/json"
|
|
"iter"
|
|
"maps"
|
|
"sync"
|
|
)
|
|
|
|
// Map is a concurrent map implementation that provides thread-safe access.
|
|
type Map[K comparable, V any] struct {
|
|
inner map[K]V
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
// NewMap creates a new thread-safe map with the specified key and value types.
|
|
func NewMap[K comparable, V any]() *Map[K, V] {
|
|
return &Map[K, V]{
|
|
inner: make(map[K]V),
|
|
}
|
|
}
|
|
|
|
// NewMapFrom creates a new thread-safe map from an existing map.
|
|
func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
|
|
return &Map[K, V]{
|
|
inner: m,
|
|
}
|
|
}
|
|
|
|
// Set sets the value for the specified key in the map.
|
|
func (m *Map[K, V]) Set(key K, value V) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.inner[key] = value
|
|
}
|
|
|
|
// Del deletes the specified key from the map.
|
|
func (m *Map[K, V]) Del(key K) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.inner, key)
|
|
}
|
|
|
|
// Get gets the value for the specified key from the map.
|
|
func (m *Map[K, V]) Get(key K) (V, bool) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
v, ok := m.inner[key]
|
|
return v, ok
|
|
}
|
|
|
|
// Len returns the number of items in the map.
|
|
func (m *Map[K, V]) Len() int {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return len(m.inner)
|
|
}
|
|
|
|
// Take gets an item and then deletes it.
|
|
func (m *Map[K, V]) Take(key K) (V, bool) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
v, ok := m.inner[key]
|
|
delete(m.inner, key)
|
|
return v, ok
|
|
}
|
|
|
|
// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
|
|
func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
|
|
dst := make(map[K]V)
|
|
m.mu.RLock()
|
|
maps.Copy(dst, m.inner)
|
|
m.mu.RUnlock()
|
|
return func(yield func(K, V) bool) {
|
|
for k, v := range dst {
|
|
if !yield(k, v) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Seq returns an iter.Seq that yields values from the map.
|
|
func (m *Map[K, V]) Seq() iter.Seq[V] {
|
|
return func(yield func(V) bool) {
|
|
for _, v := range m.Seq2() {
|
|
if !yield(v) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var (
|
|
_ json.Unmarshaler = &Map[string, any]{}
|
|
_ json.Marshaler = &Map[string, any]{}
|
|
)
|
|
|
|
func (Map[K, V]) JSONSchemaAlias() any { //nolint
|
|
m := map[K]V{}
|
|
return m
|
|
}
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
func (m *Map[K, V]) UnmarshalJSON(data []byte) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.inner = make(map[K]V)
|
|
return json.Unmarshal(data, &m.inner)
|
|
}
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
func (m *Map[K, V]) MarshalJSON() ([]byte, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return json.Marshal(m.inner)
|
|
}
|