Sha256: 6993782cde9c3291df071795c57b16b9c8d6abe248f59da7005d0103ad2fc3a8

Contents?: true

Size: 1.56 KB

Versions: 78

Compression:

Stored size: 1.56 KB

Contents

module Picky

  module Generators

    module Weights
      
      # 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.
      #
      class Logarithmic < Strategy
        
        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
        
        def weight_for amount
          return @constant if amount < 1
          @constant + Math.log(amount).round(3)
        end
        
        # def install_with_constant
        #   # Sets the weight value.
        #   #
        #   # If the size is 0 or one, we would get -Infinity or 0.0.
        #   # Thus we do not set a value if there is just one. The default, dynamically, is 0.
        #   #
        #   # BUT: We need the value, even if 0. To designate that there IS a weight!
        #   #
        #   def weight_for amount
        #     return @constant if amount < 1
        #     @constant + Math.log(amount).round(3)
        #   end
        # end
        # def install_without_constant
        #   def weight_for amount
        #     return 0 if amount < 1
        #     Math.log(amount).round 3
        #   end
        # end
        
      end

    end

  end

end

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
picky-4.31.3 lib/picky/generators/weights/logarithmic.rb
picky-4.31.2 lib/picky/generators/weights/logarithmic.rb
picky-4.31.1 lib/picky/generators/weights/logarithmic.rb
picky-4.31.0 lib/picky/generators/weights/logarithmic.rb
picky-4.30.0 lib/picky/generators/weights/logarithmic.rb
picky-4.29.0 lib/picky/generators/weights/logarithmic.rb
picky-4.28.1 lib/picky/generators/weights/logarithmic.rb
picky-4.27.1 lib/picky/generators/weights/logarithmic.rb
picky-4.27.0 lib/picky/generators/weights/logarithmic.rb
picky-4.26.2 lib/picky/generators/weights/logarithmic.rb
picky-4.26.1 lib/picky/generators/weights/logarithmic.rb
picky-4.26.0 lib/picky/generators/weights/logarithmic.rb
picky-4.25.3 lib/picky/generators/weights/logarithmic.rb
picky-4.25.2 lib/picky/generators/weights/logarithmic.rb
picky-4.25.1 lib/picky/generators/weights/logarithmic.rb
picky-4.25.0 lib/picky/generators/weights/logarithmic.rb
picky-4.24.0 lib/picky/generators/weights/logarithmic.rb
picky-4.23.2 lib/picky/generators/weights/logarithmic.rb
picky-4.23.1 lib/picky/generators/weights/logarithmic.rb
picky-4.23.0 lib/picky/generators/weights/logarithmic.rb