removed unused heap
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Jake Hillion 2021-03-22 19:45:48 +00:00
parent ee5395629e
commit 0cc72ca142
2 changed files with 0 additions and 119 deletions

View File

@ -1,65 +0,0 @@
package utils
import "errors"
var ErrorEmptyHeap = errors.New("attempted to extract from empty heap")
// A MinHeap for Uint64
type Uint32Heap []uint32
func (h *Uint32Heap) swap(x, y int) {
(*h)[x] = (*h)[x] ^ (*h)[y]
(*h)[y] = (*h)[y] ^ (*h)[x]
(*h)[x] = (*h)[x] ^ (*h)[y]
}
func (h *Uint32Heap) Insert(new uint32) uint32 {
*h = append(*h, new)
child := len(*h) - 1
for child != 0 {
parent := (child - 1) / 2
if (*h)[parent] > (*h)[child] {
h.swap(parent, child)
} else {
break
}
child = parent
}
return (*h)[0]
}
func (h *Uint32Heap) Extract() (uint32, error) {
if len(*h) == 0 {
return 0, ErrorEmptyHeap
}
min := (*h)[0]
(*h)[0] = (*h)[len(*h)-1]
*h = (*h)[:len(*h)-1]
parent := 0
for {
left, right := parent*2+1, parent*2+2
if (left < len(*h) && (*h)[parent] > (*h)[left]) || (right < len(*h) && (*h)[parent] > (*h)[right]) {
if right < len(*h) && (*h)[left] > (*h)[right] {
h.swap(parent, right)
parent = right
} else {
h.swap(parent, left)
parent = left
}
} else {
return min, nil
}
}
}
func (h *Uint32Heap) Peek() (uint32, error) {
if len(*h) == 0 {
return 0, ErrorEmptyHeap
}
return (*h)[0], nil
}

View File

@ -1,54 +0,0 @@
package utils
import (
"fmt"
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
"time"
)
func SlowHeapSort(in []uint32) []uint32 {
out := make([]uint32, len(in))
var heap Uint32Heap
for _, x := range in {
heap.Insert(x)
}
for i := range out {
var err error
out[i], err = heap.Extract()
if err != nil {
panic(err)
}
}
return out
}
func TestUint32Heap(t *testing.T) {
t.Run("EquivalentToMerge", func(t *testing.T) {
const ArrayLength = 50
sortedArray := make([]uint32, ArrayLength)
array := make([]uint32, ArrayLength)
for i := range array {
sortedArray[i] = uint32(i)
array[i] = uint32(i)
}
rand.Seed(time.Now().Unix())
for i := 0; i < 100; i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
rand.Shuffle(50, func(i, j int) { array[i], array[j] = array[j], array[i] })
heapSorted := SlowHeapSort(array)
assert.Equal(t, sortedArray, heapSorted)
})
}
})
}