github.com/sunvim/utils@v0.1.0/examples/pq/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	pq "github.com/sunvim/utils/priorityqueue"
     7  )
     8  
     9  func main() {
    10  	q := pq.NewPriorityQueue(5, false)
    11  
    12  	q.Put(&Node{Number: 5, Name: "hello"})
    13  	q.Put(&Node{Number: 3, Name: "world"})
    14  	q.Put(&Node{Number: 4, Name: "sky"})
    15  	q.Put(&Node{Number: 1, Name: "mobus"})
    16  	q.Put(&Node{Number: 2, Name: "sunqc"})
    17  
    18  	for i := 0; i < 5; i++ {
    19  		v, _ := q.Get(i)
    20  		fmt.Printf("item: %+v \n", v)
    21  	}
    22  
    23  }
    24  
    25  type Node struct {
    26  	Number uint64
    27  	Name   string
    28  }
    29  
    30  func (n *Node) Compare(other pq.Item) int {
    31  	o := other.(*Node)
    32  	if o.Number > n.Number {
    33  		return 1
    34  	} else if o.Number == n.Number {
    35  		return 0
    36  	}
    37  	return -1
    38  }