summaryrefslogtreecommitdiff
path: root/lib/containers
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-13 21:22:14 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-13 21:39:42 -0600
commit72a458520fccafe4df8c02c811cb6f64a310616e (patch)
tree1c424b5376c31524b01f8461b02950eb04d48345 /lib/containers
parent952b677bf7f10da93673e3671f764c54c454bbfe (diff)
Move the remaining former-generic.go parts out of lib/util/
Diffstat (limited to 'lib/containers')
-rw-r--r--lib/containers/rbtree.go4
-rw-r--r--lib/containers/syncmap.go40
2 files changed, 42 insertions, 2 deletions
diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go
index 64a679e..0eef553 100644
--- a/lib/containers/rbtree.go
+++ b/lib/containers/rbtree.go
@@ -8,7 +8,7 @@ import (
"fmt"
"reflect"
- "git.lukeshu.com/btrfs-progs-ng/lib/util"
+ "git.lukeshu.com/btrfs-progs-ng/lib/slices"
)
type Color bool
@@ -192,7 +192,7 @@ func (t *RBTree[K, V]) SearchRange(fn func(V) int) []V {
for node := t.Prev(middle); node != nil && fn(node.Value) == 0; node = t.Prev(node) {
ret = append(ret, node.Value)
}
- util.ReverseSlice(ret)
+ slices.Reverse(ret)
for node := t.Next(middle); node != nil && fn(node.Value) == 0; node = t.Next(node) {
ret = append(ret, node.Value)
}
diff --git a/lib/containers/syncmap.go b/lib/containers/syncmap.go
new file mode 100644
index 0000000..6c26b85
--- /dev/null
+++ b/lib/containers/syncmap.go
@@ -0,0 +1,40 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package containers
+
+import (
+ "sync"
+)
+
+type SyncMap[K comparable, V any] struct {
+ inner sync.Map
+}
+
+func (m *SyncMap[K, V]) Delete(key K) { m.inner.Delete(key) }
+func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) {
+ _value, ok := m.inner.Load(key)
+ if ok {
+ value = _value.(V)
+ }
+ return value, ok
+}
+func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
+ _value, ok := m.inner.LoadAndDelete(key)
+ if ok {
+ value = _value.(V)
+ }
+ return value, ok
+}
+func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
+ _actual, loaded := m.inner.LoadOrStore(key, value)
+ actual = _actual.(V)
+ return actual, loaded
+}
+func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) {
+ m.inner.Range(func(key, value any) bool {
+ return f(key.(K), value.(V))
+ })
+}
+func (m *SyncMap[K, V]) Store(key K, value V) { m.inner.Store(key, value) }