代码拉取完成,页面将自动刷新
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()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。