Reservoir<T> structv0.1.0

Source
A reservoir sample being built.
pub struct Reservoir<T> {
    // private fields
}

Offer every item of the stream in turn and the reservoir ends holding a uniformly chosen subset of whatever arrived, each subset of that size equally likely. The caller owns the storage, and its length is the sample size.

Type parameters

NameDescription
Tthe item type

Constructors

Reservoir

pub func Reservoir(room: var T[..]) -> Reservoir<T>

A reservoir that will fill room.

NameTypeDescription
roomvar T[..]how many items the reservoir keeps

Returns: an empty reservoir that will keep room items

Source

Methods

Length

pub func Length(self: &Reservoir<T>) -> uint

How many items the reservoir currently holds, which is the smaller of the sample size and the number offered.

Returns: how many items it currently holds

Source

Seen

pub func Seen(self: &Reservoir<T>) -> uint

How many items have been offered.

Returns: how many items have been offered to it in total

Source

Offer<G, T>

pub func Offer<G: RandomGenerator, T>(
    generator: &var G,
    reservoir: &var Reservoir<T>,
    item: T
)

Offers item to reservoir. Algorithm R: while the reservoir has room the item is kept outright, and afterwards the item replaces a uniformly chosen existing entry with probability equal to the sample size over the number seen. That is what keeps every subset equally likely at every moment, not merely at the end — so a stream can be cut short and the sample is still uniform over what arrived.

Source

Type parameters

NameDescription
G: RandomGeneratorthe generator's type
Tthe item type

Parameters

NameTypeDescription
generator&var Gwhere the bits come from
reservoir&var Reservoir<T>the reservoir to offer the item to
itemTthe item being offered