Class: PCBR

Inherits:
Object
  • Object
show all
Defined in:
lib/pcbr.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ PCBR

Returns a new instance of PCBR.



7
8
9
10
# File 'lib/pcbr.rb', line 7

def initialize &block
  @table = []
  @callback = block || ->*_{[*_[0]]}
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/pcbr.rb', line 5

def table
  @table
end

Instance Method Details

#score(key) ⇒ Object



28
29
30
# File 'lib/pcbr.rb', line 28

def score key
  @table.assoc(key).last
end

#sortedObject



32
33
34
35
# File 'lib/pcbr.rb', line 32

def sorted
  # from the best to the worst
  @table.sort_by.with_index{ |item, i| [-item.last, i] }.map(&:first)
end

#store(key, *vector) ⇒ Object

def size

@table.size

end



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pcbr.rb', line 16

def store key, *vector
  vector = vector.empty? ? [key] : vector.first
  score = @table.map do |item|
    @callback[vector, key].zip(@callback[item[1], item[0]]).map do |a, b|
      a <=> b
    end.uniq.inject(0, :+).tap do |point|
      item[2] -= point
    end
  end.inject 0, :+
  @table.push [key, vector, score]
end