From 6423a7665516d738ae50d272e3b4ca72cdb89a9d Mon Sep 17 00:00:00 2001 From: pascalschumacher Date: Thu, 20 Oct 2016 21:51:01 +0200 Subject: [PATCH] LANG-1277: StringUtils#getLevenshteinDistance reduce memory consumption minimal clean-up add changes.xml entry --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/StringUtils.java | 23 +++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b3f4b00a3..4d31ea2c3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. + StringUtils#getLevenshteinDistance reduce memory consumption ArrayUtils#add confusing example in javadoc StringUtils#isAnyEmpty and #isAnyBlank should return false for an empty array Add StringUtils#isAnyNotEmpty and #isAnyNotBlank diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index d06d60ceb..559444de9 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -7736,11 +7736,9 @@ public class StringUtils { * another, where each change is a single character modification (deletion, * insertion or substitution).

* - *

The previous implementation of the Levenshtein distance algorithm - * was from - * https://web.archive.org/web/20120526085419/http://www.merriampark.com/ldjava.htm

- * - *

This implementation only need one single-dimensional arrays of length s.length() + 1

+ *

The implementation uses a single-dimensional array of length s.length() + 1. See + * + * http://blog.softwx.net/2014/12/optimizing-levenshtein-algorithm-in-c.html for details.

* *
      * StringUtils.getLevenshteinDistance(null, *)             = IllegalArgumentException
@@ -7768,13 +7766,8 @@ public class StringUtils {
             throw new IllegalArgumentException("Strings must not be null");
         }
 
-        /*
-           This implementation use two variable to record the previous cost counts,
-           So this implementation use less memory than previous impl.
-         */
-
-        int n = s.length(); // length of s
-        int m = t.length(); // length of t
+        int n = s.length();
+        int m = t.length();
 
         if (n == 0) {
             return m;
@@ -7799,19 +7792,19 @@ public class StringUtils {
         int upper;
 
         char t_j; // jth character of t
-        int cost; // cost
+        int cost;
 
         for (i = 0; i <= n; i++) {
             p[i] = i;
         }
 
         for (j = 1; j <= m; j++) {
-        	upper_left = p[0];
+            upper_left = p[0];
             t_j = t.charAt(j - 1);
             p[0] = j;
 
             for (i = 1; i <= n; i++) {
-            	upper = p[i];
+                upper = p[i];
                 cost = s.charAt(i - 1) == t_j ? 0 : 1;
                 // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
                 p[i] = Math.min(Math.min(p[i - 1] + 1, p[i] + 1), upper_left + cost);