1 11 package org.eclipse.jdt.internal.core.util; 12 13 import org.eclipse.jdt.core.compiler.CharOperation; 14 15 public final class SimpleWordSet { 16 17 public char[][] words; 19 public int elementSize; public int threshold; 21 22 public SimpleWordSet(int size) { 23 this.elementSize = 0; 24 this.threshold = size; int extraRoom = (int) (size * 1.5f); 26 if (this.threshold == extraRoom) 27 extraRoom++; 28 this.words = new char[extraRoom][]; 29 } 30 31 public char[] add(char[] word) { 32 int length = this.words.length; 33 int index = CharOperation.hashCode(word) % length; 34 char[] current; 35 while ((current = words[index]) != null) { 36 if (CharOperation.equals(current, word)) return current; 37 if (++index == length) index = 0; 38 } 39 words[index] = word; 40 41 if (++elementSize > threshold) rehash(); 43 return word; 44 } 45 46 public boolean includes(char[] word) { 47 int length = this.words.length; 48 int index = CharOperation.hashCode(word) % length; 49 char[] current; 50 while ((current = words[index]) != null) { 51 if (CharOperation.equals(current, word)) return true; 52 if (++index == length) index = 0; 53 } 54 return false; 55 } 56 57 private void rehash() { 58 SimpleWordSet newSet = new SimpleWordSet(elementSize * 2); char[] current; 60 for (int i = words.length; --i >= 0;) 61 if ((current = words[i]) != null) 62 newSet.add(current); 63 64 this.words = newSet.words; 65 this.elementSize = newSet.elementSize; 66 this.threshold = newSet.threshold; 67 } 68 } 69 | Popular Tags |