1 11 package org.eclipse.jdt.internal.core.util; 12 13 16 public final class HashSetOfArray implements Cloneable { 17 18 public Object [][] set; 20 21 public int elementSize; int threshold; 23 24 public HashSetOfArray() { 25 this(13); 26 } 27 28 public HashSetOfArray(int size) { 29 30 this.elementSize = 0; 31 this.threshold = size; int extraRoom = (int) (size * 1.75f); 33 if (this.threshold == extraRoom) 34 extraRoom++; 35 this.set = new Object [extraRoom][]; 36 } 37 38 public Object clone() throws CloneNotSupportedException { 39 HashSetOfArray result = (HashSetOfArray) super.clone(); 40 result.elementSize = this.elementSize; 41 result.threshold = this.threshold; 42 43 int length = this.set.length; 44 result.set = new Object [length][]; 45 System.arraycopy(this.set, 0, result.set, 0, length); 46 47 return result; 48 } 49 50 public boolean contains(Object [] array) { 51 int length = this.set.length; 52 int index = hashCode(array) % length; 53 int arrayLength = array.length; 54 Object [] currentArray; 55 while ((currentArray = this.set[index]) != null) { 56 if (currentArray.length == arrayLength && Util.equalArraysOrNull(currentArray, array)) 57 return true; 58 if (++index == length) { 59 index = 0; 60 } 61 } 62 return false; 63 } 64 65 private int hashCode(Object [] element) { 66 return hashCode(element, element.length); 67 } 68 69 private int hashCode(Object [] element, int length) { 70 int hash = 0; 71 for (int i = length-1; i >= 0; i--) 72 hash = Util.combineHashCodes(hash, element[i].hashCode()); 73 return hash & 0x7FFFFFFF; 74 } 75 76 public Object add(Object [] array) { 77 int length = this.set.length; 78 int index = hashCode(array) % length; 79 int arrayLength = array.length; 80 Object [] currentArray; 81 while ((currentArray = this.set[index]) != null) { 82 if (currentArray.length == arrayLength && Util.equalArraysOrNull(currentArray, array)) 83 return this.set[index] = array; 84 if (++index == length) { 85 index = 0; 86 } 87 } 88 this.set[index] = array; 89 90 if (++this.elementSize > threshold) 92 rehash(); 93 return array; 94 } 95 96 public Object remove(Object [] array) { 97 int length = this.set.length; 98 int index = hashCode(array) % length; 99 int arrayLength = array.length; 100 Object [] currentArray; 101 while ((currentArray = this.set[index]) != null) { 102 if (currentArray.length == arrayLength && Util.equalArraysOrNull(currentArray, array)) { 103 Object existing = this.set[index]; 104 this.elementSize--; 105 this.set[index] = null; 106 rehash(); 107 return existing; 108 } 109 if (++index == length) { 110 index = 0; 111 } 112 } 113 return null; 114 } 115 116 private void rehash() { 117 118 HashSetOfArray newHashSet = new HashSetOfArray(elementSize * 2); Object [] currentArray; 120 for (int i = this.set.length; --i >= 0;) 121 if ((currentArray = this.set[i]) != null) 122 newHashSet.add(currentArray); 123 124 this.set = newHashSet.set; 125 this.threshold = newHashSet.threshold; 126 } 127 128 public int size() { 129 return elementSize; 130 } 131 132 public String toString() { 133 StringBuffer buffer = new StringBuffer (); 134 Object [] element; 135 for (int i = 0, length = this.set.length; i < length; i++) 136 if ((element = this.set[i]) != null) { 137 buffer.append('{'); 138 for (int j = 0, length2 = element.length; j < length2; j++) { 139 buffer.append(element[j]); 140 if (j != length2-1) 141 buffer.append(", "); } 143 buffer.append("}"); if (i != length-1) 145 buffer.append('\n'); 146 } 147 return buffer.toString(); 148 } 149 } 150 | Popular Tags |