/** * Add all of the elements in the given collection to this list. */ @Override public boolean addAll(Collection<? extends E> c) { Iterator<? extends E> i = c.iterator(); boolean changed = false; while (i.hasNext()) { boolean ret = add(i.next()); if (!changed) { changed = ret; } } return changed; }
/** * Conducts a binary search to find the index where Object * should be inserted. */ protected int findInsertionPoint(E o, int low, int high) { while (low <= high) { int mid = (low + high) >>> 1; int delta = compare(get(mid), o); if (delta > 0) { high = mid - 1; } else { low = mid + 1; } } return low; }
/** * Finds the index at which object should be inserted. */ public int findInsertionPoint(E o) { return findInsertionPoint(o, 0, size() - 1); }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(E o) { int idx = 0; if (!isEmpty()) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }
@Test void testList2() { SortedArrayList<String> list = new SortedArrayList<>(); list.add("bbb"); list.add("aaa"); assertEquals(2, list.size()); assertEquals("aaa", list.get(0)); assertEquals("bbb", list.get(1)); list.add("aa"); assertEquals(3, list.size()); assertEquals("aa", list.get(0)); list.add("a"); assertEquals(4, list.size()); assertEquals("a", list.get(0)); assertEquals(1, list.findInsertionPoint("a")); }
@Test void testList1() { SortedArrayList<String> list = new SortedArrayList<>(); list.add("aaa"); list.add("bbb"); assertEquals(2, list.size()); assertEquals("aaa", list.get(0)); assertEquals("bbb", list.get(1)); list.add("ccc"); assertEquals(3, list.size()); assertEquals("ccc", list.get(2)); list.add("cc"); assertEquals(4, list.size()); assertEquals("cc", list.get(2)); list.add(2, "ddd"); fail("error"); } catch (UnsupportedOperationException e) { list.set(2, "ddd"); fail("error"); } catch (UnsupportedOperationException e) { list.addAll(2, new ArrayList<String>()); fail("error"); } catch (UnsupportedOperationException e) {
@Test void testComparator(){ Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String str1, String str2) { if (str1 == null && str2 == null) { return 0; } if (str1 == null) { return 1; } if (str2 == null) { return -1; } return str2.compareTo(str1); } }; SortedArrayList<String> list = new SortedArrayList<>(comparator); assertNotNull(list.getComparator()); list.add("aaa"); list.add("bbb"); assertEquals(2, list.size()); assertEquals("bbb", list.get(0)); assertEquals("aaa", list.get(1)); } }
@Test void testRandom() { int total = 100000; ArrayList<String> randomList = new ArrayList<>(); for (int i = 0; i < total; i++) { randomList.add(RandomString.get().random(20, 'a', 'z')); } SortedArrayList<String> sortedList = new SortedArrayList<>(randomList); Collections.sort(randomList); for (int i = 0; i < total; i++) { assertEquals(randomList.get(i), sortedList.get(i)); } }
/** * Constructs a new <code>SortedArrayList</code> expecting * elements are comparable. */ public SortedArrayList(Collection<? extends E> c) { comparator = null; addAll(c); }
/** * Returns chunk <code>deep</code> of a path at <code>index</code>. */ protected String get(int index, int deep) { return list.get(index).actionPathChunks[deep]; }
@Override protected int getLastIndex() { return list.size() - 1; } }
public ActionsManager() { this.map = new HashMap<String, ActionConfigSet>(); this.list = new SortedArrayList<ActionConfigSet>(); listMatch = new ActionPathChunksBinarySearch(); listBS = BinarySearch.forList(list); }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(final E o) { int idx = 0; if (!isEmpty()) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }
/** * Constructs a new <code>SortedArrayList</code> expecting * elements are comparable. */ public SortedArrayList(final Collection<? extends E> c) { comparator = null; addAll(c); }
protected int matchChunk(String chunk, int chunkNdx, int macroNdx, int low, int high) { for (int i = low; i <= high; i++) { ActionConfigSet set = list.get(i); // check if there is a macro on this chunk position if (macroNdx >= set.actionPathMacros.length) { continue; } ActionConfigSet.PathMacro macro = set.actionPathMacros[macroNdx]; if (macro.ndx != chunkNdx) { continue; } // match macro if (chunk.startsWith(macro.left) == false) { continue; } if (chunk.endsWith(macro.right) == false) { continue; } // match value if (macro.pattern != null) { String value = chunk.substring(macro.left.length(), chunk.length() - macro.right.length()); if (macro.pattern.matcher(value).matches() == false) { continue; } } // macro found return i; } return -1; }
/** * Finds the index at which object should be inserted. */ public int findInsertionPoint(final E o) { return findInsertionPoint(o, 0, size() - 1); }
/** * Conducts a binary search to find the index where Object * should be inserted. */ protected int findInsertionPoint(final E o, int low, int high) { while (low <= high) { int mid = (low + high) >>> 1; int delta = compare(get(mid), o); if (delta > 0) { high = mid - 1; } else { low = mid + 1; } } return low; }
/** * Adds an Object to sorted list. Object is inserted at correct place, found * using binary search. If the same item exist, it will be put to the end of * the range. * <p> * This method breaks original list contract since objects are not * added at the list end, but in sorted manner. */ @Override public boolean add(E o) { int idx = 0; if (isEmpty() == false) { idx = findInsertionPoint(o); } super.add(idx, o); return true; }