1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 18 public final class SimpleSet implements Cloneable { 19 20 public Object [] values; 22 public int elementSize; public int threshold; 24 25 public SimpleSet() { 26 this(13); 27 } 28 29 public SimpleSet(int size) { 30 if (size < 3) size = 3; 31 this.elementSize = 0; 32 this.threshold = size + 1; this.values = new Object [2 * size + 1]; 34 } 35 36 public Object add(Object object) { 37 int length = this.values.length; 38 int index = (object.hashCode() & 0x7FFFFFFF) % length; 39 Object current; 40 while ((current = this.values[index]) != null) { 41 if (current.equals(object)) return this.values[index] = object; 42 if (++index == length) index = 0; 43 } 44 this.values[index] = object; 45 46 if (++this.elementSize > this.threshold) rehash(); 48 return object; 49 } 50 51 public Object addIfNotIncluded(Object object) { 52 int length = this.values.length; 53 int index = (object.hashCode() & 0x7FFFFFFF) % length; 54 Object current; 55 while ((current = this.values[index]) != null) { 56 if (current.equals(object)) return null; if (++index == length) index = 0; 58 } 59 this.values[index] = object; 60 61 if (++this.elementSize > this.threshold) rehash(); 63 return object; 64 } 65 66 public void asArray(Object [] copy) { 67 if (this.elementSize != copy.length) 68 throw new IllegalArgumentException (); 69 int index = this.elementSize; 70 for (int i = 0, l = this.values.length; i < l && index > 0; i++) 71 if (this.values[i] != null) 72 copy[--index] = this.values[i]; 73 } 74 75 public void clear() { 76 for (int i = this.values.length; --i >= 0;) 77 this.values[i] = null; 78 this.elementSize = 0; 79 } 80 81 public Object clone() throws CloneNotSupportedException { 82 SimpleSet result = (SimpleSet) super.clone(); 83 result.elementSize = this.elementSize; 84 result.threshold = this.threshold; 85 86 int length = this.values.length; 87 result.values = new Object [length]; 88 System.arraycopy(this.values, 0, result.values, 0, length); 89 return result; 90 } 91 92 public boolean includes(Object object) { 93 int length = values.length; 94 int index = (object.hashCode() & 0x7FFFFFFF) % length; 95 Object current; 96 while ((current = values[index]) != null) { 97 if (current.equals(object)) return true; 98 if (++index == length) index = 0; 99 } 100 return false; 101 } 102 103 public Object remove(Object object) { 104 int length = values.length; 105 int index = (object.hashCode() & 0x7FFFFFFF) % length; 106 Object current; 107 while ((current = values[index]) != null) { 108 if (current.equals(object)) { 109 elementSize--; 110 Object oldValue = values[index]; 111 values[index] = null; 112 if (values[index + 1 == length ? 0 : index + 1] != null) 113 rehash(); return oldValue; 115 } 116 if (++index == length) index = 0; 117 } 118 return null; 119 } 120 121 private void rehash() { 122 SimpleSet newSet = new SimpleSet(elementSize * 2); Object current; 124 for (int i = values.length; --i >= 0;) 125 if ((current = values[i]) != null) 126 newSet.add(current); 127 128 this.values = newSet.values; 129 this.elementSize = newSet.elementSize; 130 this.threshold = newSet.threshold; 131 } 132 133 public String toString() { 134 String s = ""; Object object; 136 for (int i = 0, l = values.length; i < l; i++) 137 if ((object = values[i]) != null) 138 s += object.toString() + "\n"; return s; 140 } 141 } 142 | Popular Tags |