scope intersectSortedIntArrays_ofs_optimized2. // is allowed to return null when result is empty // ofs is added to elements of b prior to comparison static int[] intersectSortedIntArrays_ofs_optimized2(int[] a, int[] b, int ofs, IntBuffer buf default null) { int i = 0, j = 0, la = lIntArray(a), lb = lIntArray(b); // swap if a is longer than b bool swapped = la > lb; if (swapped) { int[] temp = a; a = b; b = temp; int temp2 = la; la = lb; lb = temp2; ofs = -ofs; } // special case zero elements if (la == 0) null; // special case one element if (la == 1 && !swapped) ret intArrayBinarySearch(b, a[0]-ofs) >= 0 ? a : null; if (buf == null) buf = new IntBuffer; else buf.reset(); recurse(a, b, ofs, buf, 0, la, 0, lb); int[] out = buf.toArray(); if (swapped) { int n = lIntArray(out); for k to n: out[k] -= ofs; } ret out; } svoid #recurse(int[] a, int[] b, int ofs, IntBuffer buf, int aFrom, int aTo, int bFrom, int bTo) { if (aFrom >= aTo || bFrom >= bTo) ret; // nothing to do // start in the middle of a, search this element in b int i = (aFrom+aTo)/2; int x = a[i]; int j = intArrayBinarySearch(b, bFrom, bTo, x-ofs); ifdef intersectSortedIntArrays_ofs_optimized2_debug printVars_str(+aFrom, +aTo, +i, +x, +ofs, +bFrom, +bTo, +j); endifdef if (j >= 0) { // element found recurse(a, b, ofs, buf, aFrom, i, bFrom, j); buf.add(x); recurse(a, b, ofs, buf, i+1, aTo, j+1, bTo); } else { j = -j-1; recurse(a, b, ofs, buf, aFrom, i, bFrom, j); recurse(a, b, ofs, buf, i+1, aTo, j, bTo); } } end scope