mirror of
https://github.com/mudler/LocalAI.git
synced 2024-06-07 19:40:48 +00:00
fb2a05ff43
* feat(gallery): keep showing progress also when refreshing Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(intel-gpu): better defaults Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat: make it thread-safe Signed-off-by: mudler <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Signed-off-by: mudler <mudler@localai.io>
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
|
|
}
|