Sha256: debf629a007bde4a8dbe4ef0d396582306a16044be5377231c2b4813cdca91f1

Contents?: true

Size: 1.68 KB

Versions: 89

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Hashdiff
  # @private
  #
  # caculate array difference using LCS algorithm
  # http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
  def self.lcs(arraya, arrayb, options = {})
    return [] if arraya.empty? || arrayb.empty?

    opts = { similarity: 0.8 }.merge!(options)

    opts[:prefix] = prefix_append_array_index(opts[:prefix], '*', opts)

    a_start = b_start = 0
    a_finish = arraya.size - 1
    b_finish = arrayb.size - 1
    vector = []

    lcs = []
    (b_start..b_finish).each do |bi|
      lcs[bi] = []
      (a_start..a_finish).each do |ai|
        if similar?(arraya[ai], arrayb[bi], opts)
          topleft = (ai > 0) && (bi > 0) ? lcs[bi - 1][ai - 1][1] : 0
          lcs[bi][ai] = [:topleft, topleft + 1]
        elsif (top = bi > 0 ? lcs[bi - 1][ai][1] : 0)
          left = ai > 0 ? lcs[bi][ai - 1][1] : 0
          count = top > left ? top : left

          direction = if top > left
                        :top
                      elsif top < left
                        :left
                      elsif bi.zero?
                        :top
                      elsif ai.zero?
                        :left
                      else
                        :both
                      end

          lcs[bi][ai] = [direction, count]
        end
      end
    end

    x = a_finish
    y = b_finish
    while (x >= 0) && (y >= 0) && (lcs[y][x][1] > 0)
      if lcs[y][x][0] == :both
        x -= 1
      elsif lcs[y][x][0] == :topleft
        vector.insert(0, [x, y])
        x -= 1
        y -= 1
      elsif lcs[y][x][0] == :top
        y -= 1
      elsif lcs[y][x][0] == :left
        x -= 1
      end
    end

    vector
  end
end

Version data entries

89 entries across 87 versions & 8 rubygems

Version Path
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.57.1 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.54.15 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.53.79 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.53.17 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.53.3 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.53.1 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb
cloudsmith-api-0.52.121 vendor/bundle/ruby/2.6.0/gems/hashdiff-1.0.1/lib/hashdiff/lcs.rb