summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2020-08-24 17:25:35 +0100
committerPaul Buetow <pbuetow@mimecast.com>2020-08-24 17:25:35 +0100
commitc8f7879655125bcbf18f41897cf3f7e7385693aa (patch)
tree9678045cd73cbcd02d9cc95e7969d580a76b3ef9
parentb6329c9c286b09d2f974fd134cbe3029b1e4e11f (diff)
refactor
-rw-r--r--queue/elementarypriority.go (renamed from queue/elementarypq.go)20
-rw-r--r--queue/priority.go (renamed from queue/pq.go)2
-rw-r--r--queue/queue_test.go (renamed from queue/pq_test.go)12
3 files changed, 17 insertions, 17 deletions
diff --git a/queue/elementarypq.go b/queue/elementarypriority.go
index b66d531..508cbcb 100644
--- a/queue/elementarypq.go
+++ b/queue/elementarypriority.go
@@ -2,28 +2,28 @@ package queue
import "algorithms/ds"
-type ElementaryPQ struct {
+type ElementaryPriority struct {
a ds.ArrayList
size int
// Initial capacity
capacity int
}
-func NewElementaryPQ(capacity int) *ElementaryPQ {
- return &ElementaryPQ{make(ds.ArrayList, 0, capacity), 0, capacity}
+func NewElementaryPriority(capacity int) *ElementaryPriority {
+ return &ElementaryPriority{make(ds.ArrayList, 0, capacity), 0, capacity}
}
-func (q *ElementaryPQ) Insert(a int) {
+func (q *ElementaryPriority) Insert(a int) {
q.a = append(q.a, a)
q.size++
}
-func (q *ElementaryPQ) Max() (max int) {
+func (q *ElementaryPriority) Max() (max int) {
_, max = q.max()
return
}
-func (q *ElementaryPQ) DeleteMax() int {
+func (q *ElementaryPriority) DeleteMax() int {
if q.Empty() {
return 0
}
@@ -37,20 +37,20 @@ func (q *ElementaryPQ) DeleteMax() int {
return max
}
-func (q *ElementaryPQ) Empty() bool {
+func (q *ElementaryPriority) Empty() bool {
return q.Size() == 0
}
-func (q *ElementaryPQ) Size() int {
+func (q *ElementaryPriority) Size() int {
return q.size
}
-func (q *ElementaryPQ) Clear() {
+func (q *ElementaryPriority) Clear() {
q.size = 0
q.a = make(ds.ArrayList, 0, q.capacity)
}
-func (q *ElementaryPQ) max() (ind, max int) {
+func (q *ElementaryPriority) max() (ind, max int) {
for i, a := range q.a {
if a > max {
ind, max = i, a
diff --git a/queue/pq.go b/queue/priority.go
index 5f2079a..86dad68 100644
--- a/queue/pq.go
+++ b/queue/priority.go
@@ -1,6 +1,6 @@
package queue
-type PQ interface {
+type PriorityQueue interface {
Insert(a int)
Max() (max int)
DeleteMax() int
diff --git a/queue/pq_test.go b/queue/queue_test.go
index 76ad187..6be87be 100644
--- a/queue/pq_test.go
+++ b/queue/queue_test.go
@@ -13,14 +13,14 @@ const factor int = 100
// Store results here to avoid compiler optimizations
var benchResult ds.ArrayList
-func TestElementaryPQ(t *testing.T) {
- q := NewElementaryPQ(1)
+func TestElementaryPriority(t *testing.T) {
+ q := NewElementaryPriority(1)
for i := minLength; i <= maxLength; i *= factor {
test(q, i, t)
}
}
-func test(q PQ, l int, t *testing.T) {
+func test(q PriorityQueue, l int, t *testing.T) {
cb := func(t *testing.T) {
t.Parallel()
for _, a := range ds.NewRandomArrayList(l, -1) {
@@ -45,14 +45,14 @@ func test(q PQ, l int, t *testing.T) {
t.Run(fmt.Sprintf("%d", l), cb)
}
-func BenchmarkElementaryPQ(b *testing.B) {
- q := NewElementaryPQ(1)
+func BenchmarkElementaryPriority(b *testing.B) {
+ q := NewElementaryPriority(1)
for i := minLength; i <= maxLength; i *= factor {
benchmark(q, i, b)
}
}
-func benchmark(q PQ, l int, b *testing.B) {
+func benchmark(q PriorityQueue, l int, b *testing.B) {
benchResult = ds.NewRandomArrayList(l, -1)
b.Run(fmt.Sprintf("randomInsert(%d)", l), func(b *testing.B) {