@Test public void testLastItemSmaller() throws InvalidMarkupException { VirtualCharSequence vcs = new XmlToStringWrapper(new NodeList()); for (int i = 1; i < 100; i++) { int[] a = new int[i]; int counter = 0; for (int j = 0; j < i; j++) { a[j] = counter; counter += 1 + (int) Math.round(Math.random() * 10); } for (int j = 0; j < counter + 10; j++) { for (int k = 0; k < vcs.lastItemSmallerLinear(a, j, 0, a.length, false); k++) { int resultA = vcs.lastItemSmaller(a, j, k, a.length, true); int resultB = vcs.lastItemSmallerLinear(a, j, k, a.length, true); assert (resultA == resultB); resultA = vcs.lastItemSmaller(a, j, k, a.length, false); resultB = vcs.lastItemSmallerLinear(a, j, k, a.length, false); assert (resultA == resultB); } } } }
protected int lastItemSmaller(int[] l, int value, int startAt, int endAt, boolean orEqual) { int low = startAt; int high = endAt - 1; while (low <= high) { int mid = (low + high) >>> 1; int mval = l[mid]; if (mval < value + (orEqual ? 1 : 0)) low = mid + 1; else high = mid - 1; } if (low != high + 1) { assert (low == high - 1) : low + " " + high; } if (high == startAt - 1) return -1; else return high; }
VirtualCharSequence implements CharSequence { protected int lastItemSmaller(int[] l, int value, int startAt, int endAt, boolean orEqual) { int low = startAt; int high = endAt - 1; while (low <= high) { int mid = (low + high) >>> 1; int mval = l[mid]; if (mval < value + (orEqual ? 1 : 0)) low = mid + 1; else high = mid - 1; } if (low != high + 1) { assert (low == high - 1) : low + " " + high; } if (high == startAt - 1) return -1; else return high; } }
VirtualCharSequence implements CharSequence { protected int lastItemSmaller(int[] l, int value, int startAt, int endAt, boolean orEqual) { int low = startAt; int high = endAt - 1; while (low <= high) { int mid = (low + high) >>> 1; int mval = l[mid]; if (mval < value + (orEqual ? 1 : 0)) low = mid + 1; else high = mid - 1; } if (low != high + 1) { assert (low == high - 1) : low + " " + high; } if (high == startAt - 1) return -1; else return high; } VirtualCharSequence(List<ITextToken> textTokens);  VirtualCharSequence(List<ITextToken> textTokens, boolean decodeEntities); }
VirtualCharSequence implements CharSequence { protected int lastItemSmaller(int[] l, int value, int startAt, int endAt, boolean orEqual) { int low = startAt; int high = endAt - 1; while (low <= high) { int mid = (low + high) >>> 1; int mval = l[mid]; if (mval < value + (orEqual ? 1 : 0)) low = mid + 1; else high = mid - 1; } if (low != high + 1) { assert (low == high - 1) : low + " " + high; } if (high == startAt - 1) return -1; else return high; } VirtualCharSequence(List<ITextToken> textTokens);  VirtualCharSequence(List<ITextToken> textTokens, boolean decodeEntities); List<String> getComponentStrings(); ITextToken getTokenAt(int index); char charAt(int index); VirtualCharSequence setCharAt(int index, char c); int length(); CharSequence subSequence(int start, int end); int indexOf(String s, int fromIndex); String toString(); void insert(int start, String newText); void delete(int start, int length); VirtualCharSequence replace(int start, int length, String newText); VirtualCharSequence replace(int start, int length, String newText, boolean snapNext); void replaceSmart(int start, int length, String newText); void replaceSmart(int start, int length, String newText, int secondsTimeout, boolean enableLineOptimization); void flattenArea(int start, int length); static String quoteReplacement(String s); VirtualCharSequence replaceAll(Pattern p, String replacement, boolean smartMerging); VirtualCharSequence replaceAll(String regex, String replacement, boolean smartMerging); int replaceMatch(Matcher m, String replacement, boolean smartMerging); VirtualCharSequence trimStart(); VirtualCharSequence trimEnd(); VirtualCharSequence trim(); VirtualCharSequence append(String newText); }
VirtualCharSequence implements CharSequence { protected int lastItemSmaller(int[] l, int value, int startAt, int endAt, boolean orEqual) { int low = startAt; int high = endAt - 1; while (low <= high) { int mid = (low + high) >>> 1; int mval = l[mid]; if (mval < value + (orEqual ? 1 : 0)) low = mid + 1; else high = mid - 1; } if (low != high + 1) { assert (low == high - 1) : low + " " + high; } if (high == startAt - 1) return -1; else return high; } VirtualCharSequence(List<ITextToken> textTokens);  VirtualCharSequence(List<ITextToken> textTokens, boolean decodeEntities); List<String> getComponentStrings(); ITextToken getTokenAt(int index); char charAt(int index); VirtualCharSequence setCharAt(int index, char c); int length(); CharSequence subSequence(int start, int end); int indexOf(String s, int fromIndex); String toString(); void insert(int start, String newText); void delete(int start, int length); VirtualCharSequence replace(int start, int length, String newText); VirtualCharSequence replace(int start, int length, String newText, boolean snapNext); void replaceSmart(int start, int length, String newText); void replaceSmart(int start, int length, String newText, int secondsTimeout, boolean enableLineOptimization); void flattenArea(int start, int length); static String quoteReplacement(String s); VirtualCharSequence replaceAll(Pattern p, String replacement, boolean smartMerging); VirtualCharSequence replaceAll(String regex, String replacement, boolean smartMerging); int replaceMatch(Matcher m, String replacement, boolean smartMerging); VirtualCharSequence trimStart(); VirtualCharSequence trimEnd(); VirtualCharSequence trim(); VirtualCharSequence append(String newText); public HashSet<String> deletedText; }