queue

package module
v0.0.0-...-1e2eb04 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: MIT Imports: 5 Imported by: 0

README

queue

队列

优先级队列

    pq := NewPriorityQueue(context.Background(), 1000)
    // Push
    pq.Push(&Element{V: "a", P: 3})
    pq.Push(&Element{V: "b", P: 2})
    pq.Push(&Element{V: "c", P: 1})
    // Pop
    for {
        if elem, ok := pq.Pop(); !ok { // the pop is stop
            log.Printf("pop:stop\n")
            return
        } else {
            log.Printf("pop:%v--%d\n", elem.V, elem.P)
        }
    }
    // Output: `pop:a--3`
    // Output: `pop:b--2`
    // Output: `pop:c--1`

延迟队列

    dq := NewDelayQueue(context.Background(), 1000, func() time.Time {
        return time.Now()
    })
    // Push
    dq.Push(&Delay{V: "a", D: time.Millisecond * 1})
    dq.Push(&Delay{V: "b", D: time.Millisecond * 2})
    dq.Push(&Delay{V: "c", D: time.Millisecond * 3})
    // Pop
    for {
        if delay, ok := dq.Pop(); !ok { // the pop is stop
            log.Printf("pop:stop\n")
            return
    	} else {
    	    log.Printf("pop:%v-%s-[%s]\n", delay.V, delay.D, delay.Deadline().Sub(time.Now()))
    	}
    }
    // Output: `pop:a-1ms-[-4.399µs]`
    // Output: `pop:b-2ms-[-163.84µs]`
    // Output: `pop:c-3ms-[-225.608µs]`

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHeapDelays

func NewHeapDelays(cap int) heap.Interface

func NewHeapElements

func NewHeapElements(cap int) heap.Interface

Types

type Delay

type Delay struct {
	V interface{}   // value
	D time.Duration // delay: min time.Millisecond is better
	// contains filtered or unexported fields
}

impl the std lib heap interface

func (*Delay) Deadline

func (d *Delay) Deadline() time.Time

func (*Delay) Index

func (d *Delay) Index() int

type DelayQueue

type DelayQueue struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewDelayQueue

func NewDelayQueue(ctx context.Context, cap int, now func() time.Time) *DelayQueue

func (*DelayQueue) Fix

func (dq *DelayQueue) Fix(delay *Delay)

func (*DelayQueue) Len

func (dq *DelayQueue) Len() int

func (*DelayQueue) Peek

func (dq *DelayQueue) Peek(idx int) *Delay

func (*DelayQueue) Pop

func (dq *DelayQueue) Pop() (*Delay, bool)

func (*DelayQueue) Push

func (dq *DelayQueue) Push(delay *Delay)

func (*DelayQueue) Queue

func (dq *DelayQueue) Queue() <-chan *Delay

func (*DelayQueue) Remove

func (dq *DelayQueue) Remove(delay *Delay) *Delay

type Element

type Element struct {
	V interface{} // value
	P int         // priority: max is first pop
	// contains filtered or unexported fields
}

func (*Element) Index

func (e *Element) Index() int

type HeapDelays

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

func (*HeapDelays) Len

func (hd *HeapDelays) Len() int

func (*HeapDelays) Less

func (hd *HeapDelays) Less(i, j int) bool

func (*HeapDelays) Pop

func (hd *HeapDelays) Pop() interface{}

func (*HeapDelays) Push

func (hd *HeapDelays) Push(x interface{})

func (*HeapDelays) Swap

func (hd *HeapDelays) Swap(i, j int)

type HeapElements

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

impl the std lib heap interface

func (*HeapElements) Len

func (he *HeapElements) Len() int

func (*HeapElements) Less

func (he *HeapElements) Less(i, j int) bool

func (*HeapElements) Pop

func (he *HeapElements) Pop() interface{}

func (*HeapElements) Push

func (he *HeapElements) Push(x interface{})

func (*HeapElements) Swap

func (he *HeapElements) Swap(i, j int)

type PriorityQueue

type PriorityQueue struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewPriorityQueue

func NewPriorityQueue(ctx context.Context, cap int) *PriorityQueue

func (*PriorityQueue) Fix

func (pq *PriorityQueue) Fix(elem *Element)

func (*PriorityQueue) Len

func (pq *PriorityQueue) Len() int

func (*PriorityQueue) Peek

func (pq *PriorityQueue) Peek(idx int) *Element

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() (*Element, bool)

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(elem *Element)

func (*PriorityQueue) Queue

func (pq *PriorityQueue) Queue() <-chan *Element

func (*PriorityQueue) Remove

func (pq *PriorityQueue) Remove(elem *Element) *Element

Jump to

Keyboard shortcuts

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