1 Star 0 Fork 0

leonxiong/xtool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ordermap.go 4.19 KB
一键复制 编辑 原始数据 按行查看 历史
leonxiong 提交于 2024-12-05 16:40 . update xtool
package xmap
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
"github.com/iancoleman/orderedmap"
"sync"
)
type OrderMap[K comparable, V any] struct {
*maputil.OrderedMap[K, V]
mutex sync.Mutex
}
func NewOrderMap[K comparable, V any]() *OrderMap[K, V] {
resp := new(OrderMap[K, V])
resp.OrderedMap = maputil.NewOrderedMap[K, V]()
return resp
}
func (m *OrderMap[K, V]) init() {
if m.OrderedMap != nil {
return
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.OrderedMap == nil {
m.OrderedMap = maputil.NewOrderedMap[K, V]()
}
}
func (m *OrderMap[KeyT, ValueT]) Store(key KeyT, value ValueT) {
m.init()
m.OrderedMap.Set(key, value)
}
func (m *OrderMap[KeyT, ValueT]) LoadOrStore(key KeyT, value ValueT) (ValueT, bool) {
m.init()
retValue, ok := m.OrderedMap.Get(key)
m.OrderedMap.Set(key, value)
return retValue, ok
}
func (m *OrderMap[KeyT, ValueT]) Load(key KeyT) (ValueT, bool) {
m.init()
retValue, ok := m.OrderedMap.Get(key)
return retValue, ok
}
func (m *OrderMap[KeyT, ValueT]) LoadAndDelete(key KeyT) (ValueT, bool) {
m.init()
retValue, ok := m.OrderedMap.Get(key)
m.OrderedMap.Delete(key)
return retValue, ok
}
func (m *OrderMap[KeyT, ValueT]) Delete(key KeyT) {
m.init()
m.OrderedMap.Delete(key)
}
func (m *OrderMap[KeyT, ValueT]) Reset() {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
m.OrderedMap = maputil.NewOrderedMap[KeyT, ValueT]()
}
func (m *OrderMap[KeyT, ValueT]) Len() int {
m.init()
return m.OrderedMap.Len()
}
func (m *OrderMap[keyT, valueT]) Range(f func(key keyT, value valueT) bool) {
m.init()
if f == nil {
return
}
m.OrderedMap.Range(f)
}
func (m *OrderMap[keyT, valueT]) ExistKey(k keyT) bool {
m.init()
_, ok := m.OrderedMap.Get(k)
return ok
}
func (m *OrderMap[keyT, valueT]) Keys() []keyT {
m.init()
return m.OrderedMap.Keys()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type OrderMapSeq[K comparable, V any] struct {
*orderedmap.OrderedMap
mutex sync.Mutex
}
func NewOrderMapSeq[K comparable, V any]() *OrderMapSeq[K, V] {
resp := new(OrderMapSeq[K, V])
resp.OrderedMap = orderedmap.New()
return resp
}
func (m *OrderMapSeq[K, V]) init() {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.OrderedMap == nil {
m.OrderedMap = orderedmap.New()
}
}
func (m *OrderMapSeq[KeyT, ValueT]) Store(key KeyT, value ValueT) {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
tmpKey := fmt.Sprintf("%+v", key)
m.OrderedMap.Set(tmpKey, value)
}
func (m *OrderMapSeq[KeyT, ValueT]) LoadOrStore(key KeyT, value ValueT) (ValueT, bool) {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
tmpKey := fmt.Sprintf("%+v", key)
inf, ok := m.OrderedMap.Get(tmpKey)
retValue, _ := inf.(ValueT)
m.OrderedMap.Set(tmpKey, value)
return retValue, ok
}
func (m *OrderMapSeq[KeyT, ValueT]) Load(key KeyT) (ValueT, bool) {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
tmpKey := fmt.Sprintf("%+v", key)
inf, ok := m.OrderedMap.Get(tmpKey)
retValue, _ := inf.(ValueT)
return retValue, ok
}
func (m *OrderMapSeq[KeyT, ValueT]) LoadAndDelete(key KeyT) (ValueT, bool) {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
tmpKey := fmt.Sprintf("%+v", key)
inf, ok := m.OrderedMap.Get(tmpKey)
retValue, _ := inf.(ValueT)
m.OrderedMap.Delete(tmpKey)
return retValue, ok
}
func (m *OrderMapSeq[KeyT, ValueT]) Delete(key KeyT) {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
tmpKey := fmt.Sprintf("%+v", key)
m.OrderedMap.Delete(tmpKey)
}
func (m *OrderMapSeq[KeyT, ValueT]) Len() int {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
return len(m.OrderedMap.Values())
}
func (m *OrderMapSeq[keyT, valueT]) Range(f func(key string, value valueT) bool) {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
if f == nil {
return
}
for _, v := range m.OrderedMap.Keys() {
inf, _ := m.Get(v)
tmpValue, _ := inf.(valueT)
ret := f(v, tmpValue)
if ret == false {
return
}
}
}
func (m *OrderMapSeq[keyT, valueT]) ExistKey(k keyT) bool {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
tmpKey := fmt.Sprintf("%+v", k)
_, ok := m.OrderedMap.Get(tmpKey)
return ok
}
func (m *OrderMapSeq[keyT, valueT]) Keys() []string {
m.init()
m.mutex.Lock()
defer m.mutex.Unlock()
return m.OrderedMap.Keys()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xlm516/xtool.git
git@gitee.com:xlm516/xtool.git
xlm516
xtool
xtool
cb352a827910

搜索帮助

0d507c66 1850385 C8b1a773 1850385