Sha256: 91713a6251ed7c992c13eb35eb17959caf0eabe5a8b272ea0a6a329100d1c422

Contents?: true

Size: 921 Bytes

Versions: 53

Compression:

Stored size: 921 Bytes

Contents

module Cacher

  module Weights

    # Uses a logarithmic 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

      # Generates a partial index from the given index.
      #
      def generate_from index
        index.inject({}) do |hash, text_ids|
          text, ids = *text_ids
          weight = weight_for ids.size
          hash[text] ||= weight.round(2) if weight
          hash
        end
      end

      # 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 0 if amount < 1
        Math.log amount
      end

    end

  end

end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
picky-1.4.1 lib/picky/cacher/weights/logarithmic.rb
picky-1.4.0 lib/picky/cacher/weights/logarithmic.rb
picky-1.3.4 lib/picky/cacher/weights/logarithmic.rb
picky-1.3.3 lib/picky/cacher/weights/logarithmic.rb
picky-1.3.2 lib/picky/cacher/weights/logarithmic.rb
picky-1.3.1 lib/picky/cacher/weights/logarithmic.rb
picky-1.3.0 lib/picky/cacher/weights/logarithmic.rb
picky-1.2.4 lib/picky/cacher/weights/logarithmic.rb
picky-1.2.3 lib/picky/cacher/weights/logarithmic.rb
picky-1.2.2 lib/picky/cacher/weights/logarithmic.rb
picky-1.2.1 lib/picky/cacher/weights/logarithmic.rb
picky-1.2.0 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.7 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.6 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.5 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.4 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.3 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.2 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.1 lib/picky/cacher/weights/logarithmic.rb
picky-1.1.0 lib/picky/cacher/weights/logarithmic.rb