Class: Picky::Generators::Weights::Logarithmic

Inherits:
Strategy show all
Defined in:
lib/picky/generators/weights/logarithmic.rb

Overview

Uses a logarithmic weight.

If given a constant, this will be added to the weight.

If for a key k we have x ids, the weight is: w(x): log(x) Special case: If x < 1, then we use 0.

Instance Method Summary collapse

Methods inherited from Strategy

#saved?, #to_s

Constructor Details

#initialize(constant = 0.0) ⇒ Logarithmic

Returns a new instance of Logarithmic.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/picky/generators/weights/logarithmic.rb', line 17

def initialize constant = 0.0
  @constant = constant
  # # Note: Optimisation since it is called
  # # once per indexed object.
  # #
  # if constant == 0.0
  #   install_without_constant
  # else
  #   @constant = constant
  #   install_with_constant
  # end
end

Instance Method Details

#weight_for(amount) ⇒ Object



30
31
32
33
# File 'lib/picky/generators/weights/logarithmic.rb', line 30

def weight_for amount
  return @constant if amount < 1
  @constant + Math.log(amount).round(3)
end