libs5-go/structs/map.go

135 lines
2.3 KiB
Go
Raw Normal View History

2024-01-06 11:33:46 +00:00
package structs
import (
2024-01-07 08:57:12 +00:00
"github.com/emirpasic/gods/maps"
2024-01-06 11:33:46 +00:00
"github.com/emirpasic/gods/maps/hashmap"
"log"
"sync"
)
2024-01-07 08:57:12 +00:00
var _ maps.Map = (*MapImpl)(nil)
type Map interface {
GetInt(key interface{}) (value *int)
GetString(key interface{}) (value *string)
PutInt(key interface{}, value int)
Contains(value interface{}) bool
maps.Map
}
type MapImpl struct {
2024-01-06 11:33:46 +00:00
*hashmap.Map
mutex *sync.RWMutex
}
2024-01-07 08:57:12 +00:00
func NewMap() Map {
return &MapImpl{
2024-01-06 11:33:46 +00:00
Map: hashmap.New(),
mutex: &sync.RWMutex{},
}
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Get(key interface{}) (value interface{}, found bool) {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.Get(key)
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) GetInt(key interface{}) (value *int) {
2024-01-06 11:33:46 +00:00
val, found := m.Get(key)
if !found {
return nil
}
if intValue, ok := val.(int); ok {
value = &intValue
} else {
log.Fatalf("value is not an int: %v", val)
}
return value
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) GetString(key interface{}) (value *string) {
2024-01-06 11:33:46 +00:00
val, found := m.Get(key)
if !found {
return nil
}
if _, ok := val.(string); ok {
value = val.(*string)
} else {
log.Fatalf("value is not a string: %v", value)
}
return
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Put(key interface{}, value interface{}) {
2024-01-06 11:33:46 +00:00
m.mutex.Lock()
defer m.mutex.Unlock()
m.Map.Put(key, value)
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) PutInt(key interface{}, value int) {
2024-01-06 11:33:46 +00:00
m.Put(key, value)
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Remove(key interface{}) {
2024-01-06 11:33:46 +00:00
m.mutex.Lock()
defer m.mutex.Unlock()
m.Map.Remove(key)
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Keys() []interface{} {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.Keys()
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Values() []interface{} {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.Values()
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Size() int {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.Size()
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Empty() bool {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.Empty()
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Clear() {
2024-01-06 11:33:46 +00:00
m.mutex.Lock()
defer m.mutex.Unlock()
m.Map.Clear()
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) String() string {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.String()
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) GetKey(value interface{}) (key interface{}, found bool) {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.Map.Get(value)
}
2024-01-07 08:57:12 +00:00
func (m *MapImpl) Contains(value interface{}) bool {
2024-01-06 11:33:46 +00:00
m.mutex.RLock()
defer m.mutex.RUnlock()
_, has := m.Map.Get(value)
return has
}