Find an entry given its name in a sorted array of map elements. This will return the index for the closest inferior or equal item in the given array. @param name The name to find @param array The array in which to look @param len The effective length of the array @return the position of the bes
(ByteChunk name, ByteEntry[] array, int len)
| 567 | * @return the position of the best match |
| 568 | */ |
| 569 | protected static int findClosest(ByteChunk name, ByteEntry[] array, int len) { |
| 570 | |
| 571 | int a = 0; |
| 572 | int b = len - 1; |
| 573 | |
| 574 | // Special cases: -1 and 0 |
| 575 | if (b == -1) { |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | if (compare(name, array[0].name) < 0) { |
| 580 | return -1; |
| 581 | } |
| 582 | if (b == 0) { |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | int i; |
| 587 | while (true) { |
| 588 | i = (b + a) >>> 1; |
| 589 | int result = compare(name, array[i].name); |
| 590 | if (result == 1) { |
| 591 | a = i; |
| 592 | } else if (result == 0) { |
| 593 | return i; |
| 594 | } else { |
| 595 | b = i; |
| 596 | } |
| 597 | if ((b - a) == 1) { |
| 598 | int result2 = compare(name, array[b].name); |
| 599 | if (result2 < 0) { |
| 600 | return a; |
| 601 | } else { |
| 602 | return b; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /** |