class PCBR::Storage
Attributes
set[R]
table[R]
Public Class Methods
new(&block)
click to toggle source
# File lib/pcbr.rb, line 24 def initialize &block require "set" @set = ::Set.new @table = [] @callback = block || @@default_lambda end
Public Instance Methods
score(key)
click to toggle source
# File lib/pcbr.rb, line 44 def score key @table.assoc(key)[2] end
sorted()
click to toggle source
# File lib/pcbr.rb, line 48 def sorted # from the best to the worst @table.sort_by.with_index{ |item, i| [-item[2], i] }.map(&:first) end
store(key, vector = nil)
click to toggle source
# File lib/pcbr.rb, line 31 def store key, vector = nil raise Error.new "duplicating key" if @set.include? key vector = Array key if vector.nil? score = 0 @table.each do |item| point = @callback.call vector, item[1] item[2] -= point score += point end @set.add key @table.push [key, vector, score] end