class Reservoir
Attributes
sample_size[RW]
total[RW]
Public Class Methods
new(sample_size)
click to toggle source
# File lib/muflax/reservoir.rb, line 14 def initialize sample_size @sample_size = sample_size @total = 0 @reservoir = [] end
Public Instance Methods
<<(obj)
click to toggle source
# File lib/muflax/reservoir.rb, line 20 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
add(list ;)
click to toggle source
# File lib/muflax/reservoir.rb, line 33 def add list ; list.each{|x| self << x} ; end
clear()
click to toggle source
# File lib/muflax/reservoir.rb, line 34 def clear ; @reservoir.clear; @total = 0 ; end
full?()
click to toggle source
# File lib/muflax/reservoir.rb, line 35 def full? ; @reservoir.size >= @sample_size ; end