1 11 package org.eclipse.jdt.internal.core.builder; 12 13 public class StringSet { 14 15 public String [] values; 17 public int elementSize; public int threshold; 19 20 public StringSet(int size) { 21 this.elementSize = 0; 22 this.threshold = size; int extraRoom = (int) (size * 1.5f); 24 if (this.threshold == extraRoom) 25 extraRoom++; 26 this.values = new String [extraRoom]; 27 } 28 29 public boolean add(String value) { 30 int length = this.values.length; 31 int index = (value.hashCode() & 0x7FFFFFFF) % length; 32 String current; 33 while ((current = this.values[index]) != null) { 34 if (value.equals(current)) return false; if (++index == length) index = 0; 36 } 37 this.values[index] = value; 38 39 if (++elementSize > threshold) rehash(); 41 return true; 42 } 43 44 public void clear() { 45 for (int i = this.values.length; --i >= 0;) 46 this.values[i] = null; 47 this.elementSize = 0; 48 } 49 50 public boolean includes(String value) { 51 int length = values.length; 52 int index = (value.hashCode() & 0x7FFFFFFF) % length; 53 String current; 54 while ((current = this.values[index]) != null) { 55 if (value.equals(current)) return true; 56 if (++index == length) index = 0; 57 } 58 return false; 59 } 60 61 private void rehash() { 62 StringSet newSet = new StringSet(elementSize * 2); String current; 64 for (int i = this.values.length; --i >= 0;) 65 if ((current = this.values[i]) != null) 66 newSet.add(current); 67 68 this.values = newSet.values; 69 this.elementSize = newSet.elementSize; 70 this.threshold = newSet.threshold; 71 } 72 73 public String toString() { 74 String s = ""; String value; 76 for (int i = 0, l = this.values.length; i < l; i++) 77 if ((value = this.values[i]) != null) 78 s += value + "\n"; return s; 80 } 81 } 82 | Popular Tags |