Sha256: 627b36012ca72d20caba288e71eceb625aeb171cbc0ea4746e7386b691b47f68

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2019
# License: GNU APGLv3 (or later) <http://www.gnu.org/copyleft/gpl.html>

require "forwardable"

class Reservoir
  include Enumerable
  extend Forwardable

  attr_accessor :sample_size, :total

  def initialize sample_size
    @sample_size	= sample_size
    @total      	= 0
    @reservoir  	= []
  end

  def <<(obj)
    if @total < @sample_size
      # fill empty slot in the reservoir
      @reservoir << obj
    else
      # randomly replace elements in the reservoir with a decreasing probability
      r = rand(0..@total)
      @reservoir[r] = obj if r < @sample_size
    end

    @total += 1
  end

  def add list	; list.each{|x| self << x}       	; end
  def clear   	; @reservoir.clear; @total = 0   	; end
  def full?   	; @reservoir.size >= @sample_size	; end

  def_delegators :@reservoir, :each, :size, :empty?, :uniq
end

class HardReservoir < Reservoir
  # assumes examples are pre-randomized, but ensures that all elements are unique

  def initialize sample_size
    @sample_size = sample_size
    @total       = 0
    @reservoir   = Set.new
  end

  def <<(obj)
    # *only* fill empty slot in the reservoir
    @reservoir << obj if @reservoir.size < @sample_size

    @total += 1
  end

  def uniq *args, &block	; @reservoir.to_a.uniq(*args, &block)	; end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
muflax-0.7.0 lib/muflax/reservoir.rb
muflax-0.6.1 lib/muflax/reservoir.rb
muflax-0.6.0 lib/muflax/reservoir.rb