simple

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package simple implements an unweighted reservoir sampling algorithm.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Simple

type Simple[T any] struct {
	// contains filtered or unexported fields
}

Simple implements unweighted reservoir sampling using Algorithm R from "Random sampling with a reservoir" by Jeffrey Vitter (1985) https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_R

func New

func New[T any](capacity int, rnd *rand.Rand) *Simple[T]

New returns a simple reservoir sampler with given capacity (i.e., reservoir size) and random number generator.

func (*Simple[T]) Add

func (s *Simple[T]) Add(item T)

Add considers a new observation for the sample. Items have unit weight.

func (*Simple[T]) Count

func (s *Simple[T]) Count() int

Count returns the number of items that were observed.

func (*Simple[T]) Get

func (s *Simple[T]) Get(i int) T

Get returns the i'th selected item from the sample.

func (*Simple[T]) Init added in v1.4.0

func (s *Simple[T]) Init(capacity int, rnd *rand.Rand)

func (*Simple[T]) Size

func (s *Simple[T]) Size() int

Size returns the number of items in the sample. If the reservoir is full, Size() equals Capacity().

Jump to

Keyboard shortcuts

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