Skip to content

Commit f19e792

Browse files
committed
use [].min
did not perform as well in older rubies?
1 parent 9aea248 commit f19e792

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

lib/edits/damerau_levenshtein.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ def self.distance(seq1, seq2)
6767

6868
# TODO: do insertion/deletion need to be considered when
6969
# seq1_item == seq2_item ?
70-
deletion = matrix[row][col + 1] + 1
71-
insertion = matrix[row + 1][col] + 1
72-
substitution = matrix[row][col] + sub_cost
73-
74-
# step cost is min of operation costs
75-
cost = substitution < insertion ? substitution : insertion
76-
cost = deletion if deletion < cost
77-
cost = transposition if transposition < cost
70+
#
71+
# substitution, deletion, insertion, transposition
72+
cost = [
73+
matrix[row][col] + sub_cost,
74+
matrix[row][col + 1] + 1,
75+
matrix[row + 1][col] + 1,
76+
transposition
77+
].min
7878

7979
matrix[row + 1][col + 1] = cost
8080

lib/edits/levenshtein.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,25 @@ def self.distance(seq1, seq2)
3737
last_row = 0.upto(cols).to_a
3838

3939
rows.times do |row|
40-
last_col = row + 1
41-
40+
prev_col_cost = row + 1
4241
seq1_item = seq1[row]
4342

4443
cols.times do |col|
45-
deletion = last_row[col + 1] + 1
46-
insertion = last_col + 1
47-
substitution = last_row[col] + (seq1_item == seq2[col] ? 0 : 1)
48-
44+
# | Xs | Xd |
45+
# | Xi | ? |
4946
# step cost is min of operation costs
50-
cost = deletion < insertion ? deletion : insertion
51-
cost = substitution if substitution < cost
47+
# substitution, deletion, insertion
48+
cost = [
49+
last_row[col] + (seq1_item == seq2[col] ? 0 : 1),
50+
last_row[col + 1] + 1,
51+
prev_col_cost + 1
52+
].min
5253

5354
# overwrite previous row as we progress
54-
last_row[col] = last_col
55-
last_col = cost
55+
last_row[col] = prev_col_cost
56+
prev_col_cost = cost
5657
end
57-
last_row[cols] = last_col
58+
last_row[cols] = prev_col_cost
5859
end
5960

6061
last_row[cols]

lib/edits/restricted_edit.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,25 @@ def self.distance(seq1, seq2)
5151
curr_row, last_row, lastlast_row = lastlast_row, curr_row, last_row
5252

5353
curr_row[0] = row + 1
54-
curr_item = seq1[row]
54+
seq1_item = seq1[row]
5555

5656
cols.times do |col|
57-
sub_cost = curr_item == seq2[col] ? 0 : 1
58-
is_swap = sub_cost == 1 &&
57+
sub_cost = seq1_item == seq2[col] ? 0 : 1
58+
is_swap = sub_cost.positive? &&
5959
row.positive? && col.positive? &&
60-
curr_item == seq2[col - 1] &&
60+
seq1_item == seq2[col - 1] &&
6161
seq1[row - 1] == seq2[col]
6262

63-
deletion = last_row[col + 1] + 1
64-
insertion = curr_row[col] + 1
65-
substitution = last_row[col] + sub_cost
66-
63+
# | Xt | | |
64+
# | | Xs | Xd |
65+
# | | Xi | ? |
6766
# step cost is min of operation costs
68-
cost = deletion < insertion ? deletion : insertion
69-
cost = substitution if substitution < cost
67+
# substitution, deletion, insertion, transposition
68+
cost = [
69+
last_row[col] + sub_cost,
70+
last_row[col + 1] + 1,
71+
curr_row[col] + 1
72+
].min
7073

7174
if is_swap
7275
swap = lastlast_row[col - 1] + 1

0 commit comments

Comments
 (0)