hashdiff 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +2 -0
- data/README.md +7 -1
- data/RELEASE.md +91 -0
- data/changelog.md +14 -0
- data/hashdiff.gemspec +0 -1
- data/lib/hashdiff/compare_hashes.rb +11 -5
- data/lib/hashdiff/diff.rb +5 -3
- data/lib/hashdiff/lcs.rb +187 -49
- data/lib/hashdiff/linear_compare_array.rb +10 -8
- data/lib/hashdiff/version.rb +1 -1
- metadata +3 -17
- data/.travis.yml +0 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fc385ddee940e69ab448bd166a0c4878510f4e07515f695d528dea2abd1ed926
|
|
4
|
+
data.tar.gz: 15c7e1b9381602736145b6192281b0b44872212b8a755201b5ef7aea3a59e01c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 415e64ca56cf5891b0f29560338acc2c7166998e03598fd1a59a76b236a41b9c42a4df581eae03034bb0e6115df39332a2094e9054565d278625c62de58c84a2
|
|
7
|
+
data.tar.gz: 99d2ee8ea5ceda94b45d330c9d0c3ef659c2f3603524bdffad72cae8953386a0ae1bdc2d1b2b4e8b16dcb4c53ddebd1f4556f64ec8a3ea4806c0e839483f9a0d
|
data/.github/workflows/ci.yml
CHANGED
data/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Hashdiff does not monkey-patch any existing class. All features are contained in
|
|
|
9
9
|
**Docs**: [Documentation](http://rubydoc.info/gems/hashdiff)
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
__WARNING__:
|
|
12
|
+
__WARNING__: Comparing large arrays, say ~10K items, that have little in common is slow and takes a lot of memory (see [#49](https://github.com/liufengyun/hashdiff/issues/49)). Large arrays that differ in a few places are fine ([#105](https://github.com/liufengyun/hashdiff/pull/105)).
|
|
13
13
|
|
|
14
14
|
## Why Hashdiff?
|
|
15
15
|
|
|
@@ -313,6 +313,12 @@ b[:b].sort!
|
|
|
313
313
|
Hashdiff.diff(a, b).should == []
|
|
314
314
|
```
|
|
315
315
|
|
|
316
|
+
## Credits
|
|
317
|
+
|
|
318
|
+
The array diff finds common items with [Myers' diff algorithm](https://doi.org/10.1007/BF01840446),
|
|
319
|
+
introduced in Hashdiff 1.3.0 by Diego Algorta ([@oboxodo](https://github.com/oboxodo))
|
|
320
|
+
in [#105](https://github.com/liufengyun/hashdiff/pull/105).
|
|
321
|
+
|
|
316
322
|
## Maintainers
|
|
317
323
|
|
|
318
324
|
- Krzysztof Rybka ([@krzysiek1507](https://github.com/krzysiek1507))
|
data/RELEASE.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Releasing Hashdiff
|
|
2
|
+
|
|
3
|
+
`master` is protected, so releases go through a pull request: update the
|
|
4
|
+
changelog and version on a release branch, open a PR, and merge it once CI
|
|
5
|
+
passes. Tag and publish the gem from the merged commit on `master`.
|
|
6
|
+
|
|
7
|
+
## Prepare the release
|
|
8
|
+
|
|
9
|
+
1. Start with a clean working tree and bring `master` up to date:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
git checkout master
|
|
13
|
+
git pull --ff-only origin master
|
|
14
|
+
git fetch --tags origin
|
|
15
|
+
git status --short
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
2. Review changes since the previous release, replacing `v1.2.1` with the
|
|
19
|
+
previous release tag:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
git log --oneline v1.2.1..HEAD
|
|
23
|
+
git diff v1.2.1..HEAD -- lib spec
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
3. Update `Hashdiff::VERSION` in `lib/hashdiff/version.rb`. The gemspec reads
|
|
27
|
+
this constant; it does not have a separate version to update.
|
|
28
|
+
|
|
29
|
+
4. Add a section at the top of `changelog.md` with the matching version and
|
|
30
|
+
release date (`YYYY-MM-DD`). Summarize changes users will notice and link to
|
|
31
|
+
the relevant pull requests and contributors.
|
|
32
|
+
|
|
33
|
+
## Validate and open a release PR
|
|
34
|
+
|
|
35
|
+
The commands below use 1.2.2 as an example; substitute the version being released.
|
|
36
|
+
|
|
37
|
+
1. Install development dependencies and run the same checks as CI (RSpec and
|
|
38
|
+
RuboCop):
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
bundle install
|
|
42
|
+
bundle exec rake
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
2. Create a release branch, then stage and review the release files. The
|
|
46
|
+
gemspec uses `git ls-files`, so stage any new files that should be packaged.
|
|
47
|
+
Optionally run `gem build hashdiff.gemspec` to check that the gem builds.
|
|
48
|
+
Generated `.gem` files are ignored and should not be committed.
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
git checkout -b release-1.2.2
|
|
52
|
+
git add changelog.md lib/hashdiff/version.rb
|
|
53
|
+
git diff --cached --check
|
|
54
|
+
git diff --cached
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
3. Commit, push the branch, and open a pull request against `master`:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
git commit -m "Release 1.2.2"
|
|
61
|
+
git push -u origin release-1.2.2
|
|
62
|
+
gh pr create --base master --title "Release 1.2.2" \
|
|
63
|
+
--body "Bump version to 1.2.2 and update the changelog."
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
4. Wait for CI to pass on the PR, get any required review, and merge it.
|
|
67
|
+
|
|
68
|
+
## Publish the gem
|
|
69
|
+
|
|
70
|
+
Merging the PR runs CI on `master`; it does not publish to RubyGems. When
|
|
71
|
+
publishing the gem, wait for CI to pass on `master` and use a maintainer account
|
|
72
|
+
with RubyGems push access. Tag the merged commit on `master` (not the release
|
|
73
|
+
branch, since a squash or merge commit has a different hash), then build and
|
|
74
|
+
publish the gem from it:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
git checkout master
|
|
78
|
+
git pull --ff-only origin master
|
|
79
|
+
git log -1 --oneline # confirm this is the merged release commit
|
|
80
|
+
git tag v1.2.2
|
|
81
|
+
git push origin v1.2.2
|
|
82
|
+
gem build hashdiff.gemspec
|
|
83
|
+
gem push hashdiff-1.2.2.gem
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Branch protection on `master` does not block pushing tags, unless the repository
|
|
87
|
+
also has tag protection rules. After publishing, delete the local release branch
|
|
88
|
+
(use `git branch -D` if the PR was squash-merged, since Git won't see it as merged).
|
|
89
|
+
|
|
90
|
+
Confirm that RubyGems lists the new version. If a version has already been
|
|
91
|
+
published, prepare a new version rather than reusing it or moving its release tag.
|
data/changelog.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## v1.3.0 2026-11-22
|
|
4
|
+
|
|
5
|
+
* Find LCS pairs with Myers' algorithm instead of an n × m table, making diffs of large, mostly similar arrays much faster and lighter on memory; diff output is unchanged [#105](https://github.com/liufengyun/hashdiff/pull/105) ([@oboxodo](https://github.com/oboxodo))
|
|
6
|
+
* Speed up diffs of equal arrays and hashes, and of hashes without a custom comparison [#109](https://github.com/liufengyun/hashdiff/pull/109) [#110](https://github.com/liufengyun/hashdiff/pull/110) [#111](https://github.com/liufengyun/hashdiff/pull/111) ([@krzysiek1507](https://github.com/krzysiek1507))
|
|
7
|
+
* Add specs for array diffs with more than one valid answer [#106](https://github.com/liufengyun/hashdiff/pull/106) ([@oboxodo](https://github.com/oboxodo))
|
|
8
|
+
* Add Ruby 3.4 and 4.0 to CI [#108](https://github.com/liufengyun/hashdiff/pull/108) ([@krzysiek1507](https://github.com/krzysiek1507))
|
|
9
|
+
* Remove the unused `bluecloth` dependency [#107](https://github.com/liufengyun/hashdiff/pull/107) ([@krzysiek1507](https://github.com/krzysiek1507))
|
|
10
|
+
* Delete the unused `.travis.yml` [#104](https://github.com/liufengyun/hashdiff/pull/104) ([@olleolleolle](https://github.com/olleolleolle))
|
|
11
|
+
|
|
12
|
+
## v1.2.2 2026-11-08
|
|
13
|
+
|
|
14
|
+
* Fix array diffs with `use_lcs: false` so that `patch!` can replay them correctly [#102](https://github.com/liufengyun/hashdiff/pull/102) ([@oboxodo](https://github.com/oboxodo))
|
|
15
|
+
* Reduce LCS memory use and improve performance for equal arrays and arrays with common leading or trailing items [#103](https://github.com/liufengyun/hashdiff/pull/103) ([@oboxodo](https://github.com/oboxodo))
|
|
16
|
+
|
|
3
17
|
## v1.2.1 2025-09-06
|
|
4
18
|
|
|
5
19
|
* Use HTTPS for the source in the Gemfile [#101](https://github.com/liufengyun/hashdiff/issues/101) ([@krzysiek1507](https://github.com/krzysiek1507))
|
data/hashdiff.gemspec
CHANGED
|
@@ -21,7 +21,6 @@ Gem::Specification.new do |s|
|
|
|
21
21
|
|
|
22
22
|
s.homepage = 'https://github.com/liufengyun/hashdiff'
|
|
23
23
|
|
|
24
|
-
s.add_development_dependency('bluecloth')
|
|
25
24
|
s.add_development_dependency('rspec', '~> 3.5')
|
|
26
25
|
s.add_development_dependency('rubocop', '>= 1.52.1') # earliest version that works with Ruby 3.3
|
|
27
26
|
s.add_development_dependency('rubocop-rspec', '> 1.16.0') # https://github.com/rubocop/rubocop-rspec/issues/461
|
|
@@ -7,6 +7,7 @@ module Hashdiff
|
|
|
7
7
|
class << self
|
|
8
8
|
def call(obj1, obj2, opts = {})
|
|
9
9
|
return [] if obj1.empty? && obj2.empty?
|
|
10
|
+
return [] if !opts[:comparison] && obj1 == obj2
|
|
10
11
|
|
|
11
12
|
obj1_keys = obj1.keys
|
|
12
13
|
obj2_keys = obj2.keys
|
|
@@ -16,8 +17,8 @@ module Hashdiff
|
|
|
16
17
|
if opts[:indifferent]
|
|
17
18
|
obj1_lookup = obj1_keys.each_with_object({}) { |k, h| h[k.to_s] = k }
|
|
18
19
|
obj2_lookup = obj2_keys.each_with_object({}) { |k, h| h[k.to_s] = k }
|
|
19
|
-
obj1_keys
|
|
20
|
-
obj2_keys
|
|
20
|
+
obj1_keys.map!(&:to_s)
|
|
21
|
+
obj2_keys.map!(&:to_s)
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
added_keys = obj2_keys - obj1_keys
|
|
@@ -89,9 +90,14 @@ module Hashdiff
|
|
|
89
90
|
else
|
|
90
91
|
# Keys are first grouped by operation type (deletions first, then changes, then additions), and then sorted
|
|
91
92
|
# alphabetically within each group.
|
|
92
|
-
deleted_keys.sort_by(&:to_s)
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
deleted_keys.sort_by!(&:to_s)
|
|
94
|
+
deleted_keys.each { |k| handle_key.call(k, :deleted) }
|
|
95
|
+
|
|
96
|
+
common_keys.sort_by!(&:to_s)
|
|
97
|
+
common_keys.each { |k| handle_key.call(k, :common) }
|
|
98
|
+
|
|
99
|
+
added_keys.sort_by!(&:to_s)
|
|
100
|
+
added_keys.each { |k| handle_key.call(k, :added) }
|
|
95
101
|
end
|
|
96
102
|
|
|
97
103
|
result
|
data/lib/hashdiff/diff.rb
CHANGED
|
@@ -100,9 +100,11 @@ module Hashdiff
|
|
|
100
100
|
|
|
101
101
|
opts[:comparison] = block if block_given?
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
if opts[:comparison]
|
|
104
|
+
# prefer to compare with provided block
|
|
105
|
+
result = custom_compare(opts[:comparison], opts[:prefix], obj1, obj2)
|
|
106
|
+
return result if result
|
|
107
|
+
end
|
|
106
108
|
|
|
107
109
|
return [] if obj1.nil? && obj2.nil?
|
|
108
110
|
|
data/lib/hashdiff/lcs.rb
CHANGED
|
@@ -6,61 +6,199 @@ module Hashdiff
|
|
|
6
6
|
# caculate array difference using LCS algorithm
|
|
7
7
|
# http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
|
|
8
8
|
def self.lcs(arraya, arrayb, options = {})
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
9
|
+
Lcs.call(arraya, arrayb, options)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# @private
|
|
13
|
+
#
|
|
14
|
+
# Finds the pairs [index in arraya, index in arrayb] of similar items that
|
|
15
|
+
# make up the longest common subsequence of the two arrays. For arrays of n
|
|
16
|
+
# and m items its work grows with (n + m) * d, d being the number of items
|
|
17
|
+
# left unpaired, and not with n * m. At worst it compares each pair of items
|
|
18
|
+
# about once.
|
|
19
|
+
#
|
|
20
|
+
# The search is the greedy algorithm of section 3 of
|
|
21
|
+
#
|
|
22
|
+
# Myers, E. W. (1986). An O(ND) difference algorithm and its variations.
|
|
23
|
+
# Algorithmica, 1(1-4), 251-266. https://doi.org/10.1007/BF01840446
|
|
24
|
+
# Free copy: https://publications.mpi-cbg.de/Myers_1986_6330.pdf
|
|
25
|
+
#
|
|
26
|
+
# https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/ and
|
|
27
|
+
# its parts 2 and 3 explain it step by step, with Ruby code.
|
|
28
|
+
#
|
|
29
|
+
# A position [a_pos, b_pos] stands for the first a_pos items of arraya and
|
|
30
|
+
# the first b_pos items of arrayb: the paper's point (x, y). Its cost is the
|
|
31
|
+
# fewest items that have to be left unpaired to get there from [0, 0], the
|
|
32
|
+
# paper's D. Its diagonal is a_pos - b_pos, the paper's k: pairing two items
|
|
33
|
+
# stays on the diagonal, leaving one unpaired moves to the next diagonal.
|
|
34
|
+
#
|
|
35
|
+
# == Where it differs from the paper
|
|
36
|
+
#
|
|
37
|
+
# The search finds the positions the paper's does and only saves work: the
|
|
38
|
+
# common suffix is paired before it starts, and it stops following the
|
|
39
|
+
# diagonals that got to the end of an array.
|
|
40
|
+
#
|
|
41
|
+
# The backtrack is not the paper's. Several subsequences can be as long, and
|
|
42
|
+
# which one is picked decides the diff. The paper walks back along the path
|
|
43
|
+
# its search took. Hashdiff used to fill a table of LCS lengths, and that
|
|
44
|
+
# table's backtrack often picks another one: for A B C A B B A and
|
|
45
|
+
# C B A B A C the paper pairs C A B A and the table pairs B A B A. The
|
|
46
|
+
# backtrack here makes the table's choices. It uses what the search stored
|
|
47
|
+
# only to tell the cost of a position.
|
|
48
|
+
#
|
|
49
|
+
# * For: Hashdiff.diff returns what it returned with the table, so diffs
|
|
50
|
+
# that are stored, or expected by a test, stay the same.
|
|
51
|
+
# * Against: the backtrack is longer than the paper's, it compares a few
|
|
52
|
+
# pairs of items the search did not, and only the search can be read side
|
|
53
|
+
# by side with the paper.
|
|
54
|
+
class Lcs
|
|
55
|
+
def self.call(arraya, arrayb, options = {})
|
|
56
|
+
return [] if arraya.empty? || arrayb.empty?
|
|
57
|
+
|
|
58
|
+
new(arraya, arrayb, options).call
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def call
|
|
62
|
+
pair_common_suffix
|
|
63
|
+
unless a_pos.zero? || b_pos.zero? # an array has no items left to pair
|
|
64
|
+
explore
|
|
65
|
+
backtrack
|
|
66
|
+
end
|
|
67
|
+
pairs.reverse
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
attr_reader :arraya, :arrayb, :options, :pairs, :reached, :lowest
|
|
73
|
+
# the position the backtrack is at, walking from the last items to the first
|
|
74
|
+
attr_accessor :a_pos, :b_pos
|
|
75
|
+
|
|
76
|
+
def initialize(arraya, arrayb, options)
|
|
77
|
+
@arraya = arraya
|
|
78
|
+
@arrayb = arrayb
|
|
79
|
+
@options = { similarity: 0.8 }.merge!(options)
|
|
80
|
+
@options[:prefix] = Hashdiff.prefix_append_array_index(@options[:prefix], '*', @options)
|
|
81
|
+
@pairs = []
|
|
82
|
+
@reached = [] # reached[cost]: the furthest a_pos that cost gets to on each diagonal
|
|
83
|
+
@lowest = [] # lowest[cost]: the first of those diagonals, the others follow 2 apart
|
|
84
|
+
self.a_pos = arraya.size
|
|
85
|
+
self.b_pos = arrayb.size
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The backtrack starts at the last items and pairs them whenever they are
|
|
89
|
+
# similar, so it always pairs the common suffix item by item. Pairing it
|
|
90
|
+
# first leaves the search only the items before it.
|
|
91
|
+
def pair_common_suffix
|
|
92
|
+
pair_and_step while a_pos > 0 && b_pos > 0 && similar_at?(a_pos - 1, b_pos - 1)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# For each cost in turn, finds how far along each diagonal that cost
|
|
96
|
+
# gets, until one gets to the backtrack's position. It compares each pair
|
|
97
|
+
# of items at most once, and only the pairs a lower cost did not get past.
|
|
98
|
+
#
|
|
99
|
+
# A diagonal that gets to the end of arrayb is done, like the ones below
|
|
100
|
+
# it, and so are the ones above a diagonal that gets to the end of arraya:
|
|
101
|
+
# the way to the last items never goes through them again.
|
|
102
|
+
#
|
|
103
|
+
# This loop is where the time goes, hence the local variables.
|
|
104
|
+
def explore
|
|
105
|
+
a_items = arraya
|
|
106
|
+
b_items = arrayb
|
|
107
|
+
opts = options
|
|
108
|
+
a_end = a_pos # the search is over the items before the common suffix
|
|
109
|
+
b_end = b_pos
|
|
110
|
+
# The paper's V: the furthest a_pos on each diagonal, -1 where no cost
|
|
111
|
+
# got yet. A negative diagonal indexes it from its end.
|
|
112
|
+
furthest = Array.new(a_end + b_end + 3, -1)
|
|
113
|
+
low_limit = -b_end
|
|
114
|
+
cost = 0
|
|
115
|
+
loop do
|
|
116
|
+
low = [-cost, low_limit].max
|
|
117
|
+
low += 1 if (cost - low).odd? # a cost only gets to every other diagonal
|
|
118
|
+
lowest << low
|
|
119
|
+
reached << (row = [])
|
|
120
|
+
diagonal = low
|
|
121
|
+
while diagonal <= cost
|
|
122
|
+
# Leave one item unpaired from the furthest position the cost before
|
|
123
|
+
# got to, on the diagonal below or on the one above.
|
|
124
|
+
below = furthest[diagonal - 1]
|
|
125
|
+
above = furthest[diagonal + 1]
|
|
126
|
+
a_at = below < above ? above : below + 1
|
|
127
|
+
b_at = a_at - diagonal
|
|
128
|
+
# Then pair items for as long as they are similar: the paper's snake.
|
|
129
|
+
while a_at < a_end && b_at < b_end && Hashdiff.similar?(a_items[a_at], b_items[b_at], opts)
|
|
130
|
+
a_at += 1
|
|
131
|
+
b_at += 1
|
|
132
|
+
end
|
|
133
|
+
row << (furthest[diagonal] = a_at)
|
|
134
|
+
if a_at >= a_end
|
|
135
|
+
return if b_at >= b_end # got to the backtrack's position
|
|
136
|
+
|
|
137
|
+
break # the diagonals above are done
|
|
138
|
+
end
|
|
139
|
+
low_limit = diagonal + 1 if b_at >= b_end # this one and those below are done
|
|
140
|
+
diagonal += 2
|
|
141
|
+
end
|
|
142
|
+
cost += 1
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Walks back to the first items, making the choices the backtrack of a
|
|
147
|
+
# table of LCS lengths makes: it pairs the two items before its position
|
|
148
|
+
# whenever they are similar, and otherwise leaves one of them unpaired.
|
|
149
|
+
def backtrack
|
|
150
|
+
cost = reached.size - 1
|
|
151
|
+
while cost < a_pos + b_pos # the items before the position still have pairs
|
|
152
|
+
if pair_before?(cost)
|
|
153
|
+
pair_and_step
|
|
154
|
+
else
|
|
155
|
+
leave_one_unpaired(cost)
|
|
156
|
+
cost -= 1
|
|
44
157
|
end
|
|
45
158
|
end
|
|
46
159
|
end
|
|
47
160
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
x -= 1
|
|
56
|
-
y -= 1
|
|
57
|
-
elsif lcs[y][x][0] == :top
|
|
58
|
-
y -= 1
|
|
59
|
-
elsif lcs[y][x][0] == :left
|
|
60
|
-
x -= 1
|
|
161
|
+
# Steps to the position before that the cost before gets to. When it gets
|
|
162
|
+
# to both, it leaves the item of arraya unpaired.
|
|
163
|
+
def leave_one_unpaired(cost)
|
|
164
|
+
if a_pos - 1 <= reached_at(cost - 1, a_pos - b_pos - 1)
|
|
165
|
+
self.a_pos = a_pos - 1
|
|
166
|
+
else
|
|
167
|
+
self.b_pos = b_pos - 1
|
|
61
168
|
end
|
|
62
169
|
end
|
|
63
170
|
|
|
64
|
-
|
|
171
|
+
# Whether the two items before the backtrack's position are similar. The
|
|
172
|
+
# search already knows for the ones it compared: those on the run of
|
|
173
|
+
# similar items the cost made on this diagonal, and the one that stopped
|
|
174
|
+
# this diagonal's run two costs earlier.
|
|
175
|
+
def pair_before?(cost)
|
|
176
|
+
diagonal = a_pos - b_pos
|
|
177
|
+
from_below = reached_at(cost - 1, diagonal - 1)
|
|
178
|
+
from_above = reached_at(cost - 1, diagonal + 1)
|
|
179
|
+
return true if a_pos > (from_below < from_above ? from_above : from_below + 1)
|
|
180
|
+
return false if a_pos - 1 == reached_at(cost - 2, diagonal)
|
|
181
|
+
|
|
182
|
+
similar_at?(a_pos - 1, b_pos - 1)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# The furthest a_pos the cost gets to on the diagonal, -1 when it cannot
|
|
186
|
+
# get to the diagonal at all. The backtrack only stands on a cheapest way
|
|
187
|
+
# to the last items, so it never asks about a diagonal the search dropped.
|
|
188
|
+
def reached_at(cost, diagonal)
|
|
189
|
+
return -1 if diagonal.abs > cost
|
|
190
|
+
|
|
191
|
+
reached[cost].fetch((diagonal - lowest[cost]) / 2)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def pair_and_step
|
|
195
|
+
self.a_pos = a_pos - 1
|
|
196
|
+
self.b_pos = b_pos - 1
|
|
197
|
+
pairs << [a_pos, b_pos]
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def similar_at?(index_a, index_b)
|
|
201
|
+
Hashdiff.similar?(arraya[index_a], arrayb[index_b], options)
|
|
202
|
+
end
|
|
65
203
|
end
|
|
66
204
|
end
|
|
@@ -13,12 +13,10 @@ module Hashdiff
|
|
|
13
13
|
|
|
14
14
|
def call
|
|
15
15
|
return [] if old_array.empty? && new_array.empty?
|
|
16
|
+
return [] if old_array == new_array
|
|
16
17
|
|
|
17
18
|
self.old_index = 0
|
|
18
19
|
self.new_index = 0
|
|
19
|
-
# by comparing the array lengths we can expect that a number of items
|
|
20
|
-
# are either added or removed
|
|
21
|
-
self.expected_additions = new_array.length - old_array.length
|
|
22
20
|
|
|
23
21
|
loop do
|
|
24
22
|
if extra_items_in_old_array?
|
|
@@ -40,7 +38,7 @@ module Hashdiff
|
|
|
40
38
|
private
|
|
41
39
|
|
|
42
40
|
attr_reader :old_array, :new_array, :options, :additions, :deletions, :differences
|
|
43
|
-
attr_accessor :old_index, :new_index
|
|
41
|
+
attr_accessor :old_index, :new_index
|
|
44
42
|
|
|
45
43
|
def initialize(old_array, new_array, options)
|
|
46
44
|
@old_array = old_array
|
|
@@ -60,6 +58,12 @@ module Hashdiff
|
|
|
60
58
|
new_index < new_array.length && old_index >= old_array.length
|
|
61
59
|
end
|
|
62
60
|
|
|
61
|
+
# by comparing the lengths of what is left of each array we can expect
|
|
62
|
+
# that a number of items are either added or removed
|
|
63
|
+
def expected_additions
|
|
64
|
+
(new_array.length - new_index) - (old_array.length - old_index)
|
|
65
|
+
end
|
|
66
|
+
|
|
63
67
|
def iterated_through_both_arrays?
|
|
64
68
|
old_index >= old_array.length && new_index >= new_array.length
|
|
65
69
|
end
|
|
@@ -69,7 +73,7 @@ module Hashdiff
|
|
|
69
73
|
return if difference.empty?
|
|
70
74
|
|
|
71
75
|
index_after_additions = index_of_match_after_additions
|
|
72
|
-
|
|
76
|
+
append_additions_before_match(index_after_additions)
|
|
73
77
|
|
|
74
78
|
index_after_deletions = index_of_match_after_deletions
|
|
75
79
|
append_deletions_before_match(index_after_deletions)
|
|
@@ -120,11 +124,10 @@ module Hashdiff
|
|
|
120
124
|
nil
|
|
121
125
|
end
|
|
122
126
|
|
|
123
|
-
def
|
|
127
|
+
def append_additions_before_match(match_index)
|
|
124
128
|
return unless match_index
|
|
125
129
|
|
|
126
130
|
(new_index...match_index).each { |i| append_addition(new_array[i], i) }
|
|
127
|
-
self.expected_additions = expected_additions - (match_index - new_index)
|
|
128
131
|
self.new_index = match_index
|
|
129
132
|
end
|
|
130
133
|
|
|
@@ -132,7 +135,6 @@ module Hashdiff
|
|
|
132
135
|
return unless match_index
|
|
133
136
|
|
|
134
137
|
(old_index...match_index).each { |i| append_deletion(old_array[i], i) }
|
|
135
|
-
self.expected_additions = expected_additions + (match_index - new_index)
|
|
136
138
|
self.old_index = match_index
|
|
137
139
|
end
|
|
138
140
|
|
data/lib/hashdiff/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hashdiff
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Liu Fengyun
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-11-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: bluecloth
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - ">="
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '0'
|
|
20
|
-
type: :development
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - ">="
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '0'
|
|
27
13
|
- !ruby/object:Gem::Dependency
|
|
28
14
|
name: rspec
|
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -92,11 +78,11 @@ files:
|
|
|
92
78
|
- ".gitignore"
|
|
93
79
|
- ".rspec"
|
|
94
80
|
- ".rubocop.yml"
|
|
95
|
-
- ".travis.yml"
|
|
96
81
|
- ".yardopts"
|
|
97
82
|
- Gemfile
|
|
98
83
|
- LICENSE
|
|
99
84
|
- README.md
|
|
85
|
+
- RELEASE.md
|
|
100
86
|
- Rakefile
|
|
101
87
|
- changelog.md
|
|
102
88
|
- hashdiff.gemspec
|