algorithm

package
v6.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 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 BinarySearch

func BinarySearch[T any](s []T, cmp func(index int, element T) int) int

BinarySearch searches for target in a sorted slice s.

cmp must implement the same ordering as the slice, i.e. it must return a negative value if the target is less than the element at index, a positive value if the target is greater than the element at index, and zero if the target is equal to the element at index.

Returns the index of target in s, or -1 if target is not present.

I thinks this function is better than built-in slices.BinarySearchFunc, since of the cmp function is more flexible, you can get the index and element at each iteration, that can enpower you to do more things rather than just comparing. BTW, I think there is no need for the target as the parameter, since you can wrap the target in the cmp function.

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 DiffArray added in v6.1.0

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

DiffArray represents a difference array for efficient range updates.

func NewDiffArray added in v6.1.0

func NewDiffArray(arr []int) *DiffArray

NewDiffArray initializes a DiffArray based on the input array.

func (*DiffArray) IncrementRange added in v6.1.0

func (d *DiffArray) IncrementRange(l, r, val int)

IncrementRange increments all elements in the range [l, r] by val.

func (*DiffArray) ToArray added in v6.1.0

func (d *DiffArray) ToArray() []int

ToArray reconstructs and returns the updated array after all range increments.

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("empty")
}

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

type PriorityItem[T common.Sortable] struct {
	Val  T   // Value used for ordering
	Name any // Optional identifier
}

PriorityItem is a concrete implementation of PriorityItemItf.

func (PriorityItem[T]) GetVal

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

GetVal returns the value of the priority item.

type PriorityItemItf added in v6.1.0

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

PriorityItemItf defines the interface for items stored in the priority queue.

type PriorityQ

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

PriorityQ is a generic priority queue. Do not use this structure directly, use `NewPriorityQ` instead.

func NewPriorityQ

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

NewPriorityQ creates a new PriorityQ.

Args:

  • order: common.SortOrderAsc or common.SortOrderDesc. Use common.SortOrderDesc for top-N items, Use common.SortOrderAsc for bottom-N items.

func (*PriorityQ[T]) Len

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

Len returns the number of items in the queue.

func (*PriorityQ[T]) Peek

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

Peek returns the highest-priority item without removing it.

func (*PriorityQ[T]) Pop

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

Pop removes and returns the highest-priority item.

func (*PriorityQ[T]) Push

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

Push inserts an item into the priority queue.

type SkipList

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