prior

package module
v0.0.0-...-a0417c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2018 License: MIT Imports: 2 Imported by: 1

README

About prior

prior is a priority queue based on golang container/heap.

Explation

Node: node is the unit insert to queue. Node has attributes:
Key: key associated with node, can be nil
value: value of key, can be nil
Priority:priority of node
Index: index in queue

API

Push(Node) : push a node into queue, O(logN) where N is queue length
Pop(Node) : fetch node with max priority, O(1)
Remove(index): remove a specified node of index, O(logN) where N is queue length
Length() : queue length

Example

package main

import (
    "fmt"
    "time"
    "github.com/insionng/prior"
)

func main() {
    pq := prior.NewPriorityQueue()
    //写入队列
    type Meta struct {
        Timestamp int64
        Symbol    string
        Price     float64
        Quantity  float64
        Source    string
    }
    var meta Meta
    for i := 0; i < 100; i++ {
        meta.Timestamp = time.Now().Unix()
        meta.Symbol = "glod:usd"
        meta.Price = 1000.8 + float64(i)
        meta.Quantity = 0.8 + float64(i)
        if i%2==0 {
            meta.Source = "en"
        } else {
            meta.Source = "zh"
        }

        //pq.Push(prior.NewNode(nil, meta, meta.Price*-1))
        pq.AddNode(nil, meta, meta.Price*-1)
    }

    //读取队列
    for pq.Length() > 0 {
        v := pq.Pop()
        if v == nil {
            break
        }
        if value, okay := v.GetValue().(Meta); okay {
            fmt.Println(value)
        }
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddNode

func AddNode(pq *PriorityQueue, key, value interface{}, priority float64)

func RemoveNode

func RemoveNode(pq *PriorityQueue, values ...interface{})

Types

type Node

type Node struct {
	Key      interface{}
	Value    interface{}
	Priority float64
	Index    int
	// contains filtered or unexported fields
}

func NewNode

func NewNode(key, value interface{}, priority float64) *Node

func (*Node) GetIndex

func (n *Node) GetIndex() int

func (*Node) GetKey

func (n *Node) GetKey() interface{}

func (*Node) GetValue

func (n *Node) GetValue() interface{}

func (*Node) UpdatePriority

func (n *Node) UpdatePriority(newPrio float64)

type Nodes

type Nodes []*Node

func (Nodes) Len

func (nodes Nodes) Len() int

func (Nodes) Less

func (nodes Nodes) Less(i, j int) bool

func (*Nodes) Pop

func (nodes *Nodes) Pop() interface{}

func (*Nodes) Pull

func (nodes *Nodes) Pull(values ...interface{}) *Node

Pull 拉取匹配的第一个Node返回

func (*Nodes) Push

func (nodes *Nodes) Push(node interface{})

func (Nodes) Swap

func (nodes Nodes) Swap(i, j int)

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

func NewPriorityQueue

func NewPriorityQueue() *PriorityQueue

func (*PriorityQueue) AddNode

func (pq *PriorityQueue) AddNode(key, value interface{}, priority float64)

func (*PriorityQueue) Length

func (pq *PriorityQueue) Length() int

Length 优先级队列长度

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() *Node

func (*PriorityQueue) Pull

func (pq *PriorityQueue) Pull(values ...interface{}) *Node

Pull 拉取匹配的第一个Node返回

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(n *Node)

func (*PriorityQueue) Remove

func (pq *PriorityQueue) Remove(index int)

func (*PriorityQueue) RemoveNode

func (pq *PriorityQueue) RemoveNode(values ...interface{})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL