Index: ../lucene/src/java/org/apache/lucene/util/SimpleStringInterner.java =================================================================== --- ../lucene/src/java/org/apache/lucene/util/SimpleStringInterner.java (revision 801344) +++ ../lucene/src/java/org/apache/lucene/util/SimpleStringInterner.java Mon Aug 24 15:09:01 IST 2009 @@ -79,4 +79,50 @@ } return s; } + + public String intern(char[] arr, int offset, int len) { + int h = hash(arr, offset, len); + // In the future, it may be worth augmenting the string hash + // if the lower bits need better distribution. + int slot = h & (cache.length-1); + + Entry first = this.cache[slot]; + Entry nextToLast = null; + + int chainLength = 0; + + for(Entry e=first; e!=null; e=e.next) { + if (e.hash == h && isEqual(e.str,arr,offset, len)) { + return e.str; -} \ No newline at end of file + } + + chainLength++; + if (e.next != null) { + nextToLast = e; + } + } + // insertion-order cache: add new entry at head + String s = new String(arr, offset, len).intern(); + this.cache[slot] = new Entry(s, h, first); + if (chainLength >= maxChainLength) { + // prune last entry + nextToLast.next = null; + } + return s; + } + + private static boolean isEqual(String s, char[] arr, int offset, int len) { + for (int i = 0; i < len; i++) { + if(s.charAt(i) != arr[i + offset]) return false; + } + return true; + } + + private static int hash(char[] val, int offset, int len) { + int h = 0; + for (int i = 0; i < len; i++) { + h = 31 * h + val[offset++]; + } + return h; + } +} \ No newline at end of file