summaryrefslogtreecommitdiff
path: root/queue
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-04-02 20:22:13 +0300
committerPaul Buetow <paul@buetow.org>2023-04-02 20:22:13 +0300
commit0c6d4ed2e499e3e17165e43803d0d1c6dd0956d9 (patch)
tree6d6a5df53d1dd3e655d24f0423f24bc52ad9784c /queue
parentf78ba2cdc6840dbc52a27a2f9fac28f3b61e8b7b (diff)
initial generics
Diffstat (limited to 'queue')
-rw-r--r--queue/elementarypriority.go24
-rw-r--r--queue/heappriority.go24
-rw-r--r--queue/queue_test.go14
3 files changed, 31 insertions, 31 deletions
diff --git a/queue/elementarypriority.go b/queue/elementarypriority.go
index a0bbfba..391d948 100644
--- a/queue/elementarypriority.go
+++ b/queue/elementarypriority.go
@@ -4,26 +4,26 @@ import (
"codeberg.org/snonux/algorithms/ds"
)
-type ElementaryPriority struct {
- a ds.ArrayList
+type ElementaryPriority[T ds.Number] struct {
+ a ds.ArrayList[T]
// Initial capacity
capacity int
}
-func NewElementaryPriority(capacity int) *ElementaryPriority {
- return &ElementaryPriority{make(ds.ArrayList, 0, capacity), capacity}
+func NewElementaryPriority[T ds.Number](capacity int) *ElementaryPriority[T] {
+ return &ElementaryPriority[T]{make(ds.ArrayList[T], 0, capacity), capacity}
}
-func (q *ElementaryPriority) Insert(a int) {
+func (q *ElementaryPriority[T]) Insert(a T) {
q.a = append(q.a, a)
}
-func (q *ElementaryPriority) Max() (max int) {
+func (q *ElementaryPriority[T]) Max() (max T) {
_, max = q.max()
return
}
-func (q *ElementaryPriority) DeleteMax() int {
+func (q *ElementaryPriority[T]) DeleteMax() T {
if q.Empty() {
return 0
}
@@ -37,19 +37,19 @@ func (q *ElementaryPriority) DeleteMax() int {
return max
}
-func (q *ElementaryPriority) Empty() bool {
+func (q *ElementaryPriority[T]) Empty() bool {
return q.Size() == 0
}
-func (q *ElementaryPriority) Size() int {
+func (q *ElementaryPriority[T]) Size() int {
return len(q.a)
}
-func (q *ElementaryPriority) Clear() {
- q.a = make(ds.ArrayList, 0, q.capacity)
+func (q *ElementaryPriority[T]) Clear() {
+ q.a = make(ds.ArrayList[T], 0, q.capacity)
}
-func (q *ElementaryPriority) max() (ind, max int) {
+func (q *ElementaryPriority[T]) max() (ind int, max T) {
for i, a := range q.a {
if a > max {
ind, max = i, a
diff --git a/queue/heappriority.go b/queue/heappriority.go
index b607e2c..bfcae71 100644
--- a/queue/heappriority.go
+++ b/queue/heappriority.go
@@ -4,13 +4,13 @@ import (
"codeberg.org/snonux/algorithms/ds"
)
-type HeapPriority struct {
- ElementaryPriority
+type HeapPriority[T ds.Number] struct {
+ ElementaryPriority[T]
}
-func NewHeapPriority(capacity int) *HeapPriority {
- q := HeapPriority{
- ElementaryPriority: ElementaryPriority{make(ds.ArrayList, 0, capacity), capacity},
+func NewHeapPriority[T ds.Number](capacity int) *HeapPriority[T] {
+ q := HeapPriority[T]{
+ ElementaryPriority: ElementaryPriority[T]{make(ds.ArrayList[T], 0, capacity), capacity},
}
// Index 0 unused
@@ -18,20 +18,20 @@ func NewHeapPriority(capacity int) *HeapPriority {
return &q
}
-func (q *HeapPriority) Empty() bool {
+func (q *HeapPriority[T]) Empty() bool {
return q.Size() == 0
}
-func (q *HeapPriority) Size() int {
+func (q *HeapPriority[T]) Size() int {
return len(q.a) - 1
}
-func (q *HeapPriority) Insert(a int) {
+func (q *HeapPriority[T]) Insert(a T) {
q.a = append(q.a, a)
q.swim(len(q.a) - 1)
}
-func (q *HeapPriority) Max() (max int) {
+func (q *HeapPriority[T]) Max() (max T) {
if q.Empty() {
return 0
}
@@ -39,7 +39,7 @@ func (q *HeapPriority) Max() (max int) {
return q.a[1]
}
-func (q *HeapPriority) DeleteMax() int {
+func (q *HeapPriority[T]) DeleteMax() T {
switch q.Size() {
case 0:
return 0
@@ -60,14 +60,14 @@ func (q *HeapPriority) DeleteMax() int {
}
}
-func (q *HeapPriority) swim(k int) {
+func (q *HeapPriority[T]) swim(k int) {
for k > 1 && q.a[k/2] < q.a[k] {
q.a.Swap(k/2, k)
k = k / 2
}
}
-func (q *HeapPriority) sink(k int) {
+func (q *HeapPriority[T]) sink(k int) {
// Last index
max := len(q.a) - 1
diff --git a/queue/queue_test.go b/queue/queue_test.go
index 3130b42..e3c37b8 100644
--- a/queue/queue_test.go
+++ b/queue/queue_test.go
@@ -12,17 +12,17 @@ const maxLength int = 10000
const factor int = 100
// Store results here to avoid compiler optimizations
-var benchResult ds.ArrayList
+var benchResult ds.ArrayList[int]
func TestElementaryPriority(t *testing.T) {
- q := NewElementaryPriority(1)
+ q := NewElementaryPriority[int](1)
for i := minLength; i <= maxLength; i *= factor {
test(q, i, t)
}
}
func TestHeapPriority(t *testing.T) {
- q := NewHeapPriority(1)
+ q := NewHeapPriority[int](1)
for i := minLength; i <= maxLength; i *= factor {
test(q, i, t)
}
@@ -30,7 +30,7 @@ func TestHeapPriority(t *testing.T) {
func test(q PriorityQueue, l int, t *testing.T) {
cb := func(t *testing.T) {
- for _, a := range ds.NewRandomArrayList(l, -1) {
+ for _, a := range ds.NewRandomArrayList[int](l, -1) {
q.Insert(a)
}
prev, started := 0, false
@@ -53,21 +53,21 @@ func test(q PriorityQueue, l int, t *testing.T) {
}
func BenchmarkElementaryPriority(b *testing.B) {
- q := NewElementaryPriority(1)
+ q := NewElementaryPriority[int](1)
for i := minLength; i <= maxLength; i *= factor {
benchmark(q, i, b)
}
}
func BenchmarkHeapPriority(b *testing.B) {
- q := NewHeapPriority(1)
+ q := NewHeapPriority[int](1)
for i := minLength; i <= maxLength; i *= factor {
benchmark(q, i, b)
}
}
func benchmark(q PriorityQueue, l int, b *testing.B) {
- benchResult = ds.NewRandomArrayList(l, -1)
+ benchResult = ds.NewRandomArrayList[int](l, -1)
b.Run(fmt.Sprintf("randomInsert(%d)", l), func(b *testing.B) {
for i := 0; i < b.N; i++ {