mirror of
https://github.com/mudler/LocalAI.git
synced 2024-12-19 12:47:54 +00:00
78 lines
1.2 KiB
Go
78 lines
1.2 KiB
Go
|
package xsync
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type SyncedMap[K comparable, V any] struct {
|
||
|
mu sync.RWMutex
|
||
|
m map[K]V
|
||
|
}
|
||
|
|
||
|
func NewSyncedMap[K comparable, V any]() *SyncedMap[K, V] {
|
||
|
return &SyncedMap[K, V]{
|
||
|
m: make(map[K]V),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Get(key K) V {
|
||
|
m.mu.RLock()
|
||
|
defer m.mu.RUnlock()
|
||
|
return m.m[key]
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Keys() []K {
|
||
|
m.mu.RLock()
|
||
|
defer m.mu.RUnlock()
|
||
|
keys := make([]K, 0, len(m.m))
|
||
|
for k := range m.m {
|
||
|
keys = append(keys, k)
|
||
|
}
|
||
|
return keys
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Values() []V {
|
||
|
m.mu.RLock()
|
||
|
defer m.mu.RUnlock()
|
||
|
values := make([]V, 0, len(m.m))
|
||
|
for _, v := range m.m {
|
||
|
values = append(values, v)
|
||
|
}
|
||
|
return values
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Len() int {
|
||
|
m.mu.RLock()
|
||
|
defer m.mu.RUnlock()
|
||
|
return len(m.m)
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Iterate(f func(key K, value V) bool) {
|
||
|
m.mu.RLock()
|
||
|
defer m.mu.RUnlock()
|
||
|
for k, v := range m.m {
|
||
|
if !f(k, v) {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Set(key K, value V) {
|
||
|
m.mu.Lock()
|
||
|
m.m[key] = value
|
||
|
m.mu.Unlock()
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Delete(key K) {
|
||
|
m.mu.Lock()
|
||
|
delete(m.m, key)
|
||
|
m.mu.Unlock()
|
||
|
}
|
||
|
|
||
|
func (m *SyncedMap[K, V]) Exists(key K) bool {
|
||
|
m.mu.RLock()
|
||
|
defer m.mu.RUnlock()
|
||
|
_, ok := m.m[key]
|
||
|
return ok
|
||
|
}
|