/** * Finds index of given element or negative value if element is not found. */ public int find(E element) { return find(element, 0, getLastIndex()); }
/** * Finds very first index of given element or negative value if element is not found. */ public int findFirst(E o) { return findFirst(o, 0, getLastIndex()); }
/** * Finds very last index of given element or negative value if element is not found. */ public int findLast(E o) { return findLast(o, 0, getLastIndex()); }
@Test void testFindRange() { assertEquals(3, listBinarySearch.findFirst("ddd")); assertEquals(5, listBinarySearch.findLast("ddd", 3, 9)); assertEquals(7, listBinarySearch.findFirst("iii")); assertEquals(8, listBinarySearch.findLast("iii", 7, 9)); assertEquals(2, listBinarySearch.findFirst("ccc")); assertEquals(2, listBinarySearch.findLast("ccc", 2, 9)); } }
/** * Finds index of given element in inclusive index range. Returns negative * value if element is not found. */ public int find(E element, int low, int high) { while (low <= high) { int mid = (low + high) >>> 1; int delta = compare(mid, element); if (delta < 0) { low = mid + 1; } else if (delta > 0) { high = mid - 1; } else { return mid; } } // not found return -(low + 1); }
@Test void testFind() { assertEquals(0, listBinarySearch.find("aaa")); assertEquals(1, listBinarySearch.find("bbb")); assertEquals(2, listBinarySearch.find("ccc")); assertEquals(6, listBinarySearch.find("eee")); assertEquals(9, listBinarySearch.find("sss")); assertEquals(6, listBinarySearch.find("eee", 4, 6)); assertTrue(listBinarySearch.find("aaaaa") < 0); assertTrue(listBinarySearch.find("aaa", 1, 4) < 0); assertTrue(listBinarySearch.find("eee", 4, 5) < 0); int ndx = listBinarySearch.find("ddd"); assertTrue(ndx == 3 || ndx == 4 || ndx == 5); ndx = listBinarySearch.find("iii"); assertTrue(ndx == 7 || ndx == 8); }
@Test void testFindLast() { assertEquals(0, listBinarySearch.findLast("aaa")); assertEquals(6, listBinarySearch.findLast("eee")); assertEquals(9, listBinarySearch.findLast("sss")); assertEquals(6, listBinarySearch.findLast("eee", 4, 6)); assertTrue(listBinarySearch.findLast("aaaaa") < 0); assertTrue(listBinarySearch.findLast("aaa", 1, 4) < 0); assertTrue(listBinarySearch.findLast("eee", 4, 5) < 0); assertEquals(5, listBinarySearch.findLast("ddd")); assertEquals(8, listBinarySearch.findLast("iii")); }
@Test void testFindFirst() { assertEquals(0, listBinarySearch.findFirst("aaa")); assertEquals(1, listBinarySearch.findFirst("bbb")); assertEquals(2, listBinarySearch.findFirst("ccc")); assertEquals(6, listBinarySearch.findFirst("eee")); assertEquals(9, listBinarySearch.findFirst("sss")); assertEquals(6, listBinarySearch.findFirst("eee", 4, 6)); assertTrue(listBinarySearch.findFirst("aaaaa") < 0); assertTrue(listBinarySearch.findFirst("aa") < 0); assertTrue(listBinarySearch.findFirst("aaa", 1, 4) < 0); assertTrue(listBinarySearch.findFirst("eee", 4, 5) < 0); assertEquals(3, listBinarySearch.findFirst("ddd")); assertEquals(7, listBinarySearch.findFirst("iii")); }
@BeforeEach void setUp() throws Exception { list = new ArrayList<>(); list.add("aaa"); // 0 list.add("bbb"); list.add("ccc"); list.add("ddd"); // 3 list.add("ddd"); // 4 list.add("ddd"); // 5 list.add("eee"); list.add("iii"); // 7 list.add("iii"); // 8 list.add("sss"); listBinarySearch = BinarySearch.forList(list); }
/** * Finds very first index of given element in inclusive index range. Returns negative * value if element is not found. */ public int findFirst(E o, int low, int high) { int ndx = -1; while (low <= high) { int mid = (low + high) >>> 1; int delta = compare(mid, o); if (delta < 0) { low = mid + 1; } else { if (delta == 0) { ndx = mid; } high = mid - 1; } } if (ndx == -1) { return -(low + 1); } return ndx; }
int ndx = listBS.find(set); if (ndx < 0) { list.add(set);
public ActionsManager() { this.map = new HashMap<String, ActionConfigSet>(); this.list = new SortedArrayList<ActionConfigSet>(); listMatch = new ActionPathChunksBinarySearch(); listBS = BinarySearch.forList(list); }
/** * Finds index of given element or negative value if element is not found. */ public int find(final E element) { return find(element, 0, getLastIndex()); }
/** * Finds very last index of given element or negative value if element is not found. */ public int findLast(final E o) { return findLast(o, 0, getLastIndex()); }
/** * Finds very first index of given element or negative value if element is not found. */ public int findFirst(final E o) { return findFirst(o, 0, getLastIndex()); }
/** * Finds very last index of given element in inclusive index range. Returns negative * value if element is not found. */ public int findLast(E o, int low, int high) { int ndx = -1; while (low <= high) { int mid = (low + high) >>> 1; int delta = compare(mid, o); if (delta > 0) { high = mid - 1; } else { if (delta == 0) { ndx = mid; } low = mid + 1; } } if (ndx == -1) { return -(low + 1); } return ndx; }
/** * Finds index of given element or negative value if element is not found. */ public int find(final E element) { return find(element, 0, getLastIndex()); }
/** * Finds very last index of given element or negative value if element is not found. */ public int findLast(final E o) { return findLast(o, 0, getLastIndex()); }
/** * Finds very first index of given element or negative value if element is not found. */ public int findFirst(final E o) { return findFirst(o, 0, getLastIndex()); }
/** * Finds index of given element in inclusive index range. Returns negative * value if element is not found. */ public int find(final E element, int low, int high) { while (low <= high) { int mid = (low + high) >>> 1; int delta = compare(mid, element); if (delta < 0) { low = mid + 1; } else if (delta > 0) { high = mid - 1; } else { return mid; } } // not found return -(low + 1); }