Help us shape the future of Go! Take the 2026 Go Developer Survey

sampling

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInsufficientElements = errors.New("insufficient elements")
	ErrTooManyElements      = errors.New("too many elements requested")
)

Functions

This section is empty.

Types

type BestSampler

type BestSampler interface {
	// Sample returns the K elements with the best scores.
	Sample() []ids.NodeID

	// SetScore sets the score of the given element.
	SetScore(nodeID ids.NodeID, score uint64)

	// Clear removes all elements.
	Clear()
}

BestSampler samples the K elements with the best scores.

func NewBest

func NewBest(k int) BestSampler

NewBest returns a new sampler that samples the K nodes with the best scores.

type Parameters

type Parameters struct {
	// K is the number of nodes to poll
	K int `json:"k" yaml:"k"`

	// AlphaPreference is the vote threshold to change preference
	AlphaPreference int `json:"alphaPreference" yaml:"alphaPreference"`

	// AlphaConfidence is the vote threshold for confidence
	AlphaConfidence int `json:"alphaConfidence" yaml:"alphaConfidence"`

	// Beta is the number of consecutive successful polls required for finalization
	Beta int `json:"beta" yaml:"beta"`

	// ConcurrentRepolls is the number of concurrent polls
	ConcurrentRepolls int `json:"concurrentRepolls" yaml:"concurrentRepolls"`

	// OptimalProcessing is the number of items to process optimally
	OptimalProcessing int `json:"optimalProcessing" yaml:"optimalProcessing"`

	// MaxOutstandingItems is the maximum number of outstanding items
	MaxOutstandingItems int `json:"maxOutstandingItems" yaml:"maxOutstandingItems"`

	// MaxItemProcessingTime is the maximum time to process an item
	MaxItemProcessingTime time.Duration `json:"maxItemProcessingTime" yaml:"maxItemProcessingTime"`
}

Parameters defines the consensus parameters for binary voting

func (Parameters) Valid

func (p Parameters) Valid() error

Valid returns nil if the parameters are valid

type Sampler

type Sampler interface {
	// Sample returns a sample of the given size from the set.
	// If there are less than [size] elements in the set, an error is returned.
	Sample(size int) ([]ids.NodeID, error)

	// CanSample returns whether a sample of the given size can be taken from the set.
	CanSample(size int) bool

	// Clear removes all elements from the sampler.
	Clear()

	// Add adds an element to the set.
	Add(ids.NodeID)

	// Len returns the number of elements in the set.
	Len() int

	// Weighted returns whether the sampler samples by weight.
	Weighted() bool
}

Sampler samples elements from a set.

func NewDeterministicUniform

func NewDeterministicUniform(seed int64) Sampler

NewDeterministicUniform returns a new sampler that samples uniformly with a deterministic seed.

func NewUniform

func NewUniform() Sampler

NewUniform returns a new sampler that samples uniformly from the set.

type Type

type Type byte

Type enumerates the different samplers that can be used.

const (
	// UniformSamplerType samples uniformly from the set.
	UniformSamplerType Type = iota
	// WeightedSamplerType samples from the set with weights.
	WeightedSamplerType
	// BestSamplerType samples the K elements with the best scores.
	BestSamplerType

	// MaxSamplerType is the largest valid sampler type.
	MaxSamplerType = math.MaxUint8
)

func (Type) String

func (t Type) String() string

type Uniform

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

Uniform samples uniformly from a set.

func (*Uniform) Add

func (u *Uniform) Add(nodeID ids.NodeID)

Add adds an element to the set.

func (*Uniform) CanSample

func (u *Uniform) CanSample(size int) bool

CanSample returns whether a sample of the given size can be taken from the set.

func (*Uniform) Clear

func (u *Uniform) Clear()

Clear removes all elements from the sampler.

func (*Uniform) Len

func (u *Uniform) Len() int

Len returns the number of elements in the set.

func (*Uniform) Sample

func (u *Uniform) Sample(size int) ([]ids.NodeID, error)

Sample returns a sample of the given size from the set.

func (*Uniform) Weighted

func (u *Uniform) Weighted() bool

Weighted returns false for uniform sampling.

type Weighted

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

Weighted samples from a set with weights.

func (*Weighted) Add

func (w *Weighted) Add(nodeID ids.NodeID)

Add adds an element with weight 1 to the set.

func (*Weighted) AddWeighted

func (w *Weighted) AddWeighted(nodeID ids.NodeID, weight uint64)

AddWeighted adds an element with the given weight to the set.

func (*Weighted) CanSample

func (w *Weighted) CanSample(size int) bool

CanSample returns whether a sample of the given size can be taken from the set.

func (*Weighted) Clear

func (w *Weighted) Clear()

Clear removes all elements from the sampler.

func (*Weighted) Len

func (w *Weighted) Len() int

Len returns the number of elements in the set.

func (*Weighted) Sample

func (w *Weighted) Sample(size int) ([]ids.NodeID, error)

Sample returns a sample of the given size from the set.

func (*Weighted) Weighted

func (w *Weighted) Weighted() bool

Weighted returns true for weighted sampling.

type WeightedSampler

type WeightedSampler interface {
	Sampler

	// AddWeighted adds an element with the given weight to the set.
	AddWeighted(nodeID ids.NodeID, weight uint64)
}

WeightedSampler samples elements from a set with weights.

func NewWeighted

func NewWeighted() WeightedSampler

NewWeighted returns a new sampler that samples by weight.

Jump to

Keyboard shortcuts

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