Data-Reservoir-Shared

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

        my $kept  = $rsv->add($item, $weight);    # weighted reservoir: weight must be > 0
        my $n     = $rsv->add_many(\@items);              # uniform: array ref of items
        my $n     = $rsv->add_many([[$item,$w], ...]);     # weighted: [item, weight] pairs
        my @items = $rsv->sample;            # current sample (a list, up to k items)
        my $it    = $rsv->get($i);           # the i-th retained item (0-based), or undef
        $rsv->clear;                         # empty the reservoir (seen resets to 0)

    "add" feeds one item and returns 1 if it is now stored in the reservoir or
    0 if it was discarded. On a weighted reservoir "add" takes a second
    argument, the item's weight (a finite number greater than zero -- a
    missing, zero, negative, infinite, or NaN weight croaks). "add_many" feeds
    an array reference under a single write lock, returning how many of the
    batch were stored (an item that replaces an earlier sample counts as
    stored; use "count" for the number currently retained); for a weighted
    reservoir each element must be an "[$item, $weight]" pair. "sample"
    returns the retained items as a list (order is not meaningful); "get"
    returns a single retained item by index. "clear" empties the reservoir and
    resets the seen counter.

  Introspection and RNG
        $rsv->count;        # number of items currently held: min(seen, k)

lib/Data/Reservoir/Shared.pm  view on Meta::CPAN

    my $kept  = $rsv->add($item, $weight);    # weighted reservoir: weight must be > 0
    my $n     = $rsv->add_many(\@items);              # uniform: array ref of items
    my $n     = $rsv->add_many([[$item,$w], ...]);     # weighted: [item, weight] pairs
    my @items = $rsv->sample;            # current sample (a list, up to k items)
    my $it    = $rsv->get($i);           # the i-th retained item (0-based), or undef
    $rsv->clear;                         # empty the reservoir (seen resets to 0)

C<add> feeds one item and returns B<1 if it is now stored> in the reservoir or
B<0 if it was discarded>. On a B<weighted> reservoir C<add> takes a second
argument, the item's weight (a finite number greater than zero -- a missing,
zero, negative, infinite, or NaN weight croaks). C<add_many> feeds an array
reference under a single write lock, returning how many of the batch were
stored (an item that replaces an earlier sample counts as stored; use C<count>
for the number currently retained); for a weighted reservoir each element must
be an C<< [$item, $weight] >> pair. C<sample> returns the retained items as a
list (order is not meaningful); C<get> returns a single retained item by
index. C<clear> empties the reservoir and resets the seen counter.

=head2 Introspection and RNG

    $rsv->count;        # number of items currently held: min(seen, k)

t/02-weighted.t  view on Meta::CPAN

    cmp_ok $p, '<=', 0.78, "add_many k=1 scale $scale: P(B|w=3) <= 0.78 (got $p)";
}

# error handling (all croak BEFORE taking the lock)
{
    my $r = Data::Reservoir::Shared->new_weighted(undef, 3, 16);
    eval { $r->add("x") };            like $@, qr/requires a weight/, 'weighted add without weight croaks';
    eval { $r->add("x", 0) };         like $@, qr/> 0/,               'zero weight croaks';
    eval { $r->add("x", -1) };        like $@, qr/> 0/,               'negative weight croaks';
    my $inf = "Inf" + 0;              # NOT 9e999 (finite on -Duselongdouble)
    my $nan = "NaN" + 0;
    eval { $r->add("x", $inf) };      like $@, qr/finite/,            'infinite weight croaks';
    eval { $r->add("x", $nan) };      like $@, qr/> 0|finite/,        'NaN weight croaks';
    eval { $r->add_many(["notapair"]) }; like $@, qr/pair/,           'add_many rejects non-pair elements';
    is $r->seen, 0, 'no failed add mutated the reservoir';
}

# persistence: a weighted reservoir reopened from its file stays weighted
{
    my $path = File::Temp->new(SUFFIX => '.rsv', UNLINK => 0)->filename;
    unlink $path;
    {
        my $w = Data::Reservoir::Shared->new_weighted($path, 6, 16);



( run in 0.836 second using v1.01-cache-2.11-cpan-60884a8df76 )