So, I wanted to mess around with some algorithms and needed a priority 
queue.
I've written one from scratch before but I wanted to try using the 
container/heap package.
I found it pretty confusing TBH.

After trying to recreate my own version of the example (
https://golang.org/pkg/container/heap/#example__priorityQueue),
I couldn't help but feel like it was almost as much effort to satisfy the 
interface as it was to code a heap from scratch.
For example, the inclusion of an Index field in the Item structure feels 
particularly awkward.

So I took a look at https://github.com/emirpasic/gods/ , and tried making a 
custom type for use in a priority queue.
To me, it seemed a lot simpler and a lot easier to interact with.


I'm curious to hear from others about their experiences. =)


Here's my priority queue usage example:


~~~

package main

import (
"fmt"
"github.com/emirpasic/gods/trees/binaryheap"
)

type item struct {
data     interface{}
priority int
}

func compareItems(a, b interface{}) int {
return a.(item).priority - b.(item).priority
}

var heap = binaryheap.NewWith(compareItems)

func main() {
examples := []item{
{8, 3},
{"first", 1},
{"last", 4},
{3.14, 2},
}

for _, v := range examples {
fmt.Println(">> push", v)
heap.Push(v)
}

for heap.Size() > 0 {
fmt.Println(">> extract")
x, ok := heap.Pop()
if ok {
fmt.Println(x)
}
}
}

~~~

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to