Skip to content

Commit

Permalink
Clean code and fix issues reported by staticcheck
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Arroutbi <sarroutb@redhat.com>
  • Loading branch information
sarroutbi committed May 26, 2023
1 parent 68e77ee commit 498236a
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 109 deletions.
2 changes: 1 addition & 1 deletion augmentedtree/atree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func TestTraverse(t *testing.T) {
found[id.ID()] = true
})
for i := 0; i <= top; i++ {
if found, _ := found[uint64(i)]; !found {
if found := found[uint64(i)]; !found {
t.Errorf("could not find expected interval %d", i)
}
}
Expand Down
8 changes: 2 additions & 6 deletions bitarray/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ func (ba *bitArray) Equals(other BitArray) bool {
}

lastIndex, _ := getIndexAndRemainder(ba.highest)
if lastIndex >= selfIndex {
return false
}

return true
return lastIndex < selfIndex
}

// Intersects returns a bool indicating if the supplied bitarray intersects
Expand Down Expand Up @@ -371,7 +367,7 @@ func newBitArray(size uint64, args ...bool) *bitArray {
anyset: false,
}

if len(args) > 0 && args[0] == true {
if len(args) > 0 && args[0] {
for i := uint64(0); i < uint64(len(ba.blocks)); i++ {
ba.blocks[i] = maximumBlock
}
Expand Down
4 changes: 2 additions & 2 deletions bitarray/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBlockToNums(t *testing.T) {

expected := []uint64{s - 6, s - 2}

result := make([]uint64, 0, 0)
result := make([]uint64, 0)
b.toNums(0, &result)
assert.Equal(t, expected, result)
}
Expand All @@ -41,7 +41,7 @@ func BenchmarkBlockToNums(b *testing.B) {
block = block.insert(i)
}

nums := make([]uint64, 0, 0)
nums := make([]uint64, 0)
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
8 changes: 4 additions & 4 deletions bitarray/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
var bytesRead int
intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
return errors.New("invalid data for BitArray")
}
curLoc += intsize

Expand All @@ -131,15 +131,15 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
for i := uint64(0); i < intsToRead; i++ {
nextblock, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
return errors.New("invalid data for BitArray")
}
ret.blocks[i] = block(nextblock)
curLoc += intsize
}

intsToRead, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
return errors.New("invalid data for BitArray")
}
curLoc += intsize

Expand All @@ -148,7 +148,7 @@ func (ret *sparseBitArray) Deserialize(incoming []byte) error {
for i := uint64(0); i < intsToRead; i++ {
nextuint, bytesRead = Uint64FromBytes(incoming[curLoc : curLoc+intsize])
if bytesRead < 0 {
return errors.New("Invalid data for BitArray")
return errors.New("invalid data for BitArray")
}
ret.indices[i] = nextuint
curLoc += intsize
Expand Down
2 changes: 1 addition & 1 deletion bitarray/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestUnmarshalErrors(t *testing.T) {
}
}

outputBytes, err := Marshal(input)
outputBytes, _ := Marshal(input)

outputBytes[0] = 'C'

Expand Down
4 changes: 2 additions & 2 deletions btree/immutable/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (t *Tr) delete(keys Keys) error {

pb := path.peek()
node := pb.n
isRoot := bytes.Compare(node.ID, t.Root) == 0
isRoot := bytes.Equal(node.ID, t.Root)
if !t.context.nodeExists(node.ID) {
cp := node.copy()
t.context.addNode(cp)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (t *Tr) delete(keys Keys) error {
for pb.prev != nil {
parentBundle := pb.prev
parent := parentBundle.n
isRoot := bytes.Compare(parent.ID, t.Root) == 0
isRoot := bytes.Equal(parent.ID, t.Root)
if !t.context.nodeExists(parent.ID) {
cp := parent.copy()
t.context.addNode(cp)
Expand Down
8 changes: 0 additions & 8 deletions btree/immutable/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,3 @@ type Item struct {
}

type items []*Item

func (its items) split(numParts int) []items {
parts := make([]items, numParts)
for i := int64(0); i < int64(numParts); i++ {
parts[i] = its[i*int64(len(its))/int64(numParts) : (i+1)*int64(len(its))/int64(numParts)]
}
return parts
}
6 changes: 3 additions & 3 deletions btree/immutable/rt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func TestNodeSplit(t *testing.T) {

rt, err = mutable.Commit()
require.NoError(t, err)
rt, err = Load(cfg.Persister, rt.ID(), comparator)
rt, _ = Load(cfg.Persister, rt.ID(), comparator)

result, err = mutable.(*Tr).toList(itemsToValues(items...)...)
require.NoError(t, err)
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestRandom(t *testing.T) {

require.NoError(t, err)
expected := toOrdered(items).toItems()
result, err := mutable.(*Tr).toList(itemsToValues(expected...)...)
result, _ := mutable.(*Tr).toList(itemsToValues(expected...)...)
if !assert.Equal(t, expected, result) {
assert.Equal(t, len(expected), len(result))
for i, c := range expected {
Expand Down Expand Up @@ -762,7 +762,7 @@ func TestSecondCommitMultipleSplits(t *testing.T) {
mutable := rt.AsMutable()
mutable.AddItems(items[:25]...)
mutable.(*Tr).verify(mutable.(*Tr).Root, t)
rt, err := mutable.Commit()
rt, _ = mutable.Commit()
rt.(*Tr).verify(rt.(*Tr).Root, t)

result, err := rt.(*Tr).toList(itemsToValues(items...)...)
Expand Down
1 change: 0 additions & 1 deletion btree/palm/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func (ga *getAction) keys() common.Comparators {
}

func (ga *getAction) addNode(i int64, n *node) {
return // not necessary for gets
}

func (ga *getAction) nodes() []*node {
Expand Down
12 changes: 6 additions & 6 deletions fibheap/fibheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ func (heap *FloatingFibonacciHeap) DequeueMin() (*Entry, error) {
func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64) (*Entry, error) {

if heap.IsEmpty() {
return nil, EmptyHeapError("Cannot decrease key in an empty heap")
return nil, EmptyHeapError("cannot decrease key in an empty heap")
}

if node == nil {
return nil, NilError("Cannot decrease key: given node is nil")
return nil, NilError("cannot decrease key: given node is nil")
}

if newPriority >= node.Priority {
return nil, fmt.Errorf("The given new priority: %v, is larger than or equal to the old: %v",
return nil, fmt.Errorf("the given new priority: %v, is larger than or equal to the old: %v",
newPriority, node.Priority)
}

Expand All @@ -271,11 +271,11 @@ func (heap *FloatingFibonacciHeap) DecreaseKey(node *Entry, newPriority float64)
func (heap *FloatingFibonacciHeap) Delete(node *Entry) error {

if heap.IsEmpty() {
return EmptyHeapError("Cannot delete element from an empty heap")
return EmptyHeapError("cannot delete element from an empty heap")
}

if node == nil {
return NilError("Cannot delete node: given node is nil")
return NilError("cannot delete node: given node is nil")
}

decreaseKeyUnchecked(heap, node, -math.MaxFloat64)
Expand All @@ -291,7 +291,7 @@ func (heap *FloatingFibonacciHeap) Delete(node *Entry) error {
func (heap *FloatingFibonacciHeap) Merge(other *FloatingFibonacciHeap) (FloatingFibonacciHeap, error) {

if heap == nil || other == nil {
return FloatingFibonacciHeap{}, NilError("One of the heaps to merge is nil. Cannot merge")
return FloatingFibonacciHeap{}, NilError("one of the heaps to merge is nil. Cannot merge")
}

resultSize := heap.size + other.size
Expand Down
19 changes: 9 additions & 10 deletions fibheap/fibheap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func TestFibHeap_Min_EmptyHeap(t *testing.T) {
heap := NewFloatFibHeap()

heap.Enqueue(0)
min, err := heap.DequeueMin()
_, err := heap.DequeueMin()
require.NoError(t, err)

// Heap should be empty at this point

min, err = heap.Min()
min, err := heap.Min()

assert.EqualError(t, err, "Trying to get minimum element of empty heap")
assert.Nil(t, min)
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestFibHeap_DecreaseKey_EmptyHeap(t *testing.T) {
min, err := heap.DecreaseKey(elem, 0)

assert.IsType(t, EmptyHeapError(""), err)
assert.EqualError(t, err, "Cannot decrease key in an empty heap")
assert.EqualError(t, err, "cannot decrease key in an empty heap")
assert.Nil(t, min)
}

Expand All @@ -238,7 +238,7 @@ func TestFibHeap_DecreaseKey_NilNode(t *testing.T) {
min, err := heap.DecreaseKey(nil, 0)

assert.IsType(t, NilError(""), err)
assert.EqualError(t, err, "Cannot decrease key: given node is nil")
assert.EqualError(t, err, "cannot decrease key: given node is nil")
assert.Nil(t, min)
}

Expand All @@ -247,7 +247,7 @@ func TestFibHeap_DecreaseKey_LargerNewPriority(t *testing.T) {
node := heap.Enqueue(1)
min, err := heap.DecreaseKey(node, 20)

assert.EqualError(t, err, "The given new priority: 20, is larger than or equal to the old: 1")
assert.EqualError(t, err, "the given new priority: 20, is larger than or equal to the old: 1")
assert.Nil(t, min)
}

Expand Down Expand Up @@ -296,15 +296,15 @@ func TestFibHeap_Delete_EmptyHeap(t *testing.T) {
// Heap should be empty at this point
err := heap.Delete(elem)
assert.IsType(t, EmptyHeapError(""), err)
assert.EqualError(t, err, "Cannot delete element from an empty heap")
assert.EqualError(t, err, "cannot delete element from an empty heap")
}

func TestFibHeap_Delete_NilNode(t *testing.T) {
heap := NewFloatFibHeap()
heap.Enqueue(1)
err := heap.Delete(nil)
assert.IsType(t, NilError(""), err)
assert.EqualError(t, err, "Cannot delete node: given node is nil")
assert.EqualError(t, err, "cannot delete node: given node is nil")
}

func TestMerge(t *testing.T) {
Expand All @@ -330,11 +330,10 @@ func TestMerge(t *testing.T) {
}

func TestFibHeap_Merge_NilHeap(t *testing.T) {
var heap FloatingFibonacciHeap
heap = NewFloatFibHeap()
heap := NewFloatFibHeap()
newHeap, err := heap.Merge(nil)
assert.IsType(t, NilError(""), err)
assert.EqualError(t, err, "One of the heaps to merge is nil. Cannot merge")
assert.EqualError(t, err, "one of the heaps to merge is nil. Cannot merge")
assert.Equal(t, newHeap, FloatingFibonacciHeap{})
}

Expand Down
4 changes: 2 additions & 2 deletions graph/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func (g *SimpleGraph) Degree(v interface{}) (int, error) {
}

func (g *SimpleGraph) addVertex(v interface{}) {
mm, ok := g.adjacencyList[v]
_, ok := g.adjacencyList[v]
if !ok {
mm = make(map[interface{}]struct{})
mm := make(map[interface{}]struct{})
g.adjacencyList[v] = mm
g.v++
}
Expand Down
5 changes: 1 addition & 4 deletions hashmap/fastinteger/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,13 @@ func BenchmarkGoMapExists(b *testing.B) {

b.ResetTimer()

var ok bool
for i := 0; i < b.N; i++ {
for _, key := range keys {
_, ok = hm[key] // or the compiler complains
_, _ = hm[key] // or the compiler complains
}
}

b.StopTimer()
if ok { // or the compiler complains
}
}

func BenchmarkDelete(b *testing.B) {
Expand Down
4 changes: 0 additions & 4 deletions rangetree/immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func newCache(dimensions uint64) []slice.Int64Slice {
return cache
}

func (irt *immutableRangeTree) needNextDimension() bool {
return irt.dimensions > 1
}

func (irt *immutableRangeTree) add(nodes *orderedNodes, cache []slice.Int64Slice, entry Entry, added *uint64) {
var node *node
list := nodes
Expand Down
4 changes: 1 addition & 3 deletions rtree/hilbert/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ func (ga *getAction) keys() hilberts {
return nil
}

func (ga *getAction) addNode(i int64, n *node) {
return // not necessary for gets
}
func (ga *getAction) addNode(i int64, n *node) {}

func (ga *getAction) nodes() []*node {
return nil
Expand Down
34 changes: 0 additions & 34 deletions rtree/hilbert/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,6 @@ func (ks *keys) search(key hilbert) uint64 {
return uint64(i)
}

func (ks *keys) insert(key hilbert) (hilbert, uint64) {
i := ks.search(key)
if i == uint64(len(ks.list)) {
ks.list = append(ks.list, key)
return -1, i
}

var old hilbert
if ks.list[i] == key {
old = ks.list[i]
ks.list[i] = key
} else {
ks.insertAt(i, key)
}

return old, i
}

func (ks *keys) last() hilbert {
return ks.list[len(ks.list)-1]
}
Expand All @@ -190,18 +172,6 @@ func (ks *keys) insertAt(i uint64, k hilbert) {
ks.list[i] = k
}

func (ks *keys) withPosition(k hilbert) (hilbert, uint64) {
i := ks.search(k)
if i == uint64(len(ks.list)) {
return -1, i
}
if ks.list[i] == k {
return ks.list[i], i
}

return -1, i
}

func newKeys(size uint64) *keys {
return &keys{
list: make(hilberts, 0, size),
Expand Down Expand Up @@ -350,10 +320,6 @@ func (n *node) searchRects(r *rectangle) rtree.Rectangles {
return rects
}

func (n *node) key() hilbert {
return n.keys.last()
}

func newNode(isLeaf bool, keys *keys, ns *nodes) *node {
return &node{
isLeaf: isLeaf,
Expand Down
2 changes: 0 additions & 2 deletions slice/skip/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package skip

import "github.com/Workiva/go-datastructures/common"

const iteratorExhausted = -2

// iterator represents an object that can be iterated. It will
// return false on Next and nil on Value if there are no further
// values to be iterated.
Expand Down
4 changes: 0 additions & 4 deletions slice/skip/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,3 @@ func (mi *mockIterator) Value() common.Comparator {

return result
}

func (mi *mockIterator) exhaust() common.Comparators {
return nil
}
Loading

0 comments on commit 498236a

Please sign in to comment.