algorithm

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 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 CountMinSketch

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

CountMinSketch implements the Count-Min Sketch algorithm for frequency estimation.

func NewCountMinSketch

func NewCountMinSketch(epsilon float64, delta float64) *CountMinSketch

NewCountMinSketch creates a new Count-Min Sketch with the given error rate and confidence.

func (*CountMinSketch) Add

func (cms *CountMinSketch) Add(key []byte, count uint64)

Add adds a value to the sketch.

func (*CountMinSketch) Decay

func (cms *CountMinSketch) Decay(factor float64)

Decay applies exponential decay to all counts

func (*CountMinSketch) Estimate

func (cms *CountMinSketch) Estimate(key []byte) uint64

Estimate estimates the frequency of a value.

func (*CountMinSketch) Reset

func (cms *CountMinSketch) Reset()

Reset resets the sketch.

type Item

type Item struct {
	Key   string
	Count uint64
	Error uint64
	Index int // Index in the heap
}

Item represents an item in the Space-Saving algorithm.

type SpaceSaving

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

SpaceSaving implements the Space-Saving algorithm for finding top-k frequent items.

func NewSpaceSaving

func NewSpaceSaving(capacity int) *SpaceSaving

NewSpaceSaving creates a new Space-Saving instance with the given capacity.

func (*SpaceSaving) Add

func (ss *SpaceSaving) Add(key string, count uint64)

Add adds an item to the Space-Saving structure.

func (*SpaceSaving) Clear

func (ss *SpaceSaving) Clear()

Clear removes all items from the Space-Saving structure

func (*SpaceSaving) Count

func (ss *SpaceSaving) Count(key string) uint64

Count returns the count for a specific key

func (*SpaceSaving) Decay

func (ss *SpaceSaving) Decay(factor float64)

Decay applies exponential decay to all counts

func (*SpaceSaving) TopK

func (ss *SpaceSaving) TopK(k int) []Item

TopK returns the top k items.

type SpaceSavingHeap

type SpaceSavingHeap []*Item

SpaceSavingHeap is a min-heap of Items.

func (SpaceSavingHeap) Len

func (h SpaceSavingHeap) Len() int

Len returns the length of the heap.

func (SpaceSavingHeap) Less

func (h SpaceSavingHeap) Less(i, j int) bool

Less returns whether the item at index i is less than the item at index j.

func (*SpaceSavingHeap) Pop

func (h *SpaceSavingHeap) Pop() any

Pop removes and returns the minimum item from the heap.

func (*SpaceSavingHeap) Push

func (h *SpaceSavingHeap) Push(x any)

Push adds an item to the heap.

func (SpaceSavingHeap) Swap

func (h SpaceSavingHeap) Swap(i, j int)

Swap swaps the items at indices i and j.

Jump to

Keyboard shortcuts

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