summaryrefslogtreecommitdiff
path: root/lib/containers
diff options
context:
space:
mode:
Diffstat (limited to 'lib/containers')
-rw-r--r--lib/containers/intervaltree.go4
-rw-r--r--lib/containers/intervaltree_test.go3
-rw-r--r--lib/containers/lru.go38
-rw-r--r--lib/containers/rbtree.go25
-rw-r--r--lib/containers/rbtree_test.go10
-rw-r--r--lib/containers/set.go7
-rw-r--r--lib/containers/syncmap.go11
7 files changed, 49 insertions, 49 deletions
diff --git a/lib/containers/intervaltree.go b/lib/containers/intervaltree.go
index 424b297..16bc916 100644
--- a/lib/containers/intervaltree.go
+++ b/lib/containers/intervaltree.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -139,4 +139,4 @@ func (t *IntervalTree[K, V]) SearchAll(fn func(K) int) []V {
return ret
}
-//func (t *IntervalTree[K, V]) Walk(fn func(*RBNode[V]) error) error
+// TODO: func (t *IntervalTree[K, V]) Walk(fn func(*RBNode[V]) error) error
diff --git a/lib/containers/intervaltree_test.go b/lib/containers/intervaltree_test.go
index 88c3003..45743a2 100644
--- a/lib/containers/intervaltree_test.go
+++ b/lib/containers/intervaltree_test.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -35,6 +35,7 @@ func (ival SimpleInterval) String() string {
}
func TestIntervalTree(t *testing.T) {
+ t.Parallel()
tree := IntervalTree[NativeOrdered[int], SimpleInterval]{
MinFn: func(ival SimpleInterval) NativeOrdered[int] { return NativeOrdered[int]{ival.Min} },
MaxFn: func(ival SimpleInterval) NativeOrdered[int] { return NativeOrdered[int]{ival.Max} },
diff --git a/lib/containers/lru.go b/lib/containers/lru.go
index a235a12..aa372ed 100644
--- a/lib/containers/lru.go
+++ b/lib/containers/lru.go
@@ -1,80 +1,70 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package containers
import (
- "sync"
-
lru "github.com/hashicorp/golang-lru"
)
// LRUCache is a least-recently-used(ish) cache. A zero LRUCache is
-// usable and has a cache size of 128 items; use NewLRUCache to set a
-// different size.
+// not usable; it must be initialized with NewLRUCache.
type LRUCache[K comparable, V any] struct {
- initOnce sync.Once
- inner *lru.ARCCache
+ inner *lru.ARCCache
}
func NewLRUCache[K comparable, V any](size int) *LRUCache[K, V] {
c := new(LRUCache[K, V])
- c.initOnce.Do(func() {
- c.inner, _ = lru.NewARC(size)
- })
+ c.inner, _ = lru.NewARC(size)
return c
}
-func (c *LRUCache[K, V]) init() {
- c.initOnce.Do(func() {
- c.inner, _ = lru.NewARC(128)
- })
-}
-
func (c *LRUCache[K, V]) Add(key K, value V) {
- c.init()
c.inner.Add(key, value)
}
+
func (c *LRUCache[K, V]) Contains(key K) bool {
- c.init()
return c.inner.Contains(key)
}
+
func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) {
- c.init()
_value, ok := c.inner.Get(key)
if ok {
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
value = _value.(V)
}
return value, ok
}
+
func (c *LRUCache[K, V]) Keys() []K {
- c.init()
untyped := c.inner.Keys()
typed := make([]K, len(untyped))
for i := range untyped {
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
typed[i] = untyped[i].(K)
}
return typed
}
+
func (c *LRUCache[K, V]) Len() int {
- c.init()
return c.inner.Len()
}
+
func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) {
- c.init()
_value, ok := c.inner.Peek(key)
if ok {
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
value = _value.(V)
}
return value, ok
}
+
func (c *LRUCache[K, V]) Purge() {
- c.init()
c.inner.Purge()
}
+
func (c *LRUCache[K, V]) Remove(key K) {
- c.init()
c.inner.Remove(key)
}
diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go
index e2d8f8a..0430123 100644
--- a/lib/containers/rbtree.go
+++ b/lib/containers/rbtree.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -65,9 +65,9 @@ func (node *RBNode[V]) walk(fn func(*RBNode[V]) error) error {
}
// Search the tree for a value that satisfied the given callbackk
-// function. A return value of 0 means to to return this value; <0
-// means to go left on the tree (the value is too high), >0 means to
-// go right on th etree (the value is too low).
+// function. A return value of 0 means to return this value; <0 means
+// to go left on the tree (the value is too high), >0 means to go
+// right on th etree (the value is too low).
//
// +-----+
// | v=8 | == 0 : this is it
@@ -287,6 +287,8 @@ func (t *RBTree[K, V]) leftRotate(x *RBNode[V]) {
}
func (t *RBTree[K, V]) rightRotate(y *RBNode[V]) {
+ //nolint:dupword
+ //
// | |
// +---+ +---+
// | y | | x |
@@ -336,11 +338,12 @@ func (t *RBTree[K, V]) Insert(val V) {
Parent: parent,
Value: val,
}
- if parent == nil {
+ switch {
+ case parent == nil:
t.root = node
- } else if key.Cmp(t.KeyFn(parent.Value)) < 0 {
+ case key.Cmp(t.KeyFn(parent.Value)) < 0:
parent.Left = node
- } else {
+ default:
parent.Right = node
}
t.updateAttr(node)
@@ -388,10 +391,10 @@ func (t *RBTree[K, V]) Insert(val V) {
t.root.Color = Black
}
-func (t *RBTree[K, V]) transplant(old, new *RBNode[V]) {
- *t.parentChild(old) = new
- if new != nil {
- new.Parent = old.Parent
+func (t *RBTree[K, V]) transplant(oldNode, newNode *RBNode[V]) {
+ *t.parentChild(oldNode) = newNode
+ if newNode != nil {
+ newNode.Parent = oldNode.Parent
}
}
diff --git a/lib/containers/rbtree_test.go b/lib/containers/rbtree_test.go
index a487c52..e42410e 100644
--- a/lib/containers/rbtree_test.go
+++ b/lib/containers/rbtree_test.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -39,13 +39,7 @@ func (node *RBNode[V]) asciiArt(w io.Writer, u, m, l string) {
}
node.Right.asciiArt(w, u+" ", u+" ,--", u+" | ")
-
- if node.Color == Red {
- fmt.Fprintf(w, "%s%v\n", m, node)
- } else {
- fmt.Fprintf(w, "%s%v\n", m, node)
- }
-
+ fmt.Fprintf(w, "%s%v\n", m, node)
node.Left.asciiArt(w, l+" | ", l+" `--", l+" ")
}
diff --git a/lib/containers/set.go b/lib/containers/set.go
index 67ba7ac..4fc8aad 100644
--- a/lib/containers/set.go
+++ b/lib/containers/set.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -22,7 +22,10 @@ var (
_ lowmemjson.Decodable = (*Set[int])(nil)
)
-func cast[T any](x any) T { return x.(T) }
+func cast[T any](x any) T {
+ //nolint:forcetypeassert // Only called within a type switch.
+ return x.(T)
+}
func (o Set[T]) EncodeJSON(w io.Writer) error {
var less func(a, b T) bool
diff --git a/lib/containers/syncmap.go b/lib/containers/syncmap.go
index fb7f59b..74da4b3 100644
--- a/lib/containers/syncmap.go
+++ b/lib/containers/syncmap.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -15,30 +15,39 @@ type SyncMap[K comparable, V any] struct {
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 {
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
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 {
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
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)
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
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 {
+ //nolint:forcetypeassert // Typed wrapper around untyped lib.
return f(key.(K), value.(V))
})
}
+
func (m *SyncMap[K, V]) Store(key K, value V) {
m.inner.Store(key, value)
}