From 1faafcd3809fe5b452a1a742137ca2cb7336aad6 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 17:39:11 -0700 Subject: lint: Turn on dupword --- lib/containers/rbtree.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib/containers') diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go index e2d8f8a..17bb3e3 100644 --- a/lib/containers/rbtree.go +++ b/lib/containers/rbtree.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // 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 | -- cgit v1.2.3-2-g168b From 493ec396fab32d9e8859e34ad497fdb0a910a33c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 17:49:11 -0700 Subject: lint: Turn on forcetypeassert --- lib/containers/lru.go | 5 ++++- lib/containers/set.go | 7 +++++-- lib/containers/syncmap.go | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) (limited to 'lib/containers') diff --git a/lib/containers/lru.go b/lib/containers/lru.go index a235a12..12446b0 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -44,6 +44,7 @@ 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 @@ -53,6 +54,7 @@ func (c *LRUCache[K, V]) Keys() []K { 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 @@ -65,6 +67,7 @@ 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 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 +// Copyright (C) 2022-2023 Luke Shumaker // // 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..4af678f 100644 --- a/lib/containers/syncmap.go +++ b/lib/containers/syncmap.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -18,6 +18,7 @@ func (m *SyncMap[K, V]) Delete(key K) { 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 @@ -25,17 +26,20 @@ func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { 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)) }) } -- cgit v1.2.3-2-g168b From c307e7048da3c02e1e540eab227def7fec7340cc Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 19:27:49 -0700 Subject: lint: Turn on gocritic --- lib/containers/intervaltree.go | 4 ++-- lib/containers/rbtree.go | 7 ++++--- lib/containers/rbtree_test.go | 10 ++-------- 3 files changed, 8 insertions(+), 13 deletions(-) (limited to 'lib/containers') 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 +// Copyright (C) 2022-2023 Luke Shumaker // // 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/rbtree.go b/lib/containers/rbtree.go index 17bb3e3..f922a7b 100644 --- a/lib/containers/rbtree.go +++ b/lib/containers/rbtree.go @@ -338,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) 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 +// Copyright (C) 2022-2023 Luke Shumaker // // 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+" ") } -- cgit v1.2.3-2-g168b From a06a7fb2d5bbf1ca5659de06fc9e975666bdcf9f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 19:33:03 -0700 Subject: lint: Turn on gofumpt All edits to .go files are made by `tools/bin/golangci-lint run --fix ./...`, not by me as a human. --- lib/containers/lru.go | 7 +++++++ lib/containers/syncmap.go | 5 +++++ 2 files changed, 12 insertions(+) (limited to 'lib/containers') diff --git a/lib/containers/lru.go b/lib/containers/lru.go index 12446b0..bfda361 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -36,10 +36,12 @@ 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) @@ -49,6 +51,7 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { } return value, ok } + func (c *LRUCache[K, V]) Keys() []K { c.init() untyped := c.inner.Keys() @@ -59,10 +62,12 @@ func (c *LRUCache[K, V]) Keys() []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) @@ -72,10 +77,12 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { } 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/syncmap.go b/lib/containers/syncmap.go index 4af678f..74da4b3 100644 --- a/lib/containers/syncmap.go +++ b/lib/containers/syncmap.go @@ -15,6 +15,7 @@ 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 { @@ -23,6 +24,7 @@ func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { } return value, ok } + func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { _value, ok := m.inner.LoadAndDelete(key) if ok { @@ -31,18 +33,21 @@ func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { } 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) } -- cgit v1.2.3-2-g168b From b261c2a4f5f028c9d83cef208ccc7d829f841bad Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 31 Dec 2022 11:59:09 -0700 Subject: tree-wide: Annotate values that I might want to be tuning --- lib/containers/lru.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) (limited to 'lib/containers') diff --git a/lib/containers/lru.go b/lib/containers/lru.go index bfda361..aa372ed 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -5,45 +5,30 @@ 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. @@ -53,7 +38,6 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { } func (c *LRUCache[K, V]) Keys() []K { - c.init() untyped := c.inner.Keys() typed := make([]K, len(untyped)) for i := range untyped { @@ -64,12 +48,10 @@ func (c *LRUCache[K, V]) Keys() []K { } 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. @@ -79,12 +61,10 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { } 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) } -- cgit v1.2.3-2-g168b From 5edd0d7be38595c4777d5130ca20f907d8a74963 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 21:12:41 -0700 Subject: lint: Turn on paralleltest --- lib/containers/intervaltree_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/containers') 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 +// Copyright (C) 2022-2023 Luke Shumaker // // 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} }, -- cgit v1.2.3-2-g168b From 0c706b6ad52776430ac15cf0e286dd473091d93e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 21:14:26 -0700 Subject: lint: Turn on predeclared --- lib/containers/rbtree.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/containers') diff --git a/lib/containers/rbtree.go b/lib/containers/rbtree.go index f922a7b..0430123 100644 --- a/lib/containers/rbtree.go +++ b/lib/containers/rbtree.go @@ -391,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 } } -- cgit v1.2.3-2-g168b