algorithm

package
v4.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package algorithm contains some useful algorithms

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLargestNItems

func GetLargestNItems[T common.Sortable](inputChan <-chan T, topN int) ([]T, error)

GetLargestNItems get N highest priority items

func GetSmallestNItems

func GetSmallestNItems[T common.Sortable](inputChan <-chan T, topN int) ([]T, error)

GetSmallestNItems get N smallest priority items

func GetTopKItems

func GetTopKItems[T common.Sortable](
	inputChan <-chan T,
	topN int,
	sortOrder common.SortOrder,
) (result []T, err error)

GetTopKItems calculate topN by heap

Types

type Deque

type Deque[T any] interface {
	PushBack(T)
	PushFront(T)
	PopFront() T
	PopBack() T
	Len() int
	Front() T
	Back() T
}

Deque

https://pkg.go.dev/github.com/gammazero/deque#Deque

func NewDeque

func NewDeque[T any](optfs ...DequeOptFunc) (Deque[T], error)

NewDeque new deque

type DequeOptFunc

type DequeOptFunc func(*dequeOpt) error

DequeOptFunc optional arguments for deque

func WithDequeCurrentCapacity

func WithDequeCurrentCapacity(size int) DequeOptFunc

WithDequeCurrentCapacity preallocate memory for deque

func WithDequeMinimalCapacity

func WithDequeMinimalCapacity(size int) DequeOptFunc

WithDequeMinimalCapacity set deque minimal capacity

type FIFO

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

FIFO is a lock-free First-In-First-Out queue

paper: https://1drv.ms/b/s!Au45o0W1gVVLuNxYkPzfBo4fOssFPQ?e=TYxHKl

Example
f := NewFIFO()
f.Put(1)
v := f.Get()
if v == nil {
	panic(v)
}

fmt.Println(v.(int))
Output:

1

func NewFIFO

func NewFIFO() *FIFO

NewFIFO create a new FIFO queue

func (*FIFO) Get

func (f *FIFO) Get() any

Get pop data from the head of queue

func (*FIFO) Len

func (f *FIFO) Len() int

Len return the length of queue

func (*FIFO) Put

func (f *FIFO) Put(d any)

Put put an data into queue's tail

type PriorityItem added in v4.7.1

type PriorityItem[T common.Sortable] struct {
	// Val 	T
	Val T
	// Name whatever to identify this item
	Name any
}

PriorityItem priority item

func (PriorityItem[T]) GetVal added in v4.7.1

func (t PriorityItem[T]) GetVal() T

GetVal get value of priority item

type PriorityQ added in v4.7.1

type PriorityQ[T common.Sortable] struct {
	// contains filtered or unexported fields
}

PriorityQ priority queue

Do not use this structure directly, use `NewPriorityQ` instead.

func NewPriorityQ added in v4.7.1

func NewPriorityQ[T common.Sortable](order common.SortOrder) *PriorityQ[T]

NewPriorityQ create new PriorityQ

Args:

  • order: common.SortOrderAsc or common.SortOrderDesc, if you want to get topN items, use common.SortOrderDesc, if you want to get bottomN items, use common.SortOrderAsc.

func (*PriorityQ[T]) Len added in v4.7.1

func (pq *PriorityQ[T]) Len() int

Len return length of priority queue

func (*PriorityQ[T]) Peek added in v4.7.1

func (pq *PriorityQ[T]) Peek() PriotiryItemItf[T]

Peek peek item from priority queue

func (*PriorityQ[T]) Pop added in v4.7.1

func (pq *PriorityQ[T]) Pop() PriotiryItemItf[T]

Pop pop item from priority queue

func (*PriorityQ[T]) Push added in v4.7.1

func (pq *PriorityQ[T]) Push(v PriotiryItemItf[T])

Push push item into priority queue

type PriotiryItemItf added in v4.7.1

type PriotiryItemItf[T common.Sortable] interface {
	GetVal() T
}

PriotiryItemItf priority item interface

type SkipList added in v4.9.0

type SkipList[T skiplist.Sortable] struct {
	*skiplist.SkipList[T]
}

SkipList skiplist

func NewSkiplist

func NewSkiplist[T skiplist.Sortable]() SkipList[T]

NewSkiplist new skiplist

https://github.com/sean-public/fast-skiplist

Jump to

Keyboard shortcuts

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