Implements PriorityQueue

pull/203/head
Emir Pasic 2 years ago
parent 6b0ffefe7f
commit 8ace639fc3

@ -40,6 +40,7 @@ Implementation of various data structures and algorithms in Go.
- [LinkedListQueue](#linkedlistqueue)
- [ArrayQueue](#arrayqueue)
- [CircularBuffer](#circularbuffer)
- [PriorityQueue](#priorityqueue)
- [Functions](#functions)
- [Comparator](#comparator)
- [Iterator](#iterator)
@ -102,6 +103,7 @@ Containers are either ordered or unordered. All ordered containers provide [stat
| | [LinkedListQueue](#linkedlistqueue) | yes | yes | no | index |
| | [ArrayQueue](#arrayqueue) | yes | yes* | no | index |
| | [CircularBuffer](#circularbuffer) | yes | yes* | no | index |
| | [PriorityQueue](#priorityqueue) | yes | yes* | no | index |
| | | | <sub><sup>*reversible</sup></sub> | | <sub><sup>*bidirectional</sup></sub> |
### Lists
@ -984,6 +986,55 @@ func main() {
}
```
#### PriorityQueue
A priority queue is a special type of [queue](#queues) in which each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher priority elements are served first. However, if elements with the same priority occur, they are served according to their order in the queue.
Implements [Queue](#queues), [ReverseIteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.
```go
package main
import (
pq "github.com/emirpasic/gods/queues/priorityqueue"
"github.com/emirpasic/gods/utils"
)
// Element is an entry in the priority queue
type Element struct {
name string
priority int
}
// Comparator function (sort by element's priority value in descending order)
func byPriority(a, b interface{}) int {
priorityA := a.(Element).priority
priorityB := b.(Element).priority
return -utils.IntComparator(priorityA, priorityB) // "-" descending order
}
// PriorityQueueExample to demonstrate basic usage of BinaryHeap
func main() {
a := Element{name: "a", priority: 1}
b := Element{name: "b", priority: 2}
c := Element{name: "c", priority: 3}
queue := pq.NewWith(byPriority) // empty
queue.Enqueue(a) // {a 1}
queue.Enqueue(c) // {c 3}, {a 1}
queue.Enqueue(b) // {c 3}, {b 2}, {a 1}
_ = queue.Values() // [{c 3} {b 2} {a 1}]
_, _ = queue.Peek() // {c 3} true
_, _ = queue.Dequeue() // {c 3} true
_, _ = queue.Dequeue() // {b 2} true
_, _ = queue.Dequeue() // {a 1} true
_, _ = queue.Dequeue() // <nil> false (nothing to dequeue)
queue.Clear() // empty
_ = queue.Empty() // true
_ = queue.Size() // 0
}
```
## Functions
Various helper functions used throughout the library.

@ -0,0 +1,44 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
pq "github.com/emirpasic/gods/queues/priorityqueue"
"github.com/emirpasic/gods/utils"
)
// Element is an entry in the priority queue
type Element struct {
name string
priority int
}
// Comparator function (sort by element's priority value in descending order)
func byPriority(a, b interface{}) int {
priorityA := a.(Element).priority
priorityB := b.(Element).priority
return -utils.IntComparator(priorityA, priorityB) // "-" descending order
}
// PriorityQueueExample to demonstrate basic usage of BinaryHeap
func main() {
a := Element{name: "a", priority: 1}
b := Element{name: "b", priority: 2}
c := Element{name: "c", priority: 3}
queue := pq.NewWith(byPriority) // empty
queue.Enqueue(a) // {a 1}
queue.Enqueue(c) // {c 3}, {a 1}
queue.Enqueue(b) // {c 3}, {b 2}, {a 1}
_ = queue.Values() // [{c 3} {b 2} {a 1}]
_, _ = queue.Peek() // {c 3} true
_, _ = queue.Dequeue() // {c 3} true
_, _ = queue.Dequeue() // {b 2} true
_, _ = queue.Dequeue() // {a 1} true
_, _ = queue.Dequeue() // <nil> false (nothing to dequeue)
queue.Clear() // empty
_ = queue.Empty() // true
_ = queue.Size() // 0
}

@ -0,0 +1,92 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package priorityqueue
import (
"github.com/emirpasic/gods/containers"
"github.com/emirpasic/gods/trees/binaryheap"
)
// Assert Iterator implementation
var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
// Iterator returns a stateful iterator whose values can be fetched by an index.
type Iterator struct {
iterator binaryheap.Iterator
}
// Iterator returns a stateful iterator whose values can be fetched by an index.
func (queue *Queue) Iterator() Iterator {
return Iterator{iterator: queue.heap.Iterator()}
}
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
return iterator.iterator.Next()
}
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) Prev() bool {
return iterator.iterator.Prev()
}
// Value returns the current element's value.
// Does not modify the state of the iterator.
func (iterator *Iterator) Value() interface{} {
return iterator.iterator.Value()
}
// Index returns the current element's index.
// Does not modify the state of the iterator.
func (iterator *Iterator) Index() int {
return iterator.iterator.Index()
}
// Begin resets the iterator to its initial state (one-before-first)
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Begin() {
iterator.iterator.Begin()
}
// End moves the iterator past the last element (one-past-the-end).
// Call Prev() to fetch the last element if any.
func (iterator *Iterator) End() {
iterator.iterator.End()
}
// First moves the iterator to the first element and returns true if there was a first element in the container.
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) First() bool {
return iterator.iterator.First()
}
// Last moves the iterator to the last element and returns true if there was a last element in the container.
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) Last() bool {
return iterator.iterator.Last()
}
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) NextTo(f func(index int, value interface{}) bool) bool {
return iterator.iterator.NextTo(f)
}
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
// passed function, and returns true if there was a next element in the container.
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator.
func (iterator *Iterator) PrevTo(f func(index int, value interface{}) bool) bool {
return iterator.iterator.PrevTo(f)
}

@ -0,0 +1,86 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package priorityqueue implements a priority queue backed by binary queue.
//
// An unbounded priority queue based on a priority queue.
// The elements of the priority queue are ordered by a comparator provided at queue construction time.
//
// The heap of this queue is the least/smallest element with respect to the specified ordering.
// If multiple elements are tied for least value, the heap is one of those elements arbitrarily.
//
// Structure is not thread safe.
//
// References: https://en.wikipedia.org/wiki/Priority_queue
package priorityqueue
import (
"fmt"
"github.com/emirpasic/gods/queues"
"github.com/emirpasic/gods/trees/binaryheap"
"github.com/emirpasic/gods/utils"
"strings"
)
// Assert Queue implementation
var _ queues.Queue = (*Queue)(nil)
// Queue holds elements in an array-list
type Queue struct {
heap *binaryheap.Heap
Comparator utils.Comparator
}
// NewWith instantiates a new empty queue with the custom comparator.
func NewWith(comparator utils.Comparator) *Queue {
return &Queue{heap: binaryheap.NewWith(comparator), Comparator: comparator}
}
// Enqueue adds a value to the end of the queue
func (queue *Queue) Enqueue(value interface{}) {
queue.heap.Push(value)
}
// Dequeue removes first element of the queue and returns it, or nil if queue is empty.
// Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
func (queue *Queue) Dequeue() (value interface{}, ok bool) {
return queue.heap.Pop()
}
// Peek returns top element on the queue without removing it, or nil if queue is empty.
// Second return parameter is true, unless the queue was empty and there was nothing to peek.
func (queue *Queue) Peek() (value interface{}, ok bool) {
return queue.heap.Peek()
}
// Empty returns true if queue does not contain any elements.
func (queue *Queue) Empty() bool {
return queue.heap.Empty()
}
// Size returns number of elements within the queue.
func (queue *Queue) Size() int {
return queue.heap.Size()
}
// Clear removes all elements from the queue.
func (queue *Queue) Clear() {
queue.heap.Clear()
}
// Values returns all elements in the queue.
func (queue *Queue) Values() []interface{} {
return queue.heap.Values()
}
// String returns a string representation of container
func (queue *Queue) String() string {
str := "PriorityQueue\n"
values := make([]string, queue.heap.Size(), queue.heap.Size())
for index, value := range queue.heap.Values() {
values[index] = fmt.Sprintf("%v", value)
}
str += strings.Join(values, ", ")
return str
}

@ -0,0 +1,570 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package priorityqueue
import (
"encoding/json"
"fmt"
"github.com/emirpasic/gods/utils"
"math/rand"
"strings"
"testing"
)
type Element struct {
priority int
name string
}
func (element Element) String() string {
return fmt.Sprintf("{%v %v}", element.priority, element.name)
}
// Comparator function (sort by priority value in descending order)
func byPriority(a, b interface{}) int {
return -utils.IntComparator( // Note "-" for descending order
a.(Element).priority,
b.(Element).priority,
)
}
func TestBinaryQueueEnqueue(t *testing.T) {
queue := NewWith(byPriority)
if actualValue := queue.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
a := Element{name: "a", priority: 1}
c := Element{name: "c", priority: 3}
b := Element{name: "b", priority: 2}
queue.Enqueue(a)
queue.Enqueue(c)
queue.Enqueue(b)
it := queue.Iterator()
count := 0
for it.Next() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value.(Element).name, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value.(Element).name, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value.(Element).name, "a"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
if actualValue := queue.Values(); actualValue[0].(Element).name != "c" || actualValue[1].(Element).name != "b" || actualValue[2].(Element).name != "a" {
t.Errorf("Got %v expected %v", actualValue, `[{3 c} {2 b} {1 a}]`)
}
}
func TestBinaryQueueEnqueueBulk(t *testing.T) {
queue := NewWith(utils.IntComparator)
queue.Enqueue(15)
queue.Enqueue(20)
queue.Enqueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
if actualValue, ok := queue.Dequeue(); actualValue != 1 || !ok {
t.Errorf("Got %v expected %v", actualValue, 1)
}
if actualValue, ok := queue.Dequeue(); actualValue != 2 || !ok {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := queue.Dequeue(); actualValue != 3 || !ok {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := queue.Dequeue(); actualValue != 15 || !ok {
t.Errorf("Got %v expected %v", actualValue, 15)
}
if actualValue, ok := queue.Dequeue(); actualValue != 20 || !ok {
t.Errorf("Got %v expected %v", actualValue, 20)
}
}
func TestBinaryQueueDequeue(t *testing.T) {
queue := NewWith(utils.IntComparator)
if actualValue := queue.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
queue.Enqueue(3)
queue.Enqueue(2)
queue.Enqueue(1)
queue.Dequeue() // removes 1
if actualValue, ok := queue.Dequeue(); actualValue != 2 || !ok {
t.Errorf("Got %v expected %v", actualValue, 2)
}
if actualValue, ok := queue.Dequeue(); actualValue != 3 || !ok {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := queue.Dequeue(); actualValue != nil || ok {
t.Errorf("Got %v expected %v", actualValue, nil)
}
if actualValue := queue.Empty(); actualValue != true {
t.Errorf("Got %v expected %v", actualValue, true)
}
if actualValue := queue.Values(); len(actualValue) != 0 {
t.Errorf("Got %v expected %v", actualValue, "[]")
}
}
func TestBinaryQueueRandom(t *testing.T) {
queue := NewWith(utils.IntComparator)
rand.Seed(3)
for i := 0; i < 10000; i++ {
r := int(rand.Int31n(30))
queue.Enqueue(r)
}
prev, _ := queue.Dequeue()
for !queue.Empty() {
curr, _ := queue.Dequeue()
if prev.(int) > curr.(int) {
t.Errorf("Queue property invalidated. prev: %v current: %v", prev, curr)
}
prev = curr
}
}
func TestBinaryQueueIteratorOnEmpty(t *testing.T) {
queue := NewWith(utils.IntComparator)
it := queue.Iterator()
for it.Next() {
t.Errorf("Shouldn't iterate on empty queue")
}
}
func TestBinaryQueueIteratorNext(t *testing.T) {
queue := NewWith(utils.IntComparator)
queue.Enqueue(3)
queue.Enqueue(2)
queue.Enqueue(1)
it := queue.Iterator()
count := 0
for it.Next() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, 1; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, 2; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestBinaryQueueIteratorPrev(t *testing.T) {
queue := NewWith(utils.IntComparator)
queue.Enqueue(3)
queue.Enqueue(2)
queue.Enqueue(1)
it := queue.Iterator()
for it.Next() {
}
count := 0
for it.Prev() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, 1; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, 2; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
default:
t.Errorf("Too many")
}
if actualValue, expectedValue := index, 3-count; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
}
func TestBinaryQueueIteratorBegin(t *testing.T) {
queue := NewWith(utils.IntComparator)
it := queue.Iterator()
it.Begin()
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(1)
for it.Next() {
}
it.Begin()
it.Next()
if index, value := it.Index(), it.Value(); index != 0 || value != 1 {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1)
}
}
func TestBinaryQueueIteratorEnd(t *testing.T) {
queue := NewWith(utils.IntComparator)
it := queue.Iterator()
if index := it.Index(); index != -1 {
t.Errorf("Got %v expected %v", index, -1)
}
it.End()
if index := it.Index(); index != 0 {
t.Errorf("Got %v expected %v", index, 0)
}
queue.Enqueue(3)
queue.Enqueue(2)
queue.Enqueue(1)
it.End()
if index := it.Index(); index != queue.Size() {
t.Errorf("Got %v expected %v", index, queue.Size())
}
it.Prev()
if index, value := it.Index(), it.Value(); index != queue.Size()-1 || value != 3 {
t.Errorf("Got %v,%v expected %v,%v", index, value, queue.Size()-1, 3)
}
}
func TestBinaryQueueIteratorFirst(t *testing.T) {
queue := NewWith(utils.IntComparator)
it := queue.Iterator()
if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
queue.Enqueue(3) // [3]
queue.Enqueue(2) // [2,3]
queue.Enqueue(1) // [1,3,2](2 swapped with 1, hence last)
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if index, value := it.Index(), it.Value(); index != 0 || value != 1 {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1)
}
}
func TestBinaryQueueIteratorLast(t *testing.T) {
tree := NewWith(utils.IntComparator)
it := tree.Iterator()
if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
tree.Enqueue(2)
tree.Enqueue(3)
tree.Enqueue(1)
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
if index, value := it.Index(), it.Value(); index != 2 || value != 3 {
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, 3)
}
}
func TestBinaryQueueIteratorNextTo(t *testing.T) {
// Sample seek function, i.e. string starting with "b"
seek := func(index int, value interface{}) bool {
return strings.HasSuffix(value.(string), "b")
}
// NextTo (empty)
{
tree := NewWith(utils.StringComparator)
it := tree.Iterator()
for it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// NextTo (not found)
{
tree := NewWith(utils.StringComparator)
tree.Enqueue("xx")
tree.Enqueue("yy")
it := tree.Iterator()
for it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// NextTo (found)
{
tree := NewWith(utils.StringComparator)
tree.Enqueue("aa")
tree.Enqueue("bb")
tree.Enqueue("cc")
it := tree.Iterator()
it.Begin()
if !it.NextTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
if index, value := it.Index(), it.Value(); index != 1 || value.(string) != "bb" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
}
if !it.Next() {
t.Errorf("Should go to first element")
}
if index, value := it.Index(), it.Value(); index != 2 || value.(string) != "cc" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
}
if it.Next() {
t.Errorf("Should not go past last element")
}
}
}
func TestBinaryQueueIteratorPrevTo(t *testing.T) {
// Sample seek function, i.e. string starting with "b"
seek := func(index int, value interface{}) bool {
return strings.HasSuffix(value.(string), "b")
}
// PrevTo (empty)
{
tree := NewWith(utils.StringComparator)
it := tree.Iterator()
it.End()
for it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// PrevTo (not found)
{
tree := NewWith(utils.StringComparator)
tree.Enqueue("xx")
tree.Enqueue("yy")
it := tree.Iterator()
it.End()
for it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
}
// PrevTo (found)
{
tree := NewWith(utils.StringComparator)
tree.Enqueue("aa")
tree.Enqueue("bb")
tree.Enqueue("cc")
it := tree.Iterator()
it.End()
if !it.PrevTo(seek) {
t.Errorf("Shouldn't iterate on empty list")
}
if index, value := it.Index(), it.Value(); index != 1 || value.(string) != "bb" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
}
if !it.Prev() {
t.Errorf("Should go to first element")
}
if index, value := it.Index(), it.Value(); index != 0 || value.(string) != "aa" {
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
}
if it.Prev() {
t.Errorf("Should not go before first element")
}
}
}
func TestBinaryQueueSerialization(t *testing.T) {
queue := NewWith(utils.StringComparator)
queue.Enqueue("c")
queue.Enqueue("b")
queue.Enqueue("a")
var err error
assert := func() {
if actualValue := queue.Values(); actualValue[0].(string) != "a" || actualValue[1].(string) != "b" || actualValue[2].(string) != "c" {
t.Errorf("Got %v expected %v", actualValue, "[1,3,2]")
}
if actualValue := queue.Size(); actualValue != 3 {
t.Errorf("Got %v expected %v", actualValue, 3)
}
if actualValue, ok := queue.Peek(); actualValue != "a" || !ok {
t.Errorf("Got %v expected %v", actualValue, "a")
}
if err != nil {
t.Errorf("Got error %v", err)
}
}
assert()
bytes, err := queue.ToJSON()
assert()
err = queue.FromJSON(bytes)
assert()
bytes, err = json.Marshal([]interface{}{"a", "b", "c", queue})
if err != nil {
t.Errorf("Got error %v", err)
}
err = json.Unmarshal([]byte(`[1,2,3]`), &queue)
if err != nil {
t.Errorf("Got error %v", err)
}
}
func TestBTreeString(t *testing.T) {
c := NewWith(byPriority)
c.Enqueue(1)
if !strings.HasPrefix(c.String(), "PriorityQueue") {
t.Errorf("String should start with container name")
}
}
func benchmarkEnqueue(b *testing.B, queue *Queue, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
}
}
func benchmarkDequeue(b *testing.B, queue *Queue, size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
queue.Dequeue()
}
}
}
func BenchmarkBinaryQueueDequeue100(b *testing.B) {
b.StopTimer()
size := 100
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkDequeue(b, queue, size)
}
func BenchmarkBinaryQueueDequeue1000(b *testing.B) {
b.StopTimer()
size := 1000
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkDequeue(b, queue, size)
}
func BenchmarkBinaryQueueDequeue10000(b *testing.B) {
b.StopTimer()
size := 10000
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkDequeue(b, queue, size)
}
func BenchmarkBinaryQueueDequeue100000(b *testing.B) {
b.StopTimer()
size := 100000
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkDequeue(b, queue, size)
}
func BenchmarkBinaryQueueEnqueue100(b *testing.B) {
b.StopTimer()
size := 100
queue := NewWith(byPriority)
b.StartTimer()
benchmarkEnqueue(b, queue, size)
}
func BenchmarkBinaryQueueEnqueue1000(b *testing.B) {
b.StopTimer()
size := 1000
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkEnqueue(b, queue, size)
}
func BenchmarkBinaryQueueEnqueue10000(b *testing.B) {
b.StopTimer()
size := 10000
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkEnqueue(b, queue, size)
}
func BenchmarkBinaryQueueEnqueue100000(b *testing.B) {
b.StopTimer()
size := 100000
queue := NewWith(byPriority)
for n := 0; n < size; n++ {
queue.Enqueue(n)
}
b.StartTimer()
benchmarkEnqueue(b, queue, size)
}

@ -0,0 +1,33 @@
// Copyright (c) 2015, Emir Pasic. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package priorityqueue
import (
"github.com/emirpasic/gods/containers"
)
// Assert Serialization implementation
var _ containers.JSONSerializer = (*Queue)(nil)
var _ containers.JSONDeserializer = (*Queue)(nil)
// ToJSON outputs the JSON representation of the queue.
func (queue *Queue) ToJSON() ([]byte, error) {
return queue.heap.ToJSON()
}
// FromJSON populates the queue from the input JSON representation.
func (queue *Queue) FromJSON(data []byte) error {
return queue.heap.FromJSON(data)
}
// UnmarshalJSON @implements json.Unmarshaler
func (queue *Queue) UnmarshalJSON(bytes []byte) error {
return queue.FromJSON(bytes)
}
// MarshalJSON @implements json.Marshaler
func (queue *Queue) MarshalJSON() ([]byte, error) {
return queue.ToJSON()
}
Loading…
Cancel
Save