1 24 25 package com.mckoi.util; 26 27 32 33 public final class IntegerVector implements java.io.Serializable { 34 35 38 protected int[] list; 39 40 43 protected int index; 44 45 48 public IntegerVector() { 49 this(32); 50 } 51 52 public IntegerVector(int initial_list_size) { 53 index = 0; 54 list = new int[initial_list_size]; 55 } 56 57 public IntegerVector(IntegerVector vec) { 58 if (vec != null && vec.list != null) { 59 list = new int[vec.list.length]; 60 index = vec.index; 61 System.arraycopy(vec.list, 0, list, 0, index); 62 } 63 else { 64 index = 0; 65 list = new int[0]; 66 } 67 } 68 69 public IntegerVector(IntegerListInterface i_list) { 70 this(i_list.size()); 71 if (i_list instanceof AbstractBlockIntegerList) { 72 AbstractBlockIntegerList bilist = (AbstractBlockIntegerList) i_list; 73 int bill_size = bilist.size(); 74 bilist.copyToArray(list, 0, bill_size); 75 index = bill_size; 76 } 77 else { 78 IntegerIterator i = i_list.iterator(); 79 while (i.hasNext()) { 82 list[index] = i.next(); 83 ++index; 84 } 85 } 86 } 87 88 89 92 private void ensureCapacityForAddition() { 93 if (index >= list.length) { 94 int[] old_arr = list; 95 96 int grow_size = old_arr.length + 1; 97 if (grow_size > 35000) { 99 grow_size = 35000; 100 } 101 102 int new_size = old_arr.length + grow_size; 103 list = new int[new_size]; 104 System.arraycopy(old_arr, 0, list, 0, index); 105 } 106 } 107 108 111 private void ensureCapacityForAdditions(int n) { 112 int intended_size = index + n; 113 if (intended_size > list.length) { 114 int[] old_arr = list; 115 116 int grow_size = old_arr.length + 1; 117 if (grow_size > 35000) { 119 grow_size = 35000; 120 } 121 122 int new_size = Math.max(old_arr.length + grow_size, intended_size); 123 list = new int[new_size]; 124 System.arraycopy(old_arr, 0, list, 0, index); 125 } 126 } 127 128 131 public void addInt(int val) { 132 136 ensureCapacityForAddition(); 137 138 list[index] = val; 139 ++index; 140 } 141 142 145 public void removeIntAt(int pos) { 146 --index; 147 System.arraycopy(list, pos + 1, list, pos, (index - pos)); 148 } 149 150 153 public void removeInt(int val) { 154 int pos = indexOf(val); 155 if (pos == -1) { 156 throw new RuntimeException ("Tried to remove none existant int."); 157 } 158 removeIntAt(pos); 159 } 160 161 170 public void crop(int start, int end) { 171 if (start < 0) { 172 throw new Error ("Crop start < 0."); 173 } 174 else if (start == 0) { 175 if (end > index) { 176 throw new Error ("Crop end was past end."); 177 } 178 index = end; 179 } 180 else { 181 if (start >= index) { 182 throw new Error ("start >= index"); 183 } 184 int length = (end - start); 185 if (length < 0) { 186 throw new Error ("end - start < 0"); 187 } 188 System.arraycopy(list, start, list, 0, length); 189 index = length; 190 } 191 } 192 193 194 197 public void insertIntAt(int val, int pos) { 198 if (pos >= index) { 199 throw new ArrayIndexOutOfBoundsException (pos + " >= " + index); 200 } 201 202 206 ensureCapacityForAddition(); 207 System.arraycopy(list, pos, list, pos + 1, (index - pos)); 208 ++index; 209 list[pos] = val; 210 } 211 212 216 public int setIntAt(int val, int pos) { 217 if (pos >= index) { 218 throw new ArrayIndexOutOfBoundsException (pos + " >= " + index); 219 } 220 221 int old = list[pos]; 222 list[pos] = val; 223 return old; 224 } 225 226 232 public int placeIntAt(int val, int pos) { 233 int llength = list.length; 234 if (pos >= list.length) { 235 ensureCapacityForAdditions((llength - index) + (pos - llength) + 5); 236 } 237 238 if (pos >= index) { 239 index = pos + 1; 240 } 241 242 int old = list[pos]; 243 list[pos] = val; 244 return old; 245 } 246 247 248 251 public IntegerVector append(IntegerVector vec) { 252 if (vec != null) { 253 int size = vec.size(); 254 ensureCapacityForAdditions(size); 256 257 System.arraycopy(vec.list, 0, list, index, size); 259 index += size; 260 261 } 266 return this; 267 } 268 269 272 public int intAt(int pos) { 273 if (pos >= index) { 274 throw new ArrayIndexOutOfBoundsException (pos + " >= " + index); 275 } 276 277 return list[pos]; 278 } 279 280 284 public int indexOf(int val) { 285 for (int i = 0; i < index; ++i) { 286 if (list[i] == val) { 287 return i; 288 } 289 } 290 return -1; 291 } 292 293 296 public boolean contains(int val) { 297 return (indexOf(val) != -1); 298 } 299 300 303 public int getSize() { 304 return index; 305 } 306 307 310 public int size() { 311 return index; 312 } 313 314 317 public int[] toIntArray() { 318 if (getSize() != 0) { 319 int[] out_list = new int[getSize()]; 320 System.arraycopy(list, 0, out_list, 0, getSize()); 321 return out_list; 322 } 323 return null; 324 } 325 326 329 public void clear() { 330 index = 0; 331 } 332 333 336 public String toString() { 337 StringBuffer buf = new StringBuffer (); 338 for (int i = 0; i < index; ++i) { 339 buf.append(list[i]); 340 buf.append(", "); 341 } 342 return new String (buf); 343 } 344 345 348 public boolean equals(IntegerVector ivec) { 349 int dest_index = ivec.index; 350 if (index != dest_index) { 351 return false; 352 } 353 for (int i = 0; i < index; ++i) { 354 if (list[i] != ivec.list[i]) { 355 return false; 356 } 357 } 358 return true; 359 } 360 361 366 public void reverse() { 367 final int upper = index - 1; 368 final int bounds = index / 2; 369 int end_index, temp; 370 371 375 for (int i = 0; i < bounds; ++i) { 376 end_index = upper - i; 377 378 temp = list[i]; 379 list[i] = list[end_index]; 380 list[end_index] = temp; 381 } 382 } 383 384 385 386 387 391 392 395 public final void quickSort(int min, int max) { 396 int left = min; 397 int right = max; 398 399 if (max > min) { 400 int mid = list[(min + max) / 2]; 401 while (left < right) { 402 while (left < max && list[left] < mid) { 403 ++left; 404 } 405 while (right > min && list[right] > mid) { 406 --right; 407 } 408 if (left <= right) { 409 if (left != right) { 410 int t = list[left]; 411 list[left] = list[right]; 412 list[right] = t; 413 } 414 415 ++left; 416 --right; 417 } 418 419 } 420 421 if (min < right) { 422 quickSort(min, right); 423 } 424 if (left < max) { 425 quickSort(left, max); 426 } 427 428 } 429 } 430 431 434 public final void quickSort() { 435 quickSort(0, index - 1); 436 } 437 438 444 public final int sortedIndexOf(int val, int lower, int higher) { 445 446 if (lower >= higher) { 447 if (lower < index && val > list[lower]) { 448 return lower + 1; 449 } 450 else { 451 return lower; 452 } 453 } 454 455 int mid = (lower + higher) / 2; 456 int mid_val = list[mid]; 457 458 if (val == mid_val) { 459 return mid; 460 } 461 else if (val < mid_val) { 462 return sortedIndexOf(val, lower, mid - 1); 463 } 464 else { 465 return sortedIndexOf(val, mid + 1, higher); 466 } 467 468 } 469 470 475 public final int sortedIndexOf(int val) { 476 return sortedIndexOf(val, 0, index - 1); 477 } 478 479 483 public final int sortedIntCount(int val) { 484 if (index == 0) { 485 return 0; 486 } 487 488 int count = 0; 489 int size = index - 1; 490 491 int i = sortedIndexOf(val, 0, size); 492 if (i > size) { 493 return 0; 494 } 495 int temp_i = i; 496 497 while (temp_i >= 0 && list[temp_i] == val) { 498 ++count; 499 --temp_i; 500 } 501 temp_i = i + 1; 502 while (temp_i <= size && list[temp_i] == val) { 503 ++count; 504 ++temp_i; 505 } 506 507 return count; 508 509 } 510 511 512 513 516 public boolean isSorted() { 517 int cur = Integer.MIN_VALUE; for (int i = 0; i < index; ++i) { 519 int a = list[i]; 520 if (a >= cur) { 521 cur = a; 522 } 523 else { 524 return false; 525 } 526 } 527 return true; 528 } 529 530 } 531 | Popular Tags |