heap

package
v0.0.75 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Heap

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

Heap is a thread-safe producer/consumer queue that implements a heap data structure. It can be used to implement priority queues and similar data structures.

func NewHeap

func NewHeap(keyFn KeyFunc, lessFn LessFunc) *Heap

NewHeap returns a Heap which can be used to queue up items to process.

func (*Heap) Add

func (h *Heap) Add(obj interface{}) error

Add inserts an item, and puts it in the queue. The item is updated if it already exists.

func (*Heap) AddIfNotPresent

func (h *Heap) AddIfNotPresent(obj interface{}) error

AddIfNotPresent inserts an item, and puts it in the queue. If an item with the key is present in the map, no changes is made to the item.

This is useful in a single producer/consumer scenario so that the consumer can safely retry items without contending with the producer and potentially enqueueing stale items.

func (*Heap) BulkAdd

func (h *Heap) BulkAdd(list []interface{}) error

BulkAdd adds all the items in the list to the queue and then signals the condition variable. It is useful when the caller would like to add all of the items to the queue before consumer starts processing them.

func (*Heap) Close

func (h *Heap) Close()

Close the Heap and signals condition variables that may be waiting to pop items from the heap.

func (*Heap) Delete

func (h *Heap) Delete(obj interface{}) error

Delete removes an item.

func (*Heap) Get

func (h *Heap) Get(obj interface{}) (interface{}, bool)

Get returns the requested item, or sets exists=false. return item.obj, exists

func (*Heap) GetByKey

func (h *Heap) GetByKey(key string) (interface{}, bool)

GetByKey returns the requested item, or sets exists=false.

func (*Heap) IsClosed

func (h *Heap) IsClosed() bool

IsClosed returns true if the queue is closed.

func (*Heap) List

func (h *Heap) List() []interface{}

List returns a list of all the items.

func (*Heap) ListKeys

func (h *Heap) ListKeys() []string

ListKeys returns a list of all the keys of the objects currently in the Heap.

func (*Heap) Pop

func (h *Heap) Pop() (interface{}, error)

Pop waits until an item is ready. If multiple items are ready, they are returned in the order given by Heap.data.lessFunc.

func (*Heap) Update

func (h *Heap) Update(obj interface{}) error

Update is the same as Add in this implementation. When the item does not exist, it is added.

type KeyFunc

type KeyFunc func(interface{}) string

KeyFunc knows how to make a key from an object. Implementations should be deterministic.

type LessFunc

type LessFunc func(interface{}, interface{}) bool

LessFunc is used to compare two objects in the heap.

Jump to

Keyboard shortcuts

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