good morning!!!!

Skip to content
Snippets Groups Projects
Commit 8247dc4a authored by Garet Halliday's avatar Garet Halliday
Browse files

Revert "all needed is in mapn"

This reverts commit aa6bcc3c.
parent aa6bcc3c
Branches
No related tags found
No related merge requests found
import (
"sync"
)
type Map1[K0 comparable, V any] struct {
inner *sync.Map
}
func (m *Map1[K0, V]) Delete0(key K0) {
if m.inner == nil {
return
}
m.inner.Delete(key)
}
func (m *Map1[K0, V]) Load0(key K0) (value V, ok bool) {
if m.inner == nil {
return
}
val, ok := m.inner.Load(key)
if ok {
return val.(V), ok
}
var def V
return def, ok
}
func (m *Map1[K0, V]) LoadAndDelete0(key K0) (value V, loaded bool) {
if m.inner == nil {
return
}
val, loaded := m.inner.LoadAndDelete(key)
if loaded {
return val.(V), loaded
}
var def V
return def, loaded
}
func (m *Map1[K0, V]) LoadOrStore0(key K0, value V) (actual V, loaded bool) {
if m.inner == nil {
m.inner = new(sync.Map)
m.inner.Store(key, value)
actual = value
return
}
val, loaded := m.inner.LoadOrStore(key, value)
if loaded {
return val.(V), loaded
}
var def V
return def, loaded
}
func (m *Map1[K0, V]) Range0(f func(key K0, value V) bool) {
if m.inner == nil {
return
}
innerFun := func(key, value any) bool {
tKey, tVal := key.(K0), value.(V)
return f(tKey, tVal)
}
m.inner.Range(innerFun)
}
func (m *Map1[K0, V]) Store0(key K0, value V) {
if m.inner == nil {
m.inner = new(sync.Map)
}
m.inner.Store(key, value)
}
{{range $curCount := iterateFromN 1 .Count}}
type {{template "generictypedef" $curCount}} struct {
inner Map1[K0, {{template "reltypedef" (list (sub $curCount 1) 1)}}]
inner *Map1[K0, {{template "reltypedef" (list (sub $curCount 1) 1 false)}}]
}
func (m *{{template "typedef" $curCount}}) Load({{range $val := (iterateFromN 0 (add $curCount 1)) }}k{{$val}} K{{$val}}, {{end}}
......@@ -114,10 +44,10 @@ value V) {
{{range $subCurCount:= iterateFromN 0 (add $curCount 1)}}
func (m *{{template "typedef" $curCount}}) Load{{$subCurCount}}({{range $val := (iterateFromN 0 (add 1 $subCurCount)) }}k{{$val}} K{{$val}}, {{end}}
) (value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1))}}, ok bool) {
) (value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1) false)}}, ok bool) {
item0 := m.inner
{{range $val := (iterateFromN 1 ($subCurCount)) }}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) .)}}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) . false)}}
item{{$val}}, ok = item{{sub $val 1}}.Load0(k{{sub $val 1}})
if !ok {
return
......@@ -132,7 +62,7 @@ func (m *{{template "typedef" $curCount}}) Delete{{$subCurCount}}({{range $val :
_ = ok
item0 := m.inner
{{range $val := (iterateFromN 1 ($subCurCount)) }}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) .)}}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) . false)}}
item{{$val}}, ok = item{{sub $val 1}}.Load0(k{{sub $val 1}})
if !ok {
return
......@@ -142,12 +72,12 @@ func (m *{{template "typedef" $curCount}}) Delete{{$subCurCount}}({{range $val :
}
func (m *{{template "typedef" $curCount}}) Range{{$subCurCount}}({{range $val := (iterateFromN 0 $subCurCount) }}k{{$val}} K{{$val}}, {{end}}
f func(K{{$subCurCount}}, {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1))}}) bool) {
f func(K{{$subCurCount}}, {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1) false)}}) bool) {
var ok bool
_ = ok
item0 := m.inner
{{range $val := (iterateFromN 1 ($subCurCount)) }}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) .)}}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) . false)}}
item{{$val}}, ok = item{{sub $val 1}}.Load0(k{{sub $val 1}})
if !ok {
return
......@@ -157,10 +87,10 @@ f func(K{{$subCurCount}}, {{template "reltypedef" (list (sub (sub $curCount $sub
}
func (m *{{template "typedef" $curCount}}) LoadAndDelete{{$subCurCount}}({{range $val := (iterateFromN 0 (add 1 $subCurCount)) }}k{{$val}} K{{$val}}, {{end}}
) (value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1))}}, loaded bool) {
) (value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1) false)}}, loaded bool) {
item0 := m.inner
{{range $val := (iterateFromN 1 ($subCurCount)) }}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) .)}}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) . false)}}
item{{$val}}, loaded = item{{sub $val 1}}.Load0(k{{sub $val 1}})
if !loaded {
return
......@@ -170,21 +100,21 @@ func (m *{{template "typedef" $curCount}}) LoadAndDelete{{$subCurCount}}({{range
}
func (m *{{template "typedef" $curCount}}) LoadOrStore{{$subCurCount}}({{range $val := (iterateFromN 0 (add 1 $subCurCount)) }}k{{$val}} K{{$val}}, {{end}}
v {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1))}}) (value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1))}}, loaded bool) {
v {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1) false)}}) (value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1) false)}}, loaded bool) {
item0 := m.inner
{{range $val := (iterateFromN 1 ($subCurCount)) }}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) .)}}
item{{$val}}, _ = item{{sub $val 1}}.LoadOrStore0(k{{sub $val 1}}, {{template "reltypedef" (list (sub $curCount .) .)}}{})
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) . false)}}
item{{$val}}, _ = item{{sub $val 1}}.LoadOrStore0(k{{sub $val 1}}, {{template "reltypedef" (list (sub $curCount .) . true)}}{})
{{end}}
return item{{$subCurCount}}.LoadOrStore0(k{{$subCurCount}}, v)
}
func (m *{{template "typedef" $curCount}}) Store{{$subCurCount}}({{range $val := (iterateFromN 0 (add 1 $subCurCount)) }}k{{$val}} K{{$val}}, {{end}}
value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1))}}) {
value {{template "reltypedef" (list (sub (sub $curCount $subCurCount) 1) (add $subCurCount 1) false)}}) {
item0 := m.inner
{{range $val := (iterateFromN 1 ($subCurCount)) }}
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) .)}}
item{{$val}}, _ = item{{sub $val 1}}.LoadOrStore0(k{{sub $val 1}}, {{template "reltypedef" (list (sub $curCount .) .)}}{})
var item{{$val}} {{template "reltypedef" (list (sub $curCount .) . false)}}
item{{$val}}, _ = item{{sub $val 1}}.LoadOrStore0(k{{sub $val 1}}, {{template "reltypedef" (list (sub $curCount .) . true)}}{})
{{end}}
item{{$subCurCount}}.Store0(k{{$subCurCount}}, value)
}
......
This diff is collapsed.
......@@ -4,22 +4,26 @@ import (
"sync"
)
// Map is like a Go map[K]V but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.
// Map1 is like a Go map[K]V but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.
//
// The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.
// The Map1 type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map1 may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.
//
// The zero Map is empty and ready for use. A Map must not be copied after first use.
type Map[K comparable, V any] struct {
// The zero Map1 is empty and ready for use. A Map1 must not be copied after first use.
type Map1[K comparable, V any] struct {
inner sync.Map
}
// Delete deletes the value for a key.
func (m *Map[K, V]) Delete(key K) {
func (m *Map1[K, V]) Delete(key K) {
m.inner.Delete(key)
}
func (m *Map1[K, V]) Delete0(key K) {
m.Delete(key)
}
// Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (m *Map[K, V]) Load(key K) (value V, ok bool) {
func (m *Map1[K, V]) Load(key K) (value V, ok bool) {
val, ok := m.inner.Load(key)
if ok {
return val.(V), ok
......@@ -28,8 +32,12 @@ func (m *Map[K, V]) Load(key K) (value V, ok bool) {
return def, ok
}
func (m *Map1[K, V]) Load0(key K) (value V, ok bool) {
return m.Load(key)
}
// LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
func (m *Map1[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
val, loaded := m.inner.LoadAndDelete(key)
if loaded {
return val.(V), loaded
......@@ -38,8 +46,12 @@ func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
return def, loaded
}
func (m *Map1[K, V]) LoadAndDelete0(key K) (value V, loaded bool) {
return m.LoadAndDelete(key)
}
// LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
func (m *Map1[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
val, loaded := m.inner.LoadOrStore(key, value)
if loaded {
return val.(V), loaded
......@@ -48,10 +60,14 @@ func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
return def, loaded
}
func (m *Map1[K, V]) LoadOrStore0(key K, value V) (actual V, loaded bool) {
return m.LoadOrStore(key, value)
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
//
// Range does not necessarily correspond to any consistent snapshot of the Map's
// Range does not necessarily correspond to any consistent snapshot of the Map1's
// contents: no key will be visited more than once, but if the value for any key
// is stored or deleted concurrently (including by f), Range may reflect any
// mapping for that key from any point during the Range call. Range does not
......@@ -59,7 +75,7 @@ func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
//
// Range may be O(N) with the number of elements in the map even if f returns
// false after a constant number of calls.
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
func (m *Map1[K, V]) Range(f func(key K, value V) bool) {
innerFun := func(key, value any) bool {
tKey, tVal := key.(K), value.(V)
return f(tKey, tVal)
......@@ -67,7 +83,15 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) {
m.inner.Range(innerFun)
}
func (m *Map1[K, V]) Range0(f func(key K, value V) bool) {
m.Range(f)
}
// Store sets the value for a key.
func (m *Map[K, V]) Store(key K, value V) {
func (m *Map1[K, V]) Store(key K, value V) {
m.inner.Store(key, value)
}
func (m *Map1[K, V]) Store0(key K, value V) {
m.Store(key, value)
}
{{$count := (index . 0) -}}
{{$offset := (index . 1) -}}
{{$make := (index . 2) -}}
{{if eq $count -1 -}}
V
{{- else -}}
Map{{add 1 $count}}[
{{if $make}}&{{else}}*{{end}}Map{{add 1 $count}}[
{{- range $val := iterateFromN $offset (add 1 $count) -}}
K{{$val}},
{{- end -}}
......
......@@ -2,22 +2,26 @@ import (
"sync"
)
// Map is like a Go map[K]V but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.
// Map1 is like a Go map[K]V but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.
//
// The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.
// The Map1 type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map1 may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.
//
// The zero Map is empty and ready for use. A Map must not be copied after first use.
type Map[K comparable, V any] struct {
// The zero Map1 is empty and ready for use. A Map1 must not be copied after first use.
type Map1[K comparable, V any] struct {
inner sync.Map
}
// Delete deletes the value for a key.
func (m *Map[K, V]) Delete(key K) {
func (m *Map1[K, V]) Delete(key K) {
m.inner.Delete(key)
}
func (m *Map1[K, V]) Delete0(key K) {
m.Delete(key)
}
// Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (m *Map[K, V]) Load(key K) (value V, ok bool) {
func (m *Map1[K, V]) Load(key K) (value V, ok bool) {
val, ok := m.inner.Load(key)
if ok {
return val.(V), ok
......@@ -26,8 +30,12 @@ func (m *Map[K, V]) Load(key K) (value V, ok bool) {
return def, ok
}
func (m *Map1[K, V]) Load0(key K) (value V, ok bool) {
return m.Load(key)
}
// LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
func (m *Map1[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
val, loaded := m.inner.LoadAndDelete(key)
if loaded {
return val.(V), loaded
......@@ -36,8 +44,12 @@ func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
return def, loaded
}
func (m *Map1[K, V]) LoadAndDelete0(key K) (value V, loaded bool) {
return m.LoadAndDelete(key)
}
// LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
func (m *Map1[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
val, loaded := m.inner.LoadOrStore(key, value)
if loaded {
return val.(V), loaded
......@@ -46,10 +58,14 @@ func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
return def, loaded
}
func (m *Map1[K, V]) LoadOrStore0(key K, value V) (actual V, loaded bool) {
return m.LoadOrStore(key, value)
}
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
//
// Range does not necessarily correspond to any consistent snapshot of the Map's
// Range does not necessarily correspond to any consistent snapshot of the Map1's
// contents: no key will be visited more than once, but if the value for any key
// is stored or deleted concurrently (including by f), Range may reflect any
// mapping for that key from any point during the Range call. Range does not
......@@ -57,7 +73,7 @@ func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
//
// Range may be O(N) with the number of elements in the map even if f returns
// false after a constant number of calls.
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
func (m *Map1[K, V]) Range(f func(key K, value V) bool) {
innerFun := func(key, value any) bool {
tKey, tVal := key.(K), value.(V)
return f(tKey, tVal)
......@@ -65,7 +81,15 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) {
m.inner.Range(innerFun)
}
func (m *Map1[K, V]) Range0(f func(key K, value V) bool) {
m.Range(f)
}
// Store sets the value for a key.
func (m *Map[K, V]) Store(key K, value V) {
func (m *Map1[K, V]) Store(key K, value V) {
m.inner.Store(key, value)
}
func (m *Map1[K, V]) Store0(key K, value V) {
m.Store(key, value)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment