commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r789567 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/LookupTranslator.java
Date Tue, 30 Jun 2009 05:47:40 GMT
Author: bayard
Date: Tue Jun 30 05:47:39 2009
New Revision: 789567

URL: http://svn.apache.org/viewvc?rev=789567&view=rev
Log:
Performance improvement. Switching from looping through a doubled array to using a Map. This
probably costs more for simple cases like Java/EcmaScript/Xml, but makes up for it in the
Html case. This gets performance of the testUnescapeHexCharsHtml method back down to near
the same region as the original code

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/LookupTranslator.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/LookupTranslator.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/LookupTranslator.java?rev=789567&r1=789566&r2=789567&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/LookupTranslator.java
(original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/LookupTranslator.java
Tue Jun 30 05:47:39 2009
@@ -18,14 +18,18 @@
 
 import java.io.IOException;
 import java.io.Writer;
+import java.util.HashMap;
 
 /**
  * Translates a value using a lookup table. 
  * @since 3.0
  */
+// TODO: Replace with a RegexLookup? Performance test.
 public class LookupTranslator extends CharSequenceTranslator {
 
-    protected CharSequence[][] lookup;
+    private HashMap<CharSequence, CharSequence> lookupMap;
+    private int shortest = Integer.MAX_VALUE;
+    private int longest = 0;
 
     /**
      * Define the lookup table to be used in translation
@@ -33,18 +37,34 @@
      * @param CharSequence[][] Lookup table of size [*][2]
      */
     public LookupTranslator(CharSequence[][] lookup) {
-        this.lookup = lookup;
+        lookupMap = new HashMap<CharSequence, CharSequence>();
+        for(CharSequence[] seq : lookup) {
+            this.lookupMap.put(seq[0], seq[1]);
+            int sz = seq[0].length();
+            if(sz < shortest) {
+                shortest = sz;
+            }
+            if(sz > longest) {
+                longest = sz;
+            }
+        }
     }
 
     /**
      * {@inheritDoc}
      */
     public int translate(CharSequence input, int index, Writer out) throws IOException {
-        CharSequence subsequence = input.subSequence(index, input.length());
-        for(CharSequence[] seq : lookup) {
-            if( subsequence.toString().startsWith(seq[0].toString()) ) {
-                out.write(seq[1].toString());
-                return seq[0].length();
+        int max = longest;
+        if(index + longest > input.length()) {
+            max = input.length() - index;
+        }
+        // descend so as to get a greedy algorithm
+        for(int i=max; i >= shortest; i--) {
+            CharSequence subSeq = input.subSequence(index, index + i);
+            CharSequence result = lookupMap.get(subSeq);
+            if(result != null) {
+                out.write(result.toString());
+                return i;
             }
         }
         return 0;



Mime
View raw message