1 24 25 package com.mckoi.util; 26 27 import java.util.ArrayList ; 28 29 37 38 public class BlockIntegerList extends AbstractBlockIntegerList { 39 40 43 public BlockIntegerList() { 44 super(); 45 } 46 47 public BlockIntegerList(IntegerVector ivec) { 48 super(ivec); 49 } 50 51 55 public BlockIntegerList(IntegerListInterface i_list) { 56 super(i_list); 57 } 58 59 61 64 protected IntegerListBlockInterface newListBlock() { 65 return new IntArrayListBlock(512); } 67 68 72 protected void deleteListBlock(IntegerListBlockInterface list_block) { 73 } 74 75 77 81 public static class IntArrayListBlock extends IntegerListBlockInterface { 82 83 86 protected int[] array; 87 88 91 protected int count; 92 93 96 protected IntArrayListBlock() { 97 super(); 98 } 99 100 103 public IntArrayListBlock(int block_size) { 104 this(); 105 array = new int[block_size]; 106 count = 0; 107 } 108 109 113 protected int[] getArray(boolean immutable) { 114 return array; 115 } 116 117 120 protected int getArrayLength() { 121 return array.length; 122 } 123 124 135 138 public final int size() { 139 return count; 140 } 141 142 145 public final boolean isFull() { 146 return count >= getArrayLength(); 147 } 148 149 152 public final boolean isEmpty() { 153 return count <= 0; 154 } 155 156 160 public final boolean canContain(int number) { 161 return count + number + 1 < getArrayLength(); 162 } 163 164 167 public int topInt() { 168 return getArray(true)[count - 1]; 169 } 170 171 174 public int bottomInt() { 175 if (count > 0) { 176 return getArray(true)[0]; 177 } 178 throw new Error ("no bottom integer."); 179 } 180 181 184 public final int intAt(int pos) { 185 return getArray(true)[pos]; 186 } 187 188 191 public final void addInt(int val) { 192 has_changed = true; 193 int[] arr = getArray(false); 194 arr[count] = val; 195 ++count; 196 } 197 198 201 public final int removeIntAt(int pos) { 202 has_changed = true; 203 int[] arr = getArray(false); 204 int val = arr[pos]; 205 System.arraycopy(array, pos + 1, arr, pos, (count - pos)); 207 --count; 208 return val; 209 } 210 211 214 public final void insertIntAt(int val, int pos) { 215 has_changed = true; 216 int[] arr = getArray(false); 217 System.arraycopy(array, pos, arr, pos + 1, (count - pos)); 218 ++count; 219 arr[pos] = val; 220 } 221 222 227 public final int setIntAt(int val, int pos) { 228 has_changed = true; 229 int[] arr = getArray(false); 230 int old = arr[pos]; 231 arr[pos] = val; 232 return old; 233 } 234 235 240 public final void moveTo(IntegerListBlockInterface dest_block, 241 int dest_index, int length) { 242 IntArrayListBlock block = (IntArrayListBlock) dest_block; 243 244 int[] arr = getArray(false); 245 int[] dest_arr = block.getArray(false); 246 247 int destb_size = block.size(); 249 if (destb_size > 0) { 250 System.arraycopy(dest_arr, 0, 251 dest_arr, length, destb_size); 252 } 253 System.arraycopy(arr, count - length, dest_arr, 0, length); 255 block.count += length; 257 count -= length; 258 has_changed = true; 260 block.has_changed = true; 261 } 262 263 266 public final void copyTo(IntegerListBlockInterface dest_block) { 267 IntArrayListBlock block = (IntArrayListBlock) dest_block; 268 int[] dest_arr = block.getArray(false); 269 System.arraycopy(getArray(true), 0, dest_arr, 0, count); 270 block.count = count; 271 block.has_changed = true; 272 } 273 274 278 public final int copyTo(int[] to, int offset) { 279 System.arraycopy(getArray(true), 0, to, offset, count); 280 return count; 281 } 282 283 286 public final void clear() { 287 has_changed = true; 288 count = 0; 289 } 290 291 296 public int iterativeSearch(int val) { 297 int[] arr = getArray(true); 298 for (int i = count - 1; i >= 0; --i) { 299 if (arr[i] == val) { 300 return i; 301 } 302 } 303 return -1; 304 } 305 306 312 public int iterativeSearch(int val, int position) { 313 int[] arr = getArray(true); 314 for (int i = position; i < count; ++i) { 315 if (arr[i] == val) { 316 return i; 317 } 318 } 319 return -1; 320 } 321 322 323 324 326 330 public final int binarySearch(Object key, IndexComparator c) { 331 int[] arr = getArray(true); 332 int low = 0; 333 int high = count - 1; 334 335 while (low <= high) { 336 int mid = (low + high) / 2; 337 int cmp = c.compare(arr[mid], key); 338 339 if (cmp < 0) 340 low = mid + 1; 341 else if (cmp > 0) 342 high = mid - 1; 343 else 344 return mid; } 346 return -(low + 1); } 348 349 350 355 public final int searchFirst(Object key, IndexComparator c) { 356 int[] arr = getArray(true); 357 int low = 0; 358 int high = count - 1; 359 360 while (low <= high) { 361 362 if (high - low <= 2) { 363 for (int i = low; i <= high; ++i) { 364 int cmp = c.compare(arr[i], key); 365 if (cmp == 0) { 366 return i; 367 } 368 else if (cmp > 0) { 369 return -(i + 1); 370 } 371 } 372 return -(high + 2); 373 } 374 375 int mid = (low + high) / 2; 376 int cmp = c.compare(arr[mid], key); 377 378 if (cmp < 0) { 379 low = mid + 1; 380 } 381 else if (cmp > 0) { 382 high = mid - 1; 383 } 384 else { 385 high = mid; 386 } 387 } 388 return -(low + 1); } 390 391 396 public final int searchLast(Object key, IndexComparator c) { 397 int[] arr = getArray(true); 398 int low = 0; 399 int high = count - 1; 400 401 while (low <= high) { 402 403 if (high - low <= 2) { 404 for (int i = high; i >= low; --i) { 405 int cmp = c.compare(arr[i], key); 406 if (cmp == 0) { 407 return i; 408 } 409 else if (cmp < 0) { 410 return -(i + 2); 411 } 412 } 413 return -(low + 1); 414 } 415 416 int mid = (low + high) / 2; 417 int cmp = c.compare(arr[mid], key); 418 419 if (cmp < 0) { 420 low = mid + 1; 421 } 422 else if (cmp > 0) { 423 high = mid - 1; 424 } 425 else { 426 low = mid; 427 } 428 } 429 return -(low + 1); } 431 432 436 public final int searchFirst(int val) { 437 int[] arr = getArray(true); 438 int low = 0; 439 int high = count - 1; 440 441 while (low <= high) { 442 443 if (high - low <= 2) { 444 for (int i = low; i <= high; ++i) { 445 if (arr[i] == val) { 446 return i; 447 } 448 else if (arr[i] > val) { 449 return -(i + 1); 450 } 451 } 452 return -(high + 2); 453 } 454 455 int mid = (low + high) / 2; 456 457 if (arr[mid] < val) { 458 low = mid + 1; 459 } 460 else if (arr[mid] > val) { 461 high = mid - 1; 462 } 463 else { 464 high = mid; 465 } 466 } 467 return -(low + 1); } 469 470 474 public final int searchLast(int val) { 475 int[] arr = getArray(true); 476 int low = 0; 477 int high = count - 1; 478 479 while (low <= high) { 480 481 if (high - low <= 2) { 482 for (int i = high; i >= low; --i) { 483 if (arr[i] == val) { 484 return i; 485 } 486 else if (arr[i] < val) { 487 return -(i + 2); 488 } 489 } 490 return -(low + 1); 491 } 492 493 int mid = (low + high) / 2; 494 495 if (arr[mid] < val) { 496 low = mid + 1; 497 } 498 else if (arr[mid] > val) { 499 high = mid - 1; 500 } 501 else { 502 low = mid; 503 } 504 } 505 return -(low + 1); } 507 508 509 510 511 514 public String toString() { 515 int[] arr = getArray(true); 516 StringBuffer buf = new StringBuffer (); 517 buf.append("( VALUES: " + count + " ) "); 518 for (int i = 0; i < count; ++i) { 519 buf.append(arr[i]); 520 buf.append(", "); 521 } 522 return new String (buf); 523 } 524 525 } 526 527 } 528 | Popular Tags |