Sha256: a04717a04a8b8af9b282ffb9bc0b943a4ed1b7ada5caf380b0d9699f8a1b1c98

Contents?: true

Size: 787 Bytes

Versions: 1

Compression:

Stored size: 787 Bytes

Contents

# encoding: utf-8

class WeightedSelector

  def initialize
    @sums = []
    @elements = []
    @total = 0
  end

  def add(element, probability)
    @elements << element
    @total += probability
    @sums << @total
  end

  def delete(element)
    idx = @elements.index(element)
    if idx
      @total -= @sums[idx]
      @sums.delete_at(idx)
      @elements.delete_at(idx)
    end
  end

  def fill_up(element)
    add(element, 1 - @total) if @total < 1
  end

  def empty?
    @elements.empty?
  end

  # http://stackoverflow.com/questions/4463561/weighed-random-selection-from-array
  def pick_one_with_index
    rnd = Kernel.rand * @total
    idx = @sums.index { |x| x >= rnd }
    [@elements[idx], idx]
  end

  def pick_one
    pick_one_with_index[0] unless empty?
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
liquid-ext-1.2.2 lib/liquid/weighted_selector.rb