return
}
+type LoadOrStoreFunc[T any] struct {
+ Init func() *T
+ cache *T
+ l sync.Mutex
+}
+
+func (l *LoadOrStoreFunc[T]) LoadOrStore(t interface {
+ LoadOrStore(k, v any) (actual any, loaded bool)
+}, k any) (actual T, loaded bool) {
+ l.l.Lock()
+ defer l.l.Unlock()
+
+ if l.cache == nil {
+ l.cache = l.Init()
+ }
+ if actual, loaded := t.LoadOrStore(k, l.cache); !loaded {
+ l.cache = nil
+ return *(actual.(*T)), false
+ } else {
+ return *(actual.(*T)), true
+ }
+}
+
+func (l *LoadOrStoreFunc[T]) LoadOrStoreP(t interface {
+ LoadOrStore(k, v any) (actual any, loaded bool)
+}, k any) (actual *T, loaded bool) {
+ l.l.Lock()
+ defer l.l.Unlock()
+
+ if l.cache == nil {
+ l.cache = l.Init()
+ }
+ if actual, loaded := t.LoadOrStore(k, l.cache); !loaded {
+ l.cache = nil
+ return actual.(*T), false
+ } else {
+ return actual.(*T), true
+ }
+}
+
func (t *Map) Load(k any) (any, bool) {
return t.m.Load(k)
}
return
}
+func (t *Map) CopyP() (m *Map) {
+ m = &Map{}
+ t.Range(func(k, v any) bool {
+ m.Store(k, v)
+ return true
+ })
+ return
+}
+
func Copy[T comparable, S any](s map[T]S) map[T]S {
t := make(map[T]S)
for k, v := range s {
p int
}
+func TestLS(t *testing.T) {
+ var c Map
+ var ls = LoadOrStoreFunc[int]{
+ Init: func() *int {
+ var i = 1
+ return &i
+ },
+ }
+ a0, l0 := ls.LoadOrStore(&c, `1`)
+ if l0 {
+ t.Fatal()
+ }
+ a1, l1 := ls.LoadOrStore(&c, `1`)
+ if !l1 {
+ t.Fatal()
+ }
+ if a0 != a1 {
+ t.Fatal()
+ }
+}
+
func Test_customMap(t *testing.T) {
var c Map
//set