hashdiff 1.2.2 → 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 +28 -13
- data/changelog.md +9 -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 +143 -69
- data/lib/hashdiff/linear_compare_array.rb +1 -0
- data/lib/hashdiff/version.rb +1 -1
- metadata +2 -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
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Releasing Hashdiff
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
release
|
|
5
|
-
|
|
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
6
|
|
|
7
7
|
## Prepare the release
|
|
8
8
|
|
|
@@ -30,7 +30,7 @@ process (for example, `8846a2c` for 1.2.1). A release PR is not required.
|
|
|
30
30
|
release date (`YYYY-MM-DD`). Summarize changes users will notice and link to
|
|
31
31
|
the relevant pull requests and contributors.
|
|
32
32
|
|
|
33
|
-
## Validate and
|
|
33
|
+
## Validate and open a release PR
|
|
34
34
|
|
|
35
35
|
The commands below use 1.2.2 as an example; substitute the version being released.
|
|
36
36
|
|
|
@@ -42,35 +42,50 @@ The commands below use 1.2.2 as an example; substitute the version being release
|
|
|
42
42
|
bundle exec rake
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
2.
|
|
46
|
-
`git ls-files`, so stage any new files that should be packaged
|
|
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.
|
|
47
48
|
Generated `.gem` files are ignored and should not be committed.
|
|
48
49
|
|
|
49
50
|
```sh
|
|
50
|
-
git
|
|
51
|
+
git checkout -b release-1.2.2
|
|
52
|
+
git add changelog.md lib/hashdiff/version.rb
|
|
51
53
|
git diff --cached --check
|
|
52
54
|
git diff --cached
|
|
53
|
-
gem build hashdiff.gemspec
|
|
54
55
|
```
|
|
55
56
|
|
|
56
|
-
3. Commit
|
|
57
|
+
3. Commit, push the branch, and open a pull request against `master`:
|
|
57
58
|
|
|
58
59
|
```sh
|
|
59
60
|
git commit -m "Release 1.2.2"
|
|
60
|
-
git push origin
|
|
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."
|
|
61
64
|
```
|
|
62
65
|
|
|
66
|
+
4. Wait for CI to pass on the PR, get any required review, and merge it.
|
|
67
|
+
|
|
63
68
|
## Publish the gem
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
gem, wait for CI to pass and use a maintainer account
|
|
67
|
-
|
|
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:
|
|
68
75
|
|
|
69
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
|
|
70
80
|
git tag v1.2.2
|
|
71
81
|
git push origin v1.2.2
|
|
82
|
+
gem build hashdiff.gemspec
|
|
72
83
|
gem push hashdiff-1.2.2.gem
|
|
73
84
|
```
|
|
74
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
|
+
|
|
75
90
|
Confirm that RubyGems lists the new version. If a version has already been
|
|
76
91
|
published, prepare a new version rather than reusing it or moving its release tag.
|
data/changelog.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
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
|
+
|
|
3
12
|
## v1.2.2 2026-11-08
|
|
4
13
|
|
|
5
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))
|
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
|
@@ -12,7 +12,45 @@ module Hashdiff
|
|
|
12
12
|
# @private
|
|
13
13
|
#
|
|
14
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.
|
|
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.
|
|
16
54
|
class Lcs
|
|
17
55
|
def self.call(arraya, arrayb, options = {})
|
|
18
56
|
return [] if arraya.empty? || arrayb.empty?
|
|
@@ -20,21 +58,20 @@ module Hashdiff
|
|
|
20
58
|
new(arraya, arrayb, options).call
|
|
21
59
|
end
|
|
22
60
|
|
|
23
|
-
# The table costs O(n * m), so it only covers the items between the common
|
|
24
|
-
# prefix and suffix. The result is the same as with a full table.
|
|
25
61
|
def call
|
|
26
62
|
pair_common_suffix
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
63
|
+
unless a_pos.zero? || b_pos.zero? # an array has no items left to pair
|
|
64
|
+
explore
|
|
65
|
+
backtrack
|
|
66
|
+
end
|
|
30
67
|
pairs.reverse
|
|
31
68
|
end
|
|
32
69
|
|
|
33
70
|
private
|
|
34
71
|
|
|
35
|
-
attr_reader :arraya, :arrayb, :options, :pairs
|
|
36
|
-
# the
|
|
37
|
-
attr_accessor :
|
|
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
|
|
38
75
|
|
|
39
76
|
def initialize(arraya, arrayb, options)
|
|
40
77
|
@arraya = arraya
|
|
@@ -42,89 +79,126 @@ module Hashdiff
|
|
|
42
79
|
@options = { similarity: 0.8 }.merge!(options)
|
|
43
80
|
@options[:prefix] = Hashdiff.prefix_append_array_index(@options[:prefix], '*', @options)
|
|
44
81
|
@pairs = []
|
|
45
|
-
|
|
46
|
-
|
|
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
|
|
47
86
|
end
|
|
48
87
|
|
|
49
88
|
# The backtrack starts at the last items and pairs them whenever they are
|
|
50
|
-
# similar, so it always pairs the common suffix item by item.
|
|
89
|
+
# similar, so it always pairs the common suffix item by item. Pairing it
|
|
90
|
+
# first leaves the search only the items before it.
|
|
51
91
|
def pair_common_suffix
|
|
52
|
-
pair_and_step while
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def common_prefix_length
|
|
56
|
-
length = 0
|
|
57
|
-
length += 1 while length <= a_index && length <= b_index && similar_at?(length, length)
|
|
58
|
-
length
|
|
92
|
+
pair_and_step while a_pos > 0 && b_pos > 0 && similar_at?(a_pos - 1, b_pos - 1)
|
|
59
93
|
end
|
|
60
94
|
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
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.
|
|
64
98
|
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
|
82
141
|
end
|
|
142
|
+
cost += 1
|
|
83
143
|
end
|
|
84
|
-
table
|
|
85
144
|
end
|
|
86
145
|
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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)
|
|
93
153
|
pair_and_step
|
|
94
|
-
elsif direction == :top
|
|
95
|
-
self.b_index = b_index - 1
|
|
96
154
|
else
|
|
97
|
-
|
|
155
|
+
leave_one_unpaired(cost)
|
|
156
|
+
cost -= 1
|
|
98
157
|
end
|
|
99
158
|
end
|
|
100
159
|
end
|
|
101
160
|
|
|
102
|
-
#
|
|
103
|
-
#
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
while a_index >= 0 && b_index >= 0
|
|
110
|
-
if a_index == b_index || similar_at?(a_index, b_index)
|
|
111
|
-
pair_and_step
|
|
112
|
-
elsif b_index > a_index
|
|
113
|
-
self.b_index = b_index - 1
|
|
114
|
-
else
|
|
115
|
-
self.a_index = a_index - 1
|
|
116
|
-
end
|
|
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
|
|
117
168
|
end
|
|
118
169
|
end
|
|
119
170
|
|
|
120
|
-
|
|
121
|
-
|
|
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)
|
|
122
192
|
end
|
|
123
193
|
|
|
124
194
|
def pair_and_step
|
|
125
|
-
|
|
126
|
-
self.
|
|
127
|
-
|
|
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)
|
|
128
202
|
end
|
|
129
203
|
end
|
|
130
204
|
end
|
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: 2026-11-
|
|
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,7 +78,6 @@ files:
|
|
|
92
78
|
- ".gitignore"
|
|
93
79
|
- ".rspec"
|
|
94
80
|
- ".rubocop.yml"
|
|
95
|
-
- ".travis.yml"
|
|
96
81
|
- ".yardopts"
|
|
97
82
|
- Gemfile
|
|
98
83
|
- LICENSE
|