Sha256: 7456366d8bc65ef36ce132ad97e092275a7b5cee2cf9d4e70d65603d37a87bf2

Contents?: true

Size: 964 Bytes

Versions: 25

Compression:

Stored size: 964 Bytes

Contents

module Riak
  class Client
    # A float value which decays exponentially toward 0 over time.
    # @private
    class Decaying
      attr_accessor :e
      attr_accessor :p

      # @param [Hash] opts options
      # @option options [Float] :p (0.0) The initial value
      # @option options [Float] :e (Math::E) Exponent base
      # @option options [Float] :r (Math.log(0.5) / 10) Timescale
      #   factor - defaulting to decay 50% every 10 seconds
      def initialize(opts = {})
        @p = opts[:p] || 0.0
        @e = opts[:e] || Math::E
        @r = opts[:r] || Math.log(0.5) / 10
        @t0 = Time.now
      end

      # Add to current value.
      # @param [Float] d the value to add
      def <<(d)
        @p = value + d
      end

      # @return [Float] the current value (adjusted for the time decay)
      def value
        now = Time.now
        dt = now - @t0
        @t0 = now
        @p = @p * (@e ** (@r * dt))
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
riak-client-2.6.0 lib/riak/client/decaying.rb
riak-client-2.5.0 lib/riak/client/decaying.rb
riak-client-2.3.0 lib/riak/client/decaying.rb
riak-client-2.2.2 lib/riak/client/decaying.rb
riak-client-2.2.1 lib/riak/client/decaying.rb
riak-client-noenc-1.0.0 lib/riak/client/decaying.rb
riak-client-2.2.0 lib/riak/client/decaying.rb
riak-client-2.2.0.pre1 lib/riak/client/decaying.rb
riak-client-2.1.0 lib/riak/client/decaying.rb
riak-client-1.4.5 lib/riak/client/decaying.rb
riak-client-2.0.0 lib/riak/client/decaying.rb
riak-client-2.0.0.rc2 lib/riak/client/decaying.rb
riak-client-2.0.0.rc1 lib/riak/client/decaying.rb
riak-client-1.4.4.1 lib/riak/client/decaying.rb
riak-client-1.4.4 lib/riak/client/decaying.rb
riak-client-1.4.3 lib/riak/client/decaying.rb
riak-client-1.4.2 lib/riak/client/decaying.rb
riak-client-1.4.1 lib/riak/client/decaying.rb
riak-client-1.4.0 lib/riak/client/decaying.rb
riak-client-1.2.0 lib/riak/client/decaying.rb