1 11 package org.eclipse.core.internal.resources; 12 13 import org.eclipse.core.internal.utils.IStringPoolParticipant; 14 import org.eclipse.core.internal.utils.StringPool; 15 16 public class MarkerSet implements Cloneable , IStringPoolParticipant { 17 protected static final int MINIMUM_SIZE = 5; 18 protected int elementCount = 0; 19 protected IMarkerSetElement[] elements; 20 21 public MarkerSet() { 22 this(MINIMUM_SIZE); 23 } 24 25 public MarkerSet(int capacity) { 26 super(); 27 this.elements = new IMarkerSetElement[Math.max(MINIMUM_SIZE, capacity * 2)]; 28 } 29 30 public void add(IMarkerSetElement element) { 31 if (element == null) 32 return; 33 int hash = hashFor(element.getId()) % elements.length; 34 35 for (int i = hash; i < elements.length; i++) { 37 if (elements[i] == null) { 38 elements[i] = element; 39 elementCount++; 40 if (shouldGrow()) 42 expand(); 43 return; 44 } 45 } 46 47 for (int i = 0; i < hash - 1; i++) { 49 if (elements[i] == null) { 50 elements[i] = element; 51 elementCount++; 52 if (shouldGrow()) 54 expand(); 55 return; 56 } 57 } 58 59 expand(); 61 add(element); 62 } 63 64 public void addAll(IMarkerSetElement[] toAdd) { 65 for (int i = 0; i < toAdd.length; i++) 66 add(toAdd[i]); 67 } 68 69 protected Object clone() { 70 try { 71 MarkerSet copy = (MarkerSet) super.clone(); 72 copy.elements = (IMarkerSetElement[]) elements.clone(); 74 return copy; 75 } catch (CloneNotSupportedException e) { 76 return null; 78 } 79 } 80 81 public boolean contains(long id) { 82 return get(id) != null; 83 } 84 85 public IMarkerSetElement[] elements() { 86 IMarkerSetElement[] result = new IMarkerSetElement[elementCount]; 87 int j = 0; 88 for (int i = 0; i < elements.length; i++) { 89 IMarkerSetElement element = elements[i]; 90 if (element != null) 91 result[j++] = element; 92 } 93 return result; 94 } 95 96 100 protected void expand() { 101 IMarkerSetElement[] array = new IMarkerSetElement[elements.length * 2]; 102 int maxArrayIndex = array.length - 1; 103 for (int i = 0; i < elements.length; i++) { 104 IMarkerSetElement element = elements[i]; 105 if (element != null) { 106 int hash = hashFor(element.getId()) % array.length; 107 while (array[hash] != null) { 108 hash++; 109 if (hash > maxArrayIndex) 110 hash = 0; 111 } 112 array[hash] = element; 113 } 114 } 115 elements = array; 116 } 117 118 122 public IMarkerSetElement get(long id) { 123 if (elementCount == 0) 124 return null; 125 int hash = hashFor(id) % elements.length; 126 127 for (int i = hash; i < elements.length; i++) { 129 IMarkerSetElement element = elements[i]; 130 if (element == null) 131 return null; 132 if (element.getId() == id) 133 return element; 134 } 135 136 for (int i = 0; i < hash - 1; i++) { 138 IMarkerSetElement element = elements[i]; 139 if (element == null) 140 return null; 141 if (element.getId() == id) 142 return element; 143 } 144 145 return null; 147 } 148 149 private int hashFor(long id) { 150 return Math.abs((int) id); 151 } 152 153 public boolean isEmpty() { 154 return elementCount == 0; 155 } 156 157 161 protected void rehashTo(int anIndex) { 162 163 int target = anIndex; 164 int index = anIndex + 1; 165 if (index >= elements.length) 166 index = 0; 167 IMarkerSetElement element = elements[index]; 168 while (element != null) { 169 int hashIndex = hashFor(element.getId()) % elements.length; 170 boolean match; 171 if (index < target) 172 match = !(hashIndex > target || hashIndex <= index); 173 else 174 match = !(hashIndex > target && hashIndex <= index); 175 if (match) { 176 elements[target] = element; 177 target = index; 178 } 179 index++; 180 if (index >= elements.length) 181 index = 0; 182 element = elements[index]; 183 } 184 elements[target] = null; 185 } 186 187 public void remove(long id) { 188 int hash = hashFor(id) % elements.length; 189 190 for (int i = hash; i < elements.length; i++) { 191 IMarkerSetElement element = elements[i]; 192 if (element == null) 193 return; 194 if (element.getId() == id) { 195 rehashTo(i); 196 elementCount--; 197 } 198 } 199 200 for (int i = 0; i < hash - 1; i++) { 201 IMarkerSetElement element = elements[i]; 202 if (element == null) 203 return; 204 if (element.getId() == id) { 205 rehashTo(i); 206 elementCount--; 207 } 208 } 209 } 210 211 public void remove(IMarkerSetElement element) { 212 remove(element.getId()); 213 } 214 215 public void removeAll(IMarkerSetElement[] toRemove) { 216 for (int i = 0; i < toRemove.length; i++) 217 remove(toRemove[i]); 218 } 219 220 private boolean shouldGrow() { 221 return elementCount > elements.length * 0.75; 222 } 223 224 public int size() { 225 return elementCount; 226 } 227 228 231 public void shareStrings(StringPool set) { 232 Object [] array = elements; 234 if (array == null) 235 return; 236 for (int i = 0; i < array.length; i++) { 237 Object o = array[i]; 238 if (o instanceof String ) 239 array[i] = set.add((String ) o); 240 if (o instanceof IStringPoolParticipant) 241 ((IStringPoolParticipant) o).shareStrings(set); 242 } 243 } 244 } 245 | Popular Tags |