1 8 9 package net.sourceforge.chaperon.common; 10 11 18 public class IntegerSet implements IntegerCollection 19 { 20 private int capacityIncrement = 100; 21 private int elementCount = 0; 22 private int[] list = new int[10]; 23 private int dummy; 24 25 28 public IntegerSet() {} 29 30 37 public int add(int value) 38 { 39 int index = indexOf(value); 40 41 if (index==-1) 42 { 43 ensureCapacity(elementCount+1); 44 list[elementCount++] = value; 45 return elementCount-1; 46 } 47 48 return index; 49 } 50 51 56 public void add(IntegerCollection collection) 57 { 58 for (int i = 0; i<collection.getCount(); i++) 59 add(collection.get(i)); 60 } 61 62 67 public void add(int[] array) 68 { 69 for (int i = 0; i<array.length; i++) 70 add(array[i]); 71 } 72 73 78 public void remove(int index) 79 { 80 if (index>=elementCount) 81 throw new ArrayIndexOutOfBoundsException (index); 82 83 elementCount--; 84 if (index<elementCount) 85 System.arraycopy(list, index+1, list, index, elementCount-index); 86 87 list[elementCount] = 0; 88 } 89 90 96 public void set(int index, int value) 97 { 98 if (index>=elementCount) 99 throw new ArrayIndexOutOfBoundsException (index); 100 101 if (contains(value) && (!(get(index)==value))) 102 throw new IllegalArgumentException ("Set already contains the value"); 103 104 list[index] = value; 105 } 106 107 114 public int get(int index) 115 { 116 if (index>=elementCount) 117 throw new ArrayIndexOutOfBoundsException (index); 118 119 return list[index]; 120 } 121 122 127 public int getCount() 128 { 129 return elementCount; 130 } 131 132 139 public int indexOf(int value) 140 { 141 for (int i = 0; i<elementCount; i++) 142 { 143 if (list[i]==value) 144 return i; 145 } 146 147 return -1; 148 } 149 150 157 public boolean contains(int value) 158 { 159 for (int i = 0; i<elementCount; i++) 160 { 161 if (list[i]==value) 162 return true; 163 } 164 165 return false; 166 } 167 168 173 public void removeValue(int value) 174 { 175 int index; 176 177 while ((index = indexOf(value))!=-1) 178 remove(index); 179 } 180 181 186 public boolean isEmpty() 187 { 188 return (elementCount<=0); 189 } 190 191 196 public int pop() 197 { 198 if (0>=elementCount) 199 throw new java.util.EmptyStackException (); 200 201 return list[--elementCount]; 202 } 203 204 209 public void push(int value) 210 { 211 add(value); 212 } 213 214 219 public void push(int[] values) 220 { 221 add(values); 222 } 223 224 229 public int peek() 230 { 231 if (0>=elementCount) 232 throw new java.util.EmptyStackException (); 233 234 return list[elementCount-1]; 235 } 236 237 240 public void clear() 241 { 242 elementCount = 0; 243 } 244 245 251 public void swap(int index1, int index2) 252 { 253 dummy = list[index1]; 254 list[index1] = list[index2]; 255 list[index2] = dummy; 256 } 257 258 263 public String toString() 264 { 265 StringBuffer buffer = new StringBuffer (); 266 267 buffer.append("["); 268 for (int i = 0; i<elementCount; i++) 269 { 270 buffer.append(String.valueOf(list[i])); 271 if (i<(elementCount-1)) 272 buffer.append(","); 273 } 274 275 buffer.append("]"); 276 return buffer.toString(); 277 } 278 279 284 private void ensureCapacity(int minCapacity) 285 { 286 if (list.length>=minCapacity) 287 return; 288 289 int newCapacity = list.length+capacityIncrement; 290 291 if (capacityIncrement<=0) 292 newCapacity = list.length*2; 293 294 int[] newArray = new int[Math.max(newCapacity, minCapacity)]; 295 296 System.arraycopy(list, 0, newArray, 0, list.length); 297 list = newArray; 298 } 299 } 300 | Popular Tags |