summaryrefslogtreecommitdiff
path: root/vendor/github.com/puzpuzpuz/xsync/v3/mapof.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2025-01-27 15:54:51 +0000
committerLibravatar GitHub <noreply@github.com>2025-01-27 15:54:51 +0000
commit3617e27afa181392763258240bc276f3da4b44e2 (patch)
tree9a65f9a361fd9ddf53de7d32ca35c81aade6cb84 /vendor/github.com/puzpuzpuz/xsync/v3/mapof.go
parent[feature/frontend] Add login button to index page which reiterates info about... (diff)
downloadgotosocial-3617e27afa181392763258240bc276f3da4b44e2.tar.xz
bumps uptrace/bun deps to v1.2.8 (#3698)
Diffstat (limited to 'vendor/github.com/puzpuzpuz/xsync/v3/mapof.go')
-rw-r--r--vendor/github.com/puzpuzpuz/xsync/v3/mapof.go53
1 files changed, 48 insertions, 5 deletions
diff --git a/vendor/github.com/puzpuzpuz/xsync/v3/mapof.go b/vendor/github.com/puzpuzpuz/xsync/v3/mapof.go
index 4c4ad0867..cbf9cac91 100644
--- a/vendor/github.com/puzpuzpuz/xsync/v3/mapof.go
+++ b/vendor/github.com/puzpuzpuz/xsync/v3/mapof.go
@@ -149,6 +149,21 @@ func newMapOfTable[K comparable, V any](minTableLen int) *mapOfTable[K, V] {
return t
}
+// ToPlainMapOf returns a native map with a copy of xsync Map's
+// contents. The copied xsync Map should not be modified while
+// this call is made. If the copied Map is modified, the copying
+// behavior is the same as in the Range method.
+func ToPlainMapOf[K comparable, V any](m *MapOf[K, V]) map[K]V {
+ pm := make(map[K]V)
+ if m != nil {
+ m.Range(func(key K, value V) bool {
+ pm[key] = value
+ return true
+ })
+ }
+ return pm
+}
+
// Load returns the value stored in the map for a key, or zero value
// of type V if no value is present.
// The ok result indicates whether value was found in the map.
@@ -243,6 +258,34 @@ func (m *MapOf[K, V]) LoadOrCompute(key K, valueFn func() V) (actual V, loaded b
)
}
+// LoadOrTryCompute returns the existing value for the key if present.
+// Otherwise, it tries to compute the value using the provided function
+// and, if success, returns the computed value. The loaded result is true
+// if the value was loaded, false if stored. If the compute attempt was
+// cancelled, a zero value of type V will be returned.
+//
+// This call locks a hash table bucket while the compute function
+// is executed. It means that modifications on other entries in
+// the bucket will be blocked until the valueFn executes. Consider
+// this when the function includes long-running operations.
+func (m *MapOf[K, V]) LoadOrTryCompute(
+ key K,
+ valueFn func() (newValue V, cancel bool),
+) (value V, loaded bool) {
+ return m.doCompute(
+ key,
+ func(V, bool) (V, bool) {
+ nv, c := valueFn()
+ if !c {
+ return nv, false
+ }
+ return nv, true // nv is ignored
+ },
+ true,
+ false,
+ )
+}
+
// Compute either sets the computed new value for the key or deletes
// the value for the key. When the delete result of the valueFn function
// is set to true, the value will be deleted, if it exists. When delete
@@ -390,11 +433,11 @@ func (m *MapOf[K, V]) doCompute(
if b.next == nil {
if emptyb != nil {
// Insertion into an existing bucket.
- var zeroedV V
- newValue, del := valueFn(zeroedV, false)
+ var zeroV V
+ newValue, del := valueFn(zeroV, false)
if del {
rootb.mu.Unlock()
- return zeroedV, false
+ return zeroV, false
}
newe := new(entryOf[K, V])
newe.key = key
@@ -414,8 +457,8 @@ func (m *MapOf[K, V]) doCompute(
goto compute_attempt
}
// Insertion into a new bucket.
- var zeroedV V
- newValue, del := valueFn(zeroedV, false)
+ var zeroV V
+ newValue, del := valueFn(zeroV, false)
if del {
rootb.mu.Unlock()
return newValue, false